-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistinctInEachK.java
More file actions
32 lines (27 loc) · 838 Bytes
/
Copy pathDistinctInEachK.java
File metadata and controls
32 lines (27 loc) · 838 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
import java.util.*;
public class Solution {
public ArrayList<Integer> dNums(ArrayList<Integer> A, int B) {
ArrayList<Integer> list = new ArrayList<>();
HashMap<Integer, Integer> map = new HashMap<>();
int i = 0;
int j = 0;
int n = A.size();
while (j < n) {
map.put(A.get(j), map.getOrDefault(A.get(j), 0) + 1);
if ((j - i + 1) > B) {
while ((j - i + 1) > B) {
map.put(A.get(i), map.get(A.get(i)) - 1);
if (map.get(A.get(i)) == 0) {
map.remove(A.get(i));
}
i++;
}
}
if ((j - i + 1) == B) {
list.add(map.size());
}
j++;
}
return list;
}
}