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

from collections import deque

class Solution:
    def longestSubarray(self, nums: List[int], limit: int) -> int:
        i, j = 0, 0
        # Keep track of max and min vals in the subarray
        max_queue = deque()
        min_queue = deque()
        max_len = 0

        # If threshold never broken, return original array
        if max(nums) - min(nums) <= limit:
            return len(nums)
        
        # Push right pointer forward as long as max num - min num in array < limit
        while j < len(nums):
            # Keep track of max and min
            max_queue = self.max_insert(max_queue, nums[j])
            min_queue = self.min_insert(min_queue, nums[j])
            if max_queue[0] - min_queue[0] > limit:
                max_len = j - i if j - i > max_len else max_len
                # While threshold not met, remove min/max and move left pointer forward
                while max_queue[0] - min_queue[0] > limit:
                    # print(max_queue, min_queue)
                    if nums[i] == max_queue[0]:
                        max_queue.popleft()
                    if nums[i] == min_queue[0]:
                        min_queue.popleft()
                    # min_queue.remove(nums[i])
                    i += 1
            j += 1

        # Once right can no longer move forward, ensure whether final valid subarray increases len
        max_len = j - i if j - i > max_len else max_len
        return max_len

    def max_insert(self, q, val):
        # max keeps track of max on left and is decreasing
        # If new val is greater than others, those are no longer important maxes.
        # They can be removed because they will be less than the added val
        # and will be removed by left pointer earlier.
        while q and q[-1] < val:
            q.pop()
        q.append(val)

        return q

    def min_insert(self, q, val):
        # min keeps track of min on left and is increasing
        # If new val is less than others, those are no longer important mins.
        # They can be removed because they will be greater than the added val
        # and will be removed by left pointer earlier.
        while q and q[-1] > val:
            q.pop()
        q.append(val)
        
        return q
499