- We can maintain a sliding window with size
- How do we put elements like
[0, 1, 2, 3], t = 3into the same bucket?
x / tdoesn’t work when
t == -1. Let’s use
floor(x / t + 1)(In other languages other than JS, need to dicuss based on x is positive or not because in JS floor(-1.5) == -2).
map.set(bucketIndex, nums[i])to store elements.
- Always make sure there are only
i >= k, we need to delete the bucket that
nums[i - k]belongs to. Otherwise, in
i + 1, there will be one extra bucket.
Given an integer array
nums and two integers
true if there are two distinct indices
j in the array such that
abs(nums[i] - nums[j]) <= t and
abs(i - j) <= k.
Input: nums = [1,2,3,1], k = 3, t = 0