Contains Duplicate III
Thoughts
Code
class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
if (k < 1 || t < 0) {
return false;
}
Map<Long, Long> map = new HashMap<>();
long div = (long)t + 1;
for (int i = 0; i < nums.length; i++) {
long num = (long)nums[i];
long bucket = num / div;
if (num < 0) {
bucket--;
}
if (map.containsKey(bucket)
|| map.containsKey(bucket + 1) && map.get(bucket + 1) - num <= t
|| map.containsKey(bucket - 1) && num - map.get(bucket - 1) <= t) {
return true;
}
if (i >= k) {
map.remove((long)nums[i - k] / div);
}
map.put(bucket, num);
}
return false;
}
}Analysis
PreviousSubstring with Concatenation of All WordsNext1456. Maximum Number of Vowels in a Substring of Given Length
Last updated