-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathDominator.cpp
More file actions
43 lines (39 loc) · 875 Bytes
/
Dominator.cpp
File metadata and controls
43 lines (39 loc) · 875 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
35
36
37
38
39
40
41
42
43
// #define debug
#ifdef debug
#define LOG(X) cout << X << endl
#else
#define LOG(X) (void)0
#endif
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int candidate = -1;
int size = 0;
for(auto a : A) {
if(size == 0) {
size++;
candidate = a;
} else {
if(candidate == a)
size++;
else
size--;
}
}
LOG("Size = " << size);
LOG("candidate = " << candidate);
if(size > 0)
{
int index = -1;
size_t count = 0;
for(size_t i=0; i<A.size() ; i++) {
if(A[i] == candidate) {
count++;
if(index == -1)
index = i;
}
}
if(count > A.size() / 2)
return index;
}
return -1;
}