So, for an assignment we're asked to find write a pseudocode that for a given sequence, find the largest frequency of a number from the sequence. So, a quick example would be:

因此,对于一个赋值,我们被要求找到写一个伪代码,对于给定的序列,从序列中找到一个数字的最大频率。所以,一个简单的例子是:

[ 1, 8, 5, 6, 6, 7, 6, 7, 6, 1, 1, 5, 8 ] => The number with the largest frequency is 6. The "winner" is 6.

[1,8,5,6,6,7,6,7,6,1,1,5,8] =>频率最高的数字是6.“获胜者”是6。

We have to implement it in O(nlogm) time where m is the number of distinct numbers. So, in the example above, there are 5 different numbers (m=5).

我们必须在O(nlogm)时间内实现它,其中m是不同数字的数量。因此,在上面的例子中,有5个不同的数字(m = 5)。

My approach was to go through each number in the sequence and add it to a binary tree (if not already there) and increment the frequency. Thus, for every number number in the sequence, my program goes to the binary tree, finds the element (in logm time) and increments the frequency by one. It does logm in n amount of times, so the program runs in O(nlogm). However, to find out which number had the largest frequency would take another O(m). I'm left with O(nlogm + m), by dropped the lower-order terms this leaves me with O(m) which is not what the professor is asking for.

我的方法是遍历序列中的每个数字并将其添加到二叉树(如果尚未存在)并增加频率。因此,对于序列中的每个数字编号,我的程序进入二叉树,找到元素(以logm时间)并将频率递增1。它会记录n次,因此程序在O(nlogm)中运行。但是,要找出具有最大频率的数字将需要另一个O(m)。我留下了O(nlogm + m),删掉了低阶词,这留下了O(m),这不是教授所要求的。

I remember from class that a splay tree would be a good option to use in order to keep the most frequently access item at the root, thus giving me O(1) or maybe O(logn) at most to get me the "winner"? I don't know where to begin to implement a splay tree.

我从课堂上记得,为了在根部保留最频繁的访问项目,使用splay树是一个很好的选择,因此最多给我O(1)或O(logn)让我成为“赢家” ?我不知道从哪里开始实现一个splay树。

If you could provide any insight, I would highly appreciate it.

如果您能提供任何见解,我将非常感激。

public E theWinner(E[] C) {
    int i = 0;
    while (i < C.length) {
        findCandidate(C[i], this.root);
    }
    // This is where I'm stuck, returning the winner in < O(n) time.

}

public void findNumber(E number, Node<E> root) {
    if (root.left == null && root.right == null) {
        this.add(number); 
        //splay tree?
    } else if (root.data.compareTo(number) == 0) {
        root.freqCount = root.freqCount + 1;
        //splay tree?
    } else {
        if ( root.data.compareTo(number) < 0) {
            findNumber(number, root.right);
        } else {
            findNumber(number, root.left);
        }
    }
}

1 个解决方案

#1


2

You don't need a splay tree. O(n log m + m) is O(n log m) as the number of distinct elements m is not greater than the total number of elements n. So you can iterate over all the elements in the tree after processing the input sequence to find the maximum.

你不需要一个splay树。 O(n log m + m)是O(n log m),因为不同元素的数量m不大于元素n的总数。因此,您可以在处理输入序列后迭代树中的所有元素以找到最大值。

更多相关文章

  1. 在javaScript中将数字转换为罗马数字。
  2. 数字金额转换成汉字
  3. 关于反序列化时抛出java.io.EOFException异常
  4. Java区分大小写字母数字和符号
  5. Javascript 检查字符串是否是数字的几种方法
  6. 将日期保存到序列化文件
  7. java里如何取出一个字符串中的数字?
  8. 剑指Offer(六)旋转数组的最小数字(Java版 )
  9. 第二届战神杯线上编程挑战赛月赛第二题:数字游戏(Java)

随机推荐

  1. android 笔记 --- Android大TXT文本文档
  2. Android中的IPC方式(二)
  3. Android 应用程序界面设计建议
  4. 和菜鸟一起学android4.0.3源码之lcd屏幕
  5. Android中主题与样式
  6. 【Android】Android 概述_问答
  7. 深入解析Android关机
  8. Android分析View的scrollBy()和scrollTo(
  9. Android(安卓)UI 优化 使用和 标签
  10. 【Android语音合成TTS】云知声离线TTS使