-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContains Duplicate II.java
29 lines (27 loc) · 1.03 KB
/
Contains Duplicate II.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
class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
// 1 sweep, only stores previous k elements
Set<Integer> set = new HashSet<>();
for(int i = 0; i < nums.length; i++) {
if(i > k) set.remove(nums[i-k-1]);
if(!set.add(nums[i])) return true;
}
return false;
// Map<Integer, List<Integer>> map = new HashMap<>();
// // 1st sweep save indices
// for(int i = 0; i < nums.length; i++) {
// map.putIfAbsent(nums[i], new ArrayList<>());
// map.get(nums[i]).add(i);
// }
// // 2nd sweep check k distance
// for(int key: map.keySet()) {
// List<Integer> val = map.get(key);
// for(int i = 0; i < val.size(); i++) {
// for(int j = i+1; j < val.size(); j++) {
// if(val.get(j)-val.get(i) <= k) return true;
// }
// }
// }
// return false;
}
}