-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathMajority Element II.cpp
47 lines (34 loc) · 965 Bytes
/
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
// Runtime: 36 ms (Top 22.99%) | Memory: 15.8 MB (Top 91.31%)
class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
int n = nums.size();
vector<int> result;
int num1 = -1 , num2 = -1 , count1 = 0 , count2 = 0;
for(auto it : nums){
if(num1 == it) ++count1;
else if(num2 == it) ++count2;
else if(count1 == 0){
num1 = it;
count1 = 1;
}
else if(count2 == 0){
num2 =it;
count2 = 1;
}
else{
--count1;
--count2;
}
}
count1 = 0;
count2 = 0;
for(auto it : nums){
if(it == num1) ++count1;
else if(it == num2) ++count2;
}
if(count1>(n/3)) result.push_back(num1);
if(count2>(n/3)) result.push_back(num2);
return result;
}
};