-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0220. Contains Duplicate III.js
49 lines (42 loc) · 1.06 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
46
47
48
49
// Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k.
// Example 1:
// Input: nums = [1,2,3,1], k = 3, t = 0
// Output: true
// Example 2:
// Input: nums = [1,0,1,1], k = 1, t = 2
// Output: true
// Example 3:
// Input: nums = [1,5,9,1,5,9], k = 2, t = 3
// Output: false
/**
* @param {number[]} nums
* @param {number} k
* @param {number} t
* @return {boolean}
*/
const containsNearbyAlmostDuplicate = function(nums, k, t) {
const map = nums
.map((value, index) => ({
value,
index,
}))
.sort((a, b) => a.value - b.value)
let l = 0
let r = 1
while (r < map.length) {
const diff = Math.abs(map[l].value - map[r].value)
const range = Math.abs(map[l].index - map[r].index)
if (diff <= t && range <= k) {
return true
}
if (diff > t) {
l++
} else if (range > k) {
r++
}
if (l === r) {
r++
}
}
return false
}