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

import heapq
import collections

class Solution(object):
    def longestSubarray(self, A, limit):
        # maxd: Deque to store elements in descending order (maximum at front)
        # mind: Deque to store elements in ascending order (minimum at front)
        maxd = collections.deque()
        mind = collections.deque()
        i = 0  # Left pointer of the window

        # Iterate over the array elements using the right pointer `a`
        for a in A:
            # Maintain maxd in descending order by removing elements from the back
            while len(maxd) and a > maxd[-1]:
                maxd.pop()
            # Maintain mind in ascending order by removing elements from the back
            while len(mind) and a < mind[-1]:
                mind.pop()

            # Add the current element to both deques
            maxd.append(a)
            mind.append(a)

            # If the absolute difference between the maximum and minimum exceeds the limit
            if maxd[0] - mind[0] > limit:
                # If the maximum element in maxd is at index i, remove it
                if maxd[0] == A[i]:
                    maxd.popleft()
                # If the minimum element in mind is at index i, remove it
                if mind[0] == A[i]:
                    mind.popleft()

                # Move the left pointer `i` to the right to shrink the window
                i += 1

        # The length of the longest valid subarray is the size of the array minus the final position of i
        return len(A) - i

        # # maxq: Max-Heap to store the highest elements
        # # minq: Min-Heap to store the lowest elements
        # maxq, minq = [], []
        # res = 0  # Variable to store the longest valid subarray length
        # i = 0    # Left pointer of the window

        # # Iterate over the array elements using the right pointer `j`
        # for j, a in enumerate(A):
        #     # Push the negative of the element to maxq (to simulate a Max-Heap using Python's Min-Heap)
        #     heapq.heappush(maxq, [-a, j]) 
        #     # Push the element to minq (standard Min-Heap)
        #     heapq.heappush(minq, [a, j])

        #     # Check if the absolute difference between max and min exceeds the limit
        #     while -maxq[0][0] - minq[0][0] > limit:
        #         # Move the left pointer `i` to the right to reduce the window size
        #         # i is updated to the next index after the smaller of the maxq and minq indices
        #         # i = min(maxq[0][1], minq[0][1]) + 1
        #         i += 1

        #         # Remove elements from maxq that are outside the valid window
        #         while maxq[0][1] < i:
        #             heapq.heappop(maxq)
        #         # Remove elements from minq that are outside the valid window
        #         while minq[0][1] < i:
        #             heapq.heappop(minq)

        #     # Update the result to be the maximum length of the valid window found so far
        #     res = max(res, j - i + 1)

        # return res  # Return the maximum length of the subarray with the absolute difference within the limit


27735