<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p>

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

<pre>
<strong>Input:</strong> nums = [8,2,4,7], limit = 4
<strong>Output:</strong> 2 
<strong>Explanation:</strong> All subarrays are: 
[8] with maximum absolute diff |8-8| = 0 &lt;= 4.
[8,2] with maximum absolute diff |8-2| = 6 &gt; 4. 
[8,2,4] with maximum absolute diff |8-2| = 6 &gt; 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 &gt; 4.
[2] with maximum absolute diff |2-2| = 0 &lt;= 4.
[2,4] with maximum absolute diff |2-4| = 2 &lt;= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 &gt; 4.
[4] with maximum absolute diff |4-4| = 0 &lt;= 4.
[4,7] with maximum absolute diff |4-7| = 3 &lt;= 4.
[7] with maximum absolute diff |7-7| = 0 &lt;= 4. 
Therefore, the size of the longest subarray is 2.
</pre>

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

<pre>
<strong>Input:</strong> nums = [10,1,2,4,7,2], limit = 5
<strong>Output:</strong> 4 
<strong>Explanation:</strong> The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 &lt;= 5.
</pre>

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

<pre>
<strong>Input:</strong> nums = [4,2,2,2,4,4,2,2], limit = 0
<strong>Output:</strong> 3
</pre>

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

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

const int inf = 1e9 + 7;
vector<int> v;

struct mnx_stk {
    deque<int> mx_dq, mn_dq;

    void push(int i) {
        while (!mx_dq.empty() && v[mx_dq.back()] <= v[i])
            mx_dq.pop_back();
        while (!mn_dq.empty() && v[mn_dq.back()] >= v[i])
            mn_dq.pop_back();
        mx_dq.push_back(i), mn_dq.push_back(i);
    }

    void pop(int i) {
        while (!mx_dq.empty() && mx_dq.front() <= i)
            mx_dq.pop_front();
        while (!mn_dq.empty() && mn_dq.front() <= i)
            mn_dq.pop_front();
    }

    int max() { return (mx_dq.empty() ? -inf : v[mx_dq.front()]); }
    int min() { return (mn_dq.empty() ? inf : v[mn_dq.front()]); }
};

class Solution {
public:
    int longestSubarray(vector<int>& nums, int limit) {
        int n = nums.size();
        v = nums;

        auto valid = [&](int m) -> bool {
            mnx_stk st;
            for (int i = 0; i < m; i++) st.push(i);
            for (int i = m; i < n; i++) {
                if (st.max() - st.min() <= limit) return 1;
                st.push(i); st.pop(i - m);
            }
            return (st.max() - st.min() <= limit);
        };

        int L = 0, H = n + 1;
        while (H > L + 1) {
            int M = (L + H) / 2;
            valid(M) ? L = M : H = M;
        }
        return L;
    }
};
78036