-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsolution.cpp
58 lines (53 loc) · 1.22 KB
/
solution.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
55
56
57
58
/*
* Program to find the majority element in an array
* using Boyer Moore's voting algorithm: O(N) time and O(1) space
*/
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int majorityElement(vector<int>& nums) {
int majorityIdx = 0;
int n = nums.size();
int curCount = 1;
for (int i = 1; i < n; ++i) {
if (nums[i] != nums[majorityIdx]) {
// decreases the effective count
--curCount;
} else {
++curCount;
}
if (curCount == 0) {
// count of nums[majorityIdx] equals those not equal to it
majorityIdx = i + 1;
}
}
return nums[majorityIdx];
}
};
/*
// Using O(N) space
void solve() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
unordered_map<int, int> count;
int res = -1;
for (auto x: a) {
++count[x];
if (count[x] > n/2) {
res = x;
}
}
cout << res << endl;
}
*/
int main () {
Solution solver;
vector<int> inp = {1, 1, 1, 1, 2, 2};
cout << solver.majorityElement(inp) << endl;
return 0;
}