-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
49 lines (47 loc) · 1.17 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
47
48
49
class Solution {
public:
int findSpecialInteger(vector<int>& arr) {
unordered_map<int, int> numFreq;
for (int v : arr) {
++numFreq[v];
}
for (auto [v, f] : numFreq) {
if (f > arr.size() / 4) {
return v;
}
}
return -1;
}
};
class Solution {
public:
int findSpecialInteger(vector<int>& arr) {
int n = arr.size();
int p = arr.front() - 1;
int cnt = 0;
for (int v : arr) {
if (v == p) {
++cnt;
if (cnt > n / 4) return v;
} else {
p = v;
cnt = 1;
}
}
return p;
}
};
class Solution {
public:
int findSpecialInteger(vector<int>& arr) {
int n = arr.size();
auto check = [&](int v) -> bool {
auto l = lower_bound(arr.begin(), arr.end(), v);
auto r = upper_bound(arr.begin(), arr.end(), v);
return (r - l) > arr.size() / 4;
};
if (int v = arr[n / 4]; check(v)) return v;
if (int v = arr[n / 2]; check(v)) return v;
return arr[n * 3 / 4];
}
};