0220.cpp (533B)
1 class Solution { 2 public: 3 bool containsNearbyAlmostDuplicate(const vector<int> &nums, int indexDiff, int valueDiff) const { 4 multiset<int> st; 5 6 for (int i = 0; i < size(nums); i++) { 7 const auto it = st.insert(nums[i]); 8 if (it != begin(st) && nums[i] - *prev(it) <= valueDiff) return true; 9 if (next(it) != end(st) && *next(it) - nums[i] <= valueDiff) return true; 10 11 if (size(st) > indexDiff) st.extract(nums[i - indexDiff]); 12 } 13 14 return false; 15 } 16 };