-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMajority Number II.cpp
More file actions
35 lines (34 loc) · 895 Bytes
/
Majority Number II.cpp
File metadata and controls
35 lines (34 loc) · 895 Bytes
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
class Solution {
public:
/**
* @param nums: A list of integers
* @return: The majority number occurs more than 1/3.
*/
int majorityNumber(vector<int> nums) {
// write your code here
int num1 = 0, num2 = 1, count1 = 0, count2 = 0;
for(auto n:nums) {
if(num1 == n) count1++;
else if(num2 == n) count2++;
else if(count1 == 0) {
num1 = n;
count1++;
}
else if(count2 == 0) {
num2 = n;
count2++;
}
else {
count1--;
count2--;
}
}
count1 = 0, count2 = 0;
for(auto n:nums) {
if(n == num1) count1++;
else if(n == num2) count2++;
}
if(count1 > nums.size()/3) return num1;
return num2;
}
};