Contains NearBy Dup III
p176 Given an array of two integers, find out whether there are two distinct indiced i and j in the array such that the difference between nums[i] and nums[j] is at most t, and difference between i and j is at most k.
Sliding Window
Solution I : TreeSet
j - i <= k
Math.abs(nums[i] - nums
get all possible candidates
nums[i] >= nums[j] - t (range query : treeset)
for each candidate, check if index of j - i <= k.
public boolean containsNearbyDup (int[] array, int indexDif, int valDif) {
if (indexDif <= 0 || array.length == 0) {
return false;
}
TreeSet<Integer> window = new TreeSet<>();
for(int i = 0; i < array.length; i++) {
Integer floor = window.floor(nums[i] + t);
Integer ceiling = window.ceiling(nums[i] - t);
if (floor != null && floor >= num[i] || ceiling != null && ceiling <= nums[i]) {
return true;
}
window.add(nums[i]);
//if we have a duplicate within size k, it will return true already
if (i >= k) {
ts.remove(nums[i - k]);
}
}
return false;
}
Solution II : Buckets