-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3964.cpp
More file actions
35 lines (33 loc) · 894 Bytes
/
Copy path3964.cpp
File metadata and controls
35 lines (33 loc) · 894 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
class Solution {
public:
int minLights(vector<int>& lights) {
int n = lights.size();
vector<int> diff(n + 1, 0);
for (int i = 0; i < n; ++i) {
int light = lights[i];
if (light > 0) {
int left = max(0, i - light);
int right = min(n - 1, i + light) + 1;
diff[left]++;
diff[right]--;
}
}
vector<bool> mask(n, false);
int curr = 0;
for (int i = 0; i < n; ++i) {
curr += diff[i];
if (curr > 0) mask[i] = true;
}
int cnt = 0;
int res = 0;
for (int i = 0; i < n; ++i) {
if (mask[i]) {
res += (cnt + 3 - 1) / 3;
cnt = 0;
}
else cnt++;
}
res += (cnt + 3 - 1) / 3;
return res;
}
};