-
Notifications
You must be signed in to change notification settings - Fork 0
/
169_majorityElement.cpp
51 lines (39 loc) · 988 Bytes
/
169_majorityElement.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
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
int majorityElement(std::vector<int> vec)
{
int ret = 0;
int size = vec.size();
// count
// std::map<int, int> container;
// for (int i : vec) {
// if (container.end() != container.find(i)) {
// container[i] ++;
// if (container[i] > size/2)
// return i;
// }
// else {
// container[i] = 1;
// }
// }
// sort and return middle number
// std::sort(vec.begin(), vec.end());
// return vec[size/2];
// boyce-moore vote
int count = 0;
for (auto i : vec) {
if (count == 0) {
ret = i;
}
count += (i == ret) ? 1 : -1;
}
return ret;
}
int main(int argc, char const *argv[])
{
std::vector<int> vec = {2, 4, 2, 5, 6, 3, 2, 6, 2, 2, 2};
std::cout << "result = " << majorityElement(vec) << std::endl;
return 0;
}