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.
class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
map<int, int> m;
for(int i = 0; i < nums.size(); ++i) {
auto it = m.lower_bound(nums[i] - t);
while(it != m.end() && it->second < i - k) it = m.erase(it);
if(it != m.end() && nums[i] >= it->first - t)
return true;
m[nums[i]] = i;
}
return false;
}
};
浙公网安备 33010602011771号