This problem looks busy at first, but the accepted solution is built around one steady invariant. For 229. Majority Element II, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: straightforward implementation.
The notes already sitting in the source point us in the right direction:
- hashmap, counter
Guide
When?
This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are majorityElement.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(N), space: O(1)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//hashmap, counter
02//Runtime: 32 ms, faster than 54.63% of C++ online submissions for Majority Element II.
03//Memory Usage: 15.8 MB, less than 17.16% of C++ online submissions for Majority Element II.
04class Solution {
05public:
06 vector<int> majorityElement(vector<int>& nums) {
07 unordered_map<int, int> counter;
08
09 for(int& num : nums){
10 ++counter[num];
11 }
12
13 vector<int> ans;
14 int n = nums.size();
15
16 for(auto it = counter.begin(); it != counter.end(); ++it){
17 if(it->second > n/3){
18 ans.push_back(it->first);
19 }
20 }
21
22 return ans;
23 }
24};
25
26//Approach 1: Boyer-Moore Voting Algorithm
27//Runtime: 32 ms, faster than 54.63% of C++ online submissions for Majority Element II.
28//Memory Usage: 15.6 MB, less than 54.60% of C++ online submissions for Majority Element II.
29//time: O(N), space: O(1)
30class Solution {
31public:
32 vector<int> majorityElement(vector<int>& nums) {
33 /*
34 the initial value of a and b should be different,
35 consider [-2147483648],
36 in the second pass, a will be -2147483648 and b also be -2147483648,
37 so acounter and bcounter will be 1 after 2nd pass,
38 but bcounter should not be updated
39 */
40 int a = INT_MIN, b = INT_MIN+1;
41 int acounter = 0, bcounter = 0;
42
43 int n = nums.size();
44
45 for(int& num : nums){
46 /*
47 we should check if num == a or num == b before
48 acounter == 0 or bcounter == 0:
49 consider the case [1,1],
50 in 1st iteration, a will be 1 by acounter == 0,
51 in 2nd iteration, b will be 1 by bcounter == 0,
52 this is not what we want
53 */
54 if(num == a){
55 ++acounter;
56 }else if(num == b){
57 ++bcounter;
58 }else if(acounter == 0){
59 a = num;
60 ++acounter;
61 }else if(bcounter == 0){
62 b = num;
63 ++bcounter;
64 }else if(num != a && num != b){
65 --acounter;
66 --bcounter;
67 }
68 }
69
70 acounter = bcounter = 0;
71 for(int i = 0; i < n; ++i){
72 if(nums[i] == a) ++acounter;
73 if(nums[i] == b) ++bcounter;
74 }
75
76 vector<int> ans;
77
78 if(acounter > n/3) ans.push_back(a);
79 if(bcounter > n/3) ans.push_back(b);
80
81 return ans;
82 }
83};
Cost