Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] andnums[j] is at most t and the difference between i and j is at most k.
Difficulty: Medium
Solution: TreeSet
public class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
if(k < 1 || t < 0) return false;
TreeSet<Integer> set = new TreeSet<Integer>();
for(int i = 0; i < nums.length; i++){
if ((set.floor(nums[i]) != null && nums[i] <= set.floor(nums[i]) + t) || (set.ceiling(nums[i]) != null && nums[i] >= set.ceiling(nums[i]) -t))
return true;
set.add(nums[i]);
if(i - k >= 0){
set.remove(nums[i - k]);
}
}
return false;
}
}