Skip to content

Instantly share code, notes, and snippets.

@chmllr
Created June 20, 2015 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chmllr/b4d0b8397248954e5b1e to your computer and use it in GitHub Desktop.
Save chmllr/b4d0b8397248954e5b1e to your computer and use it in GitHub Desktop.
var containsNearbyAlmostDuplicate = function (nums, k, t) {
if (k < 1 || t < 0 || nums.length < 2) return false;
var B = {}, MIN = -1 * 2147483648;
for (var i = 0; i < nums.length; ++i) {
var N = nums[i] - MIN;
var bucket = Math.floor(N / (t+1));
if (bucket in B ||
(bucket - 1) in B && Math.abs(B[bucket - 1] - N) <= t ||
(bucket + 1) in B && Math.abs(B[bucket + 1] - N) <= t)
return true;
B[bucket] = N;
if (i >= k) {
N = nums[i - k] - MIN;
bucket = Math.floor(N / (t+1));
delete B[bucket];
}
}
return false;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment