-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay 112.java
More file actions
43 lines (33 loc) · 1.06 KB
/
Day 112.java
File metadata and controls
43 lines (33 loc) · 1.06 KB
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
import java.util.*;
class Solution {
public int minMen(int arr[]) {
int n = arr.length;
List<int[]> intervals = new ArrayList<>();
for (int i = 0; i < n; i++) {
if (arr[i] != -1) {
int left = Math.max(0, i - arr[i]);
int right = Math.min(n - 1, i + arr[i]);
intervals.add(new int[]{left, right});
}
}
if (intervals.isEmpty()) return -1;
intervals.sort((a, b) -> {
if (a[0] != b[0]) return a[0] - b[0];
return b[1] - a[1];
});
int count = 0;
int idx = 0;
int coveredTill = 0;
while (coveredTill < n) {
int farthest = coveredTill;
while (idx < intervals.size() && intervals.get(idx)[0] <= coveredTill) {
farthest = Math.max(farthest, intervals.get(idx)[1] + 1);
idx++;
}
if (farthest == coveredTill) return -1;
count++;
coveredTill = farthest;
}
return count;
}
}