[英]find major elements appears more than n/3 times
Working on below algorithm puzzle and debugging below solution works, for a few test cases. 对于一些测试用例,在解决方案下面进行下面的算法拼图和调试工作。 My confusion and question is, how could we always guarantee the count for an elements appears more than n/3 times have a positive count? 我的困惑和疑问是,我们怎样才能始终保证元素的计数出现超过n / 3次有正数? There are another 2n/3 elements which could make it count negative? 还有另外2n / 3个元素可以使它数为负数? But I tried and it always work in my samples. 但我试过,它总是在我的样本中工作。 If anyone could help to clarify, it will be great. 如果有人可以帮助澄清,那将是伟大的。
Here are the problem statement and my code/test cases, 这是问题陈述和我的代码/测试用例,
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. 给定大小为n的整数数组,找到所有出现超过⌊n /3⌋次的元素。 The algorithm should run in linear time and in O(1) space. 算法应该在线性时间和O(1)空间中运行。
def majorityElement(nums):
if not nums:
return []
count1, count2, candidate1, candidate2 = 0, 0, 0, 0
for n in nums:
if n == candidate1:
count1 += 1
elif n == candidate2:
count2 += 1
elif count1 == 0:
candidate1, count1 = n, 1
elif count2 == 0:
candidate2, count2 = n, 1
else:
count1, count2 = count1 - 1, count2 - 1
return [n for n in (candidate1, candidate2) if nums.count(n) > len(nums) // 3]
if __name__ == "__main__":
# print majorityElement([1,2,1,3,1,5,6])
print majorityElement([2,3,1,2,1,3,1,5,5,1,6])
thanks in advance, Lin 林先生,提前谢谢
Conceptually, we repeatedly apply a reduction operation to the list that involves deleting three pairwise distinct items. 从概念上讲,我们重复对列表中应用缩减操作,该操作涉及删除三个成对不同的项目。 This particular code does reductions online, so that the reduced list so far can be described by two different elements and their corresponding counts (because if there were a third element distinct from the other two, then we could reduce). 这个特殊的代码在线进行减少,因此到目前为止减少的列表可以用两个不同的元素及其相应的计数来描述(因为如果有第三个元素与其他两个元素不同,那么我们可以减少)。 At the end, we consider at most two elements for occurring more than n/3 times. 最后,我们考虑最多两个元素发生超过n / 3次。
The interesting part of the correctness proof is a lemma that, whenever we perform this reduction operation, any element that occurred more n/3 times in the old list occurs more than n'/3 times in the new list, where n is the length of the old list and n' = n-3 is the length of the new list. 正确性证明的有趣部分是一个引理,无论何时我们执行此减少操作,在旧列表中发生更多n / 3次的任何元素在新列表中发生的次数超过n'/ 3次,其中n是长度旧列表和n'= n-3是新列表的长度。 This ensures by induction that the final list contains all elements occurring more than n/3 times in the initial list (but of course the final list contains only two distinct elements). 这通过归纳确保最终列表包含在初始列表中出现超过n / 3次的所有元素(但当然最终列表仅包含两个不同的元素)。
The proof of the lemma is that, if an item occurs k times out of n in the old list, then at worst it occurs k-1 times out of n-3 in the new list, and if k/n > 1/3, then 引理的证明是,如果一个项目在旧列表中出现n次k次,则最坏的情况是在新列表中出现n-3次k-1次,并且如果k / n> 1/3 , 然后
(k-1) n
(k-1)/(n-3) = -------
(n-3) n
k (n-3) + 3 k - n
= -----------------
(n-3) n
(k/n - 1/3)
= k/n + 3 -----------
n-3
> 1/3.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.