From : https://leetcode.com/problems/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 difference between nums[i] andnums[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) {
if(k<1 || t<0 || nums.size() < 2) return false;
multiset<int> box;
for(int i=0; i<nums.size()-1; ) {
box.insert(nums[i]);
i++;
set<int>::iterator it = box.lower_bound(nums[i]-t);
if(it!=box.end()) {
if(*it-nums[i]>=-t && *it-nums[i]<=t) return true;
}
if(i>=k) {
box.erase(nums[i-k]);
}
}
return false;
}
};