Created
June 20, 2015 17:01
-
-
Save chmllr/b4d0b8397248954e5b1e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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