<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p>

<p>Find a pair of indices <code>(i, j)</code> such that:</p>

<ul>
	<li><code>i != j</code>,</li>
	<li><code>abs(i - j) &lt;= indexDiff</code>.</li>
	<li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li>
</ul>

<p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> We can choose (i, j) = (0, 3).
We satisfy the three conditions:
i != j --&gt; 0 != 3
abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3
abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3
<strong>Output:</strong> false
<strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
	<li><code>1 &lt;= indexDiff &lt;= nums.length</code></li>
	<li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li>
</ul>

// class Solution {
// public:
//     bool containsNearbyAlmostDuplicate(vector<int>& nums,
//                                     int indexDiff,
//                                     int valueDiff) {
//         //  value
//         //  ^
//         //  |
//         //  |
//         //  |
//         //  |
//         //  --(indexDiff)--x---------------------> index

//         int len = nums.size();
//         map<int, int> m;
//         for (int i = 0; i < min(len, indexDiff); ++i) {
//             m[nums[i]] += 1;
//         }

//         for (int i = 0; i < len; ++i) {

//             if (i + indexDiff < len) {
//                 m[nums[i + indexDiff]] += 1;
//             }

//             if (i - indexDiff - 1 >= 0) {
//                 int k = nums[i - indexDiff - 1];
//                 m[k] -= 1;
//                 if (m[k] == 0) {
//                     m.erase(k);
//                 }
//             }

//             int k = nums[i];
//             m[k] -= 1;
//             if (m[k] == 0) {
//                 m.erase(k);
//             }

//             auto it = m.lower_bound(k);
//             int r = 0;
//             int l = 0;
//             if (it == m.end()) {
//                 r = (*(--it)).first;
//                 l = (*(--it)).first;
//             } else if (it == m.begin()){
//                 r = (*it).first;
//                 l = (*it).first;
//             } else {
//                 r = (*it).first;
//                 l = (*(--it)).first;
//             }
//             if (abs(r - k) <= valueDiff || abs(k - l) <= valueDiff) {
//                 return true;
//             }

//             m[k] += 1;
//         }

//         return false;
//     }
// };



class Solution {
public:
    bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
        if (nums.size() < 2 || k == 0)
            return false;
        deque<int> windows_deq;
        multiset<long> windows;
        for (int i = 0; i < nums.size(); i++) {
            if (windows.size() > k) {
                int num = windows_deq.front();
                windows_deq.pop_front();
                windows.erase(windows.find(num));
            }
            auto it = windows.lower_bound((long)nums[i] - (long)t);
            if (it == windows.end() || *it > (long)nums[i] + (long)t) {
                // not found
                windows_deq.push_back(nums[i]);
                windows.insert(nums[i]);
            }
            else return true;
        }
        return false;
    }
};
149360