-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmajorityElementII.cpp
More file actions
48 lines (47 loc) · 1.22 KB
/
majorityElementII.cpp
File metadata and controls
48 lines (47 loc) · 1.22 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
class Solution {
public:
bool getFreq(vector<int>&nums, int a ) {
int n = nums.size();
int freq = 0;
for(int i=0; i<n; i++) {
if(nums[i]==a)
freq++;
}
if(freq>n/3)
return true;
else
return false;
}
vector<int> findCandidate(vector<int>& nums) {
int cand1 = INT_MAX, count1 = 0;
int cand2 = INT_MAX, count2 = 0;
int n = nums.size();
for(int i=0; i<n; i++) {
if(nums[i]==cand1)
count1++;
else if(nums[i]==cand2)
count2++;
else if(count1==0) {
cand1 = nums[i];
count1 = 1;
}
else if(count2==0) {
cand2 = nums[i];
count2 = 1;
}
else {
count1--;
count2--;
}
}
vector<int> ans;
if(cand1!=INT_MAX && getFreq(nums,cand1))
ans.push_back(cand1);
if(cand2!=INT_MAX && getFreq(nums,cand2))
ans.push_back(cand2);
return ans;
}
vector<int> majorityElement(vector<int>& nums) {
return findCandidate(nums);
}
};