Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 函数用于在给定的时间瞬间获得赢家_Algorithm_Sorting_Dictionary_Time Complexity_Memoization - Fatal编程技术网

Algorithm 函数用于在给定的时间瞬间获得赢家

Algorithm 函数用于在给定的时间瞬间获得赢家,algorithm,sorting,dictionary,time-complexity,memoization,Algorithm,Sorting,Dictionary,Time Complexity,Memoization,我想和更聪明的程序员讨论一下最近一次采访中提出的这个问题,让你们了解我的方法,并问你们如何有效地解决这个问题。非常感谢您的见解:) 假设给您一个数组: arr = [[sam,123],[ram,124],[kris,125],[hen,127],[kris,135], [sam,140],...] 其中,每个单元格表示一个候选名称和时间戳(该时间戳不断增加,新记录不断添加)。此阵列可以有数百万条记录 编写一个函数findwiner(arr,timestamp),返回赢家的名字,直到该时间

我想和更聪明的程序员讨论一下最近一次采访中提出的这个问题,让你们了解我的方法,并问你们如何有效地解决这个问题。非常感谢您的见解:)

假设给您一个数组:

 arr = [[sam,123],[ram,124],[kris,125],[hen,127],[kris,135], [sam,140],...] 
其中,每个单元格表示一个候选名称和时间戳(该时间戳不断增加,新记录不断添加)。此阵列可以有数百万条记录

编写一个函数findwiner(arr,timestamp),返回赢家的名字,直到该时间戳,给定的时间戳可能在给定数组中,也可能不在给定数组中。领带印花“领带”

我的做法:

方法1:

a. Create a function voteCount(mydict, a) which takes an entry a and stores the count of each candidate in a dictionary and returns a dictionary say mydict
b. Create a function getMax(mydict) which takes the dictionary mydict created and sorts the dictionary with respect to values(count here), checks if there is a repeat (names) with respect to max as one edge case and print out "tie" or prints out the name of the max candidate
c. Create a function findWinner(arr, timestamp) which takes the input array arr, and a given timestamp and iterate through each entry in the array untill the given timestamp and take the name arr[i][0] and call voteCount(mydict, arr[i][0]) on it. 
d. After the loop finishes run getMax on mydict and print out the results
我考虑在这个场景中使用备忘录,因为没有空间限制

方法2:

a. Create a datastructure Node with a name and count like the one below:

class Node
 attr_accessor :name, :count
 def initialize(name,count)
  @name = name
  @count = count
 end
end
b. In the getMax(mydict), take each key value pair as a node in a heap and run a max-heapify function with node.count and print out the root name(For edge case of tie, we can check with root's left child and right child)
创建一个
SortedMap
(例如红黑树),其中键是时间戳,值是该时间戳上的获奖者的姓名。还创建一个
映射
,其中键是名称,值是分数(初始化为零)。接下来,遍历数组:

SortedMap<Integer, Set<String>> timestampMap = new RedBlackTree()
Map<String, Integer> scoreMap = new HashMap()
Set<String> currentWinners

foreach [name, timestamp] in array {
  int currentNameScore = scoreMap.get(name) + 1
  scoreMap.put(name, currentNameScore)
  int currentWinnerScore = scoreMap.get(currentWinners.first())
  if(currentWinners.contains(name) || currentNameScore > currentWinnerScore) {
    currentWinners = new Set(name)
  } else if(currentWinnerScore == currentNameScore) {
    currentWinners = currentWinners.copy().add(name)
  }
  timestampMap.put(timestamp, currentWinners)
}
SortedMap timestampap=new RedBlackTree()
Map scoreMap=newhashmap()
设置当前的赢家
数组中的foreach[name,timestamp]{
int currentNameScore=scoreMap.get(name)+1
scoreMap.put(名称,currentNameScore)
int currentWinnerScore=scoreMap.get(currentWinners.first())
if(currentWinners.contains(name)| | currentNameScore>currentWinnerScore){
currentWinners=新集合(名称)
}else if(currentWinnerScore==currentNameScore){
currentWinners=currentWinners.copy().add(名称)
}
timestamp.put(timestamp,currentWinners)
}

您使用
地图
跟踪每个名字的当前分数,然后确定名字的分数现在是否等于或超过当前获胜者的分数。查询
SortedMap
以获得给定的时间戳(或者如果给定的密钥不存在,则在前一个时间戳)是一个O(log(n))操作。(例如参见
SortedMap
上的典型操作示例)初始化
SortedMap
是一个O(n*log(n))操作,因此只有在对数据进行多次查询时才使用这种方法(否则线性搜索速度更快).

您能否将代码附加到将新项目添加到阵列的流程中?@hatchet:Hey!:)面试官没有给出答案,他直接问数据是否是这样给出的,你会如何处理这个问题。什么是赢家?数组中的每个元素是否代表一次投票,以及投票的时间?如果在被查询的时间点出现平局,该怎么办?@hatchet Winner是指从开始到给定输入时间为止投票数最大的一个,其中输入数组中的每个单元格表示一个投票(名称、时间戳),时间戳不断增加。正如我在appraoch中已经说明的,如果是平局,我们可以打印“平局”,否则打印候选人的姓名。有效解决这一问题意味着什么?如果只调用一次函数或多次调用函数,则有效的解决方案是不同的。此外,候选人的数量是否有限,是否事先知道?同时添加记录意味着什么?它们将添加到何处?输入实际上是如何组织的?时间戳参数是当前时间戳,所以我们只对当前结果感兴趣,还是对以前的结果也感兴趣?我正在写一个答案,但它与您的非常相似,现在可能是重复的。主要区别在于,赢家地图只需要在赢家改变其先前值的点记录赢家。在平局的情况下,需要注意的是“平局”,而不是赢家名单(根据OP的澄清)。@hatchet我没有看到他的澄清-在这种情况下,
SortedMap
只是一个
SortedMap