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;
    }
};

posted on 2015-07-09 06:48  风云逸  阅读(48)  评论(0)    收藏  举报