-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path1326.cpp
30 lines (29 loc) · 902 Bytes
/
1326.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
class Solution {
public:
static bool cmp(array<int, 2>& a, array<int, 2>& b) {
if (a[0] != b[0]) return a[0] < b[0];
return a[1] > b[1];
}
int minTaps(int n, vector<int>& ranges) {
vector<array<int, 2>> sortedRanges(n + 1);
for (int i = 0; i <= n; ++i) {
sortedRanges[i] = {i - ranges[i], i + ranges[i]};
}
sort(sortedRanges.begin(), sortedRanges.end(), cmp);
int index = 0;
int count = 0;
int far = 0;
while (index <= n) {
int nextFar = far;
while (index <= n && sortedRanges[index][0] <= far) {
nextFar = max(nextFar, sortedRanges[index][1]);
index++;
}
count++;
if (nextFar >= n) return count;
if (nextFar == far) return -1;
far = nextFar;
}
return -1;
}
};