forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1326.cpp
More file actions
26 lines (25 loc) · 645 Bytes
/
Copy path1326.cpp
File metadata and controls
26 lines (25 loc) · 645 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
class Solution
{
public:
int minTaps(int n, vector<int>& ranges)
{
vector<int> data(n + 1);
for (int i = 0; i < ranges.size(); i++)
{
int l = max(0, i - ranges[i]), r = min(n, i + ranges[i]);
data[l] = max(data[l], r - l);
}
int pre = -1, cur = 0, step = 0;
for (int i = 0; i <= n; i++)
{
if (cur < i) return -1;
if (cur >= n) return step;
if (pre < i && i <= cur)
{
step++, pre = cur;
}
cur = max(cur, i + data[i]);
}
return step;
}
};