forked from szl0072/Leetcode-Solution-Code
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathContainsDuplicateIII.java
78 lines (66 loc) · 2.23 KB
/
ContainsDuplicateIII.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package leetcode;
import java.util.HashMap;
import java.util.TreeSet;
/**
* Project Name : Leetcode
* Package Name : leetcode
* File Name : ContainsDuplicateIII
* Creator : Edward
* Date : Dec, 2017
* Description : 220. Contains Duplicate III
*/
public class ContainsDuplicateIII {
/**
* Given an array of integers, find out whether there are two distinct indices i and j in the array such
* that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference
* between i and j is at most k.
time : O(nlogk)
space : O(k)
* @param nums
* @param k
* @param t
* @return
*/
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
if( k < 1 || t < 0) return false;
TreeSet<Long> set = new TreeSet<>();
for(int i = 0; i < nums.length; i++){
Long floor = set.floor((long)nums[i] + t);
Long ceil = set.ceiling((long)nums[i] - t);
if((floor != null && floor >= nums[i])
|| (ceil != null && ceil <= nums[i]) )
return true;
set.add((long)nums[i]);
if(i >= k){
set.remove((long)nums[i-k]);
}
}
return false;
}
/**
time : O(n)
space : O(k)
* @param nums
* @param k
* @param t
* @return
*/
public boolean containsNearbyAlmostDuplicate2(int[] nums, int k, int t) {
if (k < 1 || t < 0) return false;
HashMap<Long, Long> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
long remappedNum = (long) nums[i] - Integer.MIN_VALUE;
long bucket = remappedNum / ((long) t + 1);
if (map.containsKey(bucket)
|| (map.containsKey(bucket - 1) && remappedNum - map.get(bucket - 1) <= t)
|| (map.containsKey(bucket + 1) && map.get(bucket + 1) - remappedNum <= t))
return true;
if (map.entrySet().size() >= k) {
long lastBucket = ((long) nums[i - k] - Integer.MIN_VALUE) / ((long) t + 1);
map.remove(lastBucket);
}
map.put(bucket, remappedNum);
}
return false;
}
}