Posted on

Description

Submission

class Solution {
public:
    bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
        multiset<long long> Set;
        int n = nums.size();
        for(int i = 0, j = 0; j < n; j++) {
            Set.insert(nums[j]);
            if(Set.size() > k+1) {
                Set.erase(Set.lower_bound(nums[i++]));
            }
            auto lo = Set.lower_bound(nums[j]);
            if(lo != Set.begin() && nums[j] - *prev(lo) <= t) return true;
            if(lo != prev(Set.end()) && *next(lo) - nums[j] <= t) return true;
        }
        return false;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *