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

class Solution:
    def longestSubarray(self, nums: List[int], limit: int) -> int:
        # time O(N)
        # space O(N)
        maxq = []
        minq = []
        
        res = 0
        left = 0
        
        for right in range(len(nums)):
            # delete all element smaller than curr, keep the 2nd/3rd... bigger one
            while maxq and nums[right] > maxq[-1]: maxq.pop()
            # delete all element bigger than curr, keep the 2nd/3rd... smaller one
            while minq and nums[right] < minq[-1]: minq.pop()
            maxq.append(nums[right])
            minq.append(nums[right])
            # print(maxq, minq)
            
            while maxq[0] - minq[0] > limit:
                if maxq[0] == nums[left]: maxq.pop(0)
                if minq[0] == nums[left]: minq.pop(0)
                left += 1
            res = max(res, right - left + 1)
        return res


        # # O(N**2)
        # res = 0
        # for i in range(len(nums)):
        #     local_min = float("inf")
        #     local_max = -float("inf")
        #     count = 0
        #     for j in range(i, len(nums)):
        #         local_min = min(local_min, nums[j])
        #         local_max = max(local_max, nums[j])
        #         if local_max - local_min <= limit:
        #             count += 1
        #             res = max(res,count)
        #         else:
        #             break
        # return res


                
#         time: O(NlogN)
#         space: O(N)
#         if not nums:
#             return 0
        
#         minheap = []
#         maxheap = []
#         res  = curr_index = 0
        
#         for index, value in enumerate(nums):
#             heapq.heappush(minheap, (value, index))
#             heapq.heappush(maxheap, (-value, index))
            
#             if abs(-maxheap[0][0] - minheap[0][0]) > limit:
# #                 minheap [(8, 0), (2, 1), (4,2)]
# #                 maxheap [(-8,0), (-2,1), (-4,2)]

#                 curr_index = min(maxheap[0][1], minheap[0][1]) + 1
#                 while maxheap[0][1] < curr_index: heapq.heappop(maxheap)
#                 while minheap[0][1] < curr_index: heapq.heappop(minheap)
#             res = max(res, index - curr_index + 1)
            
#         return res
# ----------------
        # bruct force time complexity: o(N^2)
#         if not nums:
#             return 0
        
#         subarray = []
#         for i in range(len(nums)):
#             for j in range(i, len(nums)+1):
#                 if nums[i:j]:
#                     subarray.append(nums[i:j]) 
                    
#         res = 0
#         for sub in subarray:
#             if abs(max(sub) - min(sub)) <= limit:
#                 res = max(res, len(sub))
#         return res
# -----------------
#         time complexity worst case: O(n^2)
#         on average = O(NlogN)
#         if not nums:
#             return 0
#         l = 0
#         r = 1
#         res = 0
#         while l < len(nums):
            
#             if r > len(nums):
#                 break
#             diff = abs(max(nums[l:r]) - min(nums[l:r]))
#             if diff <= limit:
#                 res = max(res, r-l)
#                 r += 1
#             else:
#                 l += 1
#         return res
1346