[LeetCode] Majority Element II

简介: Well, this problem becomes a little trickier since there may be more than one majority element. But, there can be at most two of them.

Well, this problem becomes a little trickier since there may be more than one majority element. But, there can be at most two of them. This can be proved using proof by contradiction. If there are not less than 3 majority element and each appears more than n / 3 times, then nums will have 3 * n / 3 > n elements, which is a contradiction. Note that the floor sign does not affect the correctness of this proof. You may try some examples and convince yourself of this.

Now we maintain a vector<int> major for the majority elements. Once we meet an element which has appeared more than n / 3 times and have not been added to major, we add it tomajor. For counting the number of appearances, we use an unordered_map<int, int> counts. For telling whether it has been added, we use an unordered_set<int> exist to store the added elements.

Since we visit each element exactly once and the operations (search, insertion) w.r.t counts andexist (both are hash tables) is O(1), this idea can simply be proved to be of O(n) time-complexity.

The code is as follows.

 1 class Solution {
 2 public:
 3     vector<int> majorityElement(vector<int>& nums) {
 4         vector<int> major; 
 5         unordered_map<int, int> counts;
 6         unordered_set<int> exist;
 7         int n = nums.size();
 8         for (int i = 0; i < n; i++) {
 9             counts[nums[i]]++;
10             if (counts[nums[i]] > n / 3 && exist.find(nums[i]) == exist.end()) {
11                 major.push_back(nums[i]);
12                 exist.insert(nums[i]);
13             }
14             if (major.size() == 2) break;
15         }
16         return major;
17     }
18 };

Due to the use of counts,  the above code is not of O(1) space. If you want to have an O(1)-space solution, you need to adapt the idea of Boyer-Moore Voting here. This link has a nice explanation.

 1 class Solution {
 2 public:
 3     vector<int> majorityElement(vector<int>& nums) {
 4         int candidate1 = 0, candidate2 = 0, count1 = 0, count2 = 0;
 5         for (int num : nums) {
 6             if (num == candidate1) count1++;
 7             else if (num == candidate2) count2++;
 8             else {
 9                 if (!count1) {
10                     candidate1 = num;
11                     count1++;
12                 }
13                 else if (!count2) {
14                     candidate2 = num;
15                     count2++;
16                 }
17                 else {
18                     count1--;
19                     count2--;
20                 }
21             }
22         }
23         count1 = count2 = 0;
24         for (int num : nums) {
25             if (num == candidate1) count1++;
26             else if (num == candidate2) count2++;
27         }
28         int n = nums.size();
29         vector<int> majority;
30         if (count1 > n / 3) majority.push_back(candidate1);
31         if (count2 > n / 3) majority.push_back(candidate2);
32         return majority;
33     }
34 };

 

目录
相关文章
|
6月前
Leetcode 230. Kth Smallest Element in a BST
先来看下二叉搜索树的性质,对于任意一个非叶子节点,它的左子树中所有的值必定小于这个节点的val,右子树中所有的值必定大于或等于当前节点的val。 这条性质就保证了如果我们对二叉搜索树做中序遍历,中序遍历的结果肯定是有序的。对于此题而言,我们只需要拿到中序遍历结果,然后返回第k个即可,时间复杂度是O(n)。
46 1
|
算法 Python
LeetCode 169. 多数元素 Majority Element
LeetCode 169. 多数元素 Majority Element
|
Python
LeetCode 378. Kth S Element in a Sorted Matrix
给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素。 请注意,它是排序后的第k小元素,而不是第k个元素。
77 0
LeetCode 378. Kth S Element in a Sorted Matrix
|
算法
LeetCode 229. Majority Element II
给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素。 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1)。
61 0
LeetCode 229. Majority Element II
|
索引
LeetCode 162. Find Peak Element
给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1],找到峰值元素并返回其索引。 数组可能包含多个峰值,在这种情况下,返回任何一个峰值所在位置即可。
73 0
LeetCode 162. Find Peak Element
|
Java C++
LeetCode之Remove Element
LeetCode之Remove Element
86 0
LeetCode之Next Greater Element I
LeetCode之Next Greater Element I
57 0
|
算法 Java
LeetCode 229 Majority Element II(主要元素II)(Array)(Boyer–Moore majority vote algorithm)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/52356817 原文 给定一个长度为n的整型数组,找出所有出现超过 ⌊ n/3 ⌋ 次的元素。
1002 0
|
容器
LeetCode 169 Majority Element(主要元素)(vector、map)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50504698 翻译 给定一个长度为n的数组,找出主要的元素。
781 0

热门文章

最新文章