-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path0220-contains-duplicate-iii.js
45 lines (38 loc) · 1.09 KB
/
0220-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
/**
* 220. Contains Duplicate III
* https://leetcode.com/problems/contains-duplicate-iii/
* Difficulty: Hard
*
* You are given an integer array nums and two integers indexDiff and valueDiff.
*
* Find a pair of indices (i, j) such that:
* - i != j,
* - abs(i - j) <= indexDiff.
* - abs(nums[i] - nums[j]) <= valueDiff, and
*
* Return true if such pair exists or false otherwise.
*/
/**
* @param {number[]} nums
* @param {number} indexDiff
* @param {number} valueDiff
* @return {boolean}
*/
var containsNearbyAlmostDuplicate = function(nums, indexDiff, valueDiff) {
if (valueDiff < 0) return false;
const map = new Map();
for (let i = 0; i < nums.length; i++) {
const limit = valueDiff + 1;
const item = Math.floor(nums[i] / limit);
if (map.has(item)
|| map.has(item - 1) && Math.abs(nums[i] - map.get(item - 1)) < limit
|| map.has(item + 1) && Math.abs(nums[i] - map.get(item + 1)) < limit) {
return true;
}
map.set(item, nums[i]);
if (i >= indexDiff) {
map.delete(Math.floor(nums[i - indexDiff] / limit));
}
}
return false;
};