-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
46 lines (42 loc) · 1.44 KB
/
main.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
/**
* // This is the ArrayReader's API interface.
* // You should not implement it, or speculate about its implementation
* class ArrayReader {
* public:
* // Compares 4 different elements in the array
* // return 4 if the values of the 4 elements are the same (0 or 1).
* // return 2 if three elements have a value equal to 0 and one element has
* value equal to 1 or vice versa.
* // return 0 : if two element have a value equal to 0 and two elements
* have a value equal to 1. int query(int a, int b, int c, int d);
*
* // Returns the length of the array
* int length();
* };
*/
class Solution {
public:
int guessMajority(ArrayReader &reader) {
int n = reader.length();
int equal = 1, different = 0;
int opposite = -1;
auto update = [&](int i, bool isSame) -> void {
if (isSame) {
++equal;
} else {
++different;
opposite = i;
}
};
int q0123 = reader.query(0, 1, 2, 3);
int q1234 = reader.query(1, 2, 3, 4);
for (int i = 4; i < n; ++i) {
update(i, reader.query(1, 2, 3, i) == q0123);
}
update(1, q1234 == reader.query(0, 2, 3, 4));
update(2, q1234 == reader.query(0, 1, 3, 4));
update(3, q1234 == reader.query(0, 1, 2, 4));
if (equal == different) return -1;
return equal > different ? 0 : opposite;
}
};