-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathContains Duplicate III.js
55 lines (43 loc) · 1.41 KB
/
Contains Duplicate III.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
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] and nums[j] is at most t and the difference between i and j is at most k.
*/
/**
The idea is like the bucket sort algorithm. Suppose we have consecutive buckets covering the range of nums with each bucket a width of (t+1). If there are two item with difference <= t, one of the two will happen:
(1) the two in the same bucket
(2) the two in neighbor buckets
*/
/**
* @param {number[]} nums
* @param {number} k
* @param {number} t
* @return {boolean}
*/
var containsNearbyAlmostDuplicate = function(nums, k, t) {
var len = nums.length,
map = {},
id,
i;
if (t < 0) {
return false;
}
for (i = 0; i < len; i++) {
id = getBucketId(nums[i], t + 1);
if (map.hasOwnProperty(id)) {
return true;
}
if (map.hasOwnProperty(id - 1) && Math.abs(map[id - 1] - nums[i]) <= t) {
return true;
}
if (map.hasOwnProperty(id + 1) && Math.abs(map[id + 1] - nums[i]) <= t) {
return true;
}
map[id] = nums[i];
if (i >= k) {
delete map[getBucketId(nums[i - k], t + 1)];
}
}
return false;
};
function getBucketId(num, bucketLength) {
return Math.floor(num / bucketLength);
}