Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.
Solution1:Moore voting
和169题思路Solution1相同: //www.greatytc.com/p/931ed67158fe
思路:主要思想是每次找到三个不同的元素 一起弃掉,最终剩下的就是有可能是是出现过>n/3的。
最多只能有两个出现过>n/3次的,所以定义两个candidates,如果有的话,Moore voting会选出来,但题目没有guarantee,所以最后需要再遍历count来double check。
Time Complexity: O(N) Space Complexity: O(1)
Solution2:二分治来找到最多出现的两个 再check?
Solution3:Sort
思路:sort后 找到 1/3处 和 2/3处 这两个candidates 。再double check 是否超过n/3次,check的方式因为已经排好序,可以binary search 找 起始位置和终止位置,check类似81题://www.greatytc.com/p/597d80b0c161
Solution3 refer: https://discuss.leetcode.com/topic/65330/a-binary-search-solution-assuming-the-input-array-is-sorted-beats-99/2
Code: not finished
Solution4:Hashmap
Solution1 Code:
class Solution1 {
// Moore voting algorithm
public List<Integer> majorityElement(int[] nums) {
List<Integer> result = new ArrayList<Integer>();
int candidate1 = 0, candidate2 = 0; // there are max two candidates
int count1 = 0, count2 = 0;
for(int i = 0; i < nums.length; i++) {
if(nums[i] == candidate1) {
count1++;
} else if (nums[i] == candidate2) {
count2++;
} else if(count1 == 0) {
candidate1 = nums[i];
count1 = 1;
} else if(count2 == 0) {
candidate2 = nums[i];
count2 = 1;
} else {
count1--;
count2--;
}
}
// check two candidiates, since there is no guarantee there are two that appear > n / 3
count1 = 0;
count2 = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == candidate1)
count1++;
else if (nums[i] == candidate2)
count2++;
}
if (count1 > nums.length / 3)
result.add(candidate1);
if (count2 > nums.length / 3)
result.add(candidate2);
return result;
}
}