-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBM92.java
More file actions
32 lines (27 loc) · 887 Bytes
/
BM92.java
File metadata and controls
32 lines (27 loc) · 887 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
package NiukeTOP101;
import java.util.HashMap;
import java.util.Map;
public class BM92 {
public int maxLength (int[] arr) {
HashMap<Integer, Integer> map = new HashMap<>();
int slow = 0, fast = -1;
int maxCount = 0;
for (fast = 0; fast < arr.length; fast++) {
map.put(arr[fast], map.getOrDefault(arr[fast], 0) +1);
while (map.get(arr[fast]) > 1){
map.put(arr[slow], map.getOrDefault(arr[slow], 0) - 1);
slow ++;
}
maxCount = Math.max(fast - slow + 1, maxCount);
}
return maxCount;
}
// private boolean check(HashMap<Integer, Integer> map) {
// for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
// if(entry.getValue() > 1){
// return false;
// }
// }
// return true;
// }
}