-
Notifications
You must be signed in to change notification settings - Fork 0
/
MajorityElement2.cc
52 lines (40 loc) · 946 Bytes
/
MajorityElement2.cc
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
#include "leet.h"
#include <algorithm>
#include <cstring>
class Solution{
public:
vector<int> majorityElement(vector<int>& nums) {
const int inf = 0x3f3f3f3f;
int v1 = inf, v2 = inf;
int c1 = 0, c2 = 0;
for (auto n : nums) {
if (n == v1) {
++c1;
} else if (n == v2) {
++c2;
} else if (!c1) {
++c1;
v1 = n;
} else if (!c2) {
++c2;
v2 = n;
} else {
--c1;
--c2;
}
}
vector<int> ans;
c1 = c2 = 0;
for (auto n : nums) {
if (n == v1) ++c1;
if (n == v2) ++c1;
}
if (c1 > nums.size()/3) ans.push_back(v1);
if (c2 > nums.size()/3) ans.push_back(v2);
return ans;
}
};
int main(){
Solution slu;
return 0;
}