public class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
if (nums.length < 2 || k < 0 || t < 0) {
return false;
}
SortedSet<Long> set = new TreeSet<Long>();
for (int i = 0; i < nums.length; i++) {
long curr = (long) nums[i];
long leftBound = (long) curr - t;
long rightBound = (long) curr + t + 1;
SortedSet<Long> subset = set.subSet(leftBound, rightBound);
if (subset.size() > 0) {
return true;
}
set.add((long) nums[i]);
if (i >= k) {
set.remove((long) nums[i - k]);
}
}
return false;
}
}