-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode_229.cpp
More file actions
66 lines (58 loc) · 1.75 KB
/
Copy pathLeetCode_229.cpp
File metadata and controls
66 lines (58 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// This Solution Is not Optimal For Space Complexity it take O(n) Space Becz of HashMap
// vector<int> majorityElement(vector<int>& nums) {
//int times = nums.size()/3;
// unordered_map<int,int>map;
// for(int i=0;i<nums.size();i++){
// map[nums[i]]++;
// }
// nums.clear();
// for(auto &pair : map){
// if(pair.second > times){
// nums.push_back(pair.first);
// }
// }
// return nums;
//}
// Optimal solution Both Time O(n) and Space O(1)
// Here we use extended Version of Boyer-Moore Majority Voting Algorithm
class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
int cad1 = 0, cad2 = 0, count1 = 0, count2 = 0;
int n = nums.size();
int times = n / 3;
// Step 1: Find potential candidates
for (int num : nums) {
if (num == cad1) {
count1++;
}
else if (num == cad2) {
count2++;
}
else if (count1 == 0) {
cad1 = num;
count1 = 1;
}
else if (count2 == 0) {
cad2 = num;
count2 = 1;
}
else {
count1--;
count2--;
}
}
// Step 2: Verify actual occurrences
count1 = 0;
count2 = 0;
for (int num : nums) {
if (num == cad1) count1++;
else if (num == cad2) count2++;
}
// Step 3: Prepare result
nums.clear();
if (count1 > times) nums.push_back(cad1);
if (count2 > times && cad2 != cad1) nums.push_back(cad2);
return nums;
}
};