-
Notifications
You must be signed in to change notification settings - Fork 46
/
_768_1.java
38 lines (29 loc) · 1.09 KB
/
_768_1.java
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
import java.util.HashMap;
import java.util.Map;
import java.util.stream.IntStream;
public class _768_1 {
public int maxChunksToSorted(int[] arr) {
int[] sorted = IntStream.of(arr).sorted().toArray();
Map<Integer, Integer> counter = new HashMap<>();
int ans = 0;
for (int i = 0; i < arr.length; i++) {
counter.put(arr[i], counter.getOrDefault(arr[i], 0) + 1);
if (counter.containsKey(arr[i]) && counter.get(arr[i]) == 0) {
counter.remove(arr[i]);
}
counter.put(sorted[i], counter.getOrDefault(sorted[i], 0) - 1);
if (counter.containsKey(sorted[i]) && counter.get(sorted[i]) == 0) {
counter.remove(sorted[i]);
}
if (counter.size() == 0) {
ans++;
}
}
return ans;
}
public static void main(String[] args) {
_768_1 sol = new _768_1();
System.out.println(sol.maxChunksToSorted(new int[]{4, 3, 2, 1, 0}));
System.out.println(sol.maxChunksToSorted(new int[]{1, 0, 2, 3, 4}));
}
}