### Contains Duplicate III

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 numsi and numsj 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``````

### Analyze

``````| i - j | ≤ k
| nums[i] - nums[j] | ≤ t``````
• 此外需要考虑边界值
• k <= 0、 t <= 0
``````/**
* @param {number[]} nums
* @param {number} k
* @param {number} t
* @return {boolean}
*/
var containsNearbyAlmostDuplicate = function(nums, k, t) {
if (k < 0 || t < 0) return false
const getKey = (value) => {
return Math.floor(value / (t + 1))
}

const map = new Map()

let l = 0
while (l < nums.length) {
const key = getKey(nums[l])

if (map.has(key)) {
return true
} else if (map.has(key + 1) || map.has(key - 1)) {
if (map.get(key + 1) - nums[l] <= t) { return true }
if (nums[l] - map.get(key - 1) <= t) { return true }
}

map.set(key, nums[l])

if (l >= k) {
map.delete(getKey(nums[l - k]))
}

l++
}

return false
}``````

217、219