-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path229_Majority-Element-II.cpp
54 lines (51 loc) · 1.47 KB
/
229_Majority-Element-II.cpp
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
class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
int n = nums.size();
int threshold = n/3;
unordered_map<int, int> count;
for(const int &ele : nums){
count[ele]++;
}
vector<int> output;
for(const auto &it : count){
if(it.second > threshold) output.push_back(it.first);
}
return output;
}
};
class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
int lcount = 0, rcount = 0, n = nums.size();
int lval = INT_MIN, rval = INT_MIN;
// find top 2 majority elements
for(int i = 0; i < n; ++i){
if(lcount == 0 && nums[i] != rval){
lcount = 1;
lval = nums[i];
}else if(rcount == 0 && nums[i] != lval){
rcount = 1;
rval = nums[i];
}else if(nums[i] == lval){
++lcount;
}else if(nums[i] == rval){
++rcount;
}else{
--lcount;
--rcount;
}
}
// check these elements satisfy n/3 times
lcount = 0, rcount = 0;
int mm = (n/3)+1;
vector<int> ans;
for(int i = 0; i < n; ++i){
if(nums[i] == lval) ++lcount;
if(nums[i] == rval) ++rcount;
}
if(lcount >= mm) ans.push_back(lval);
if(rcount >= mm) ans.push_back(rval);
return ans;
}
};