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

# 2024/09/04
# 刷uber高频题
# 
# 这题和2444. Count Subarrays With Fixed Bounds有点像
# 本质只需要求window中的最大值和最小值进行比较就行
# 但不一定和2444方法一模一样
# 思路应该还是固定end，求满足条件的最长Subarray
# Brute force是O(n^2)
# 感觉可以DP，也不对
# 没什么思路，看答案去了
# 
# Approach 1: Two Heaps
# Simply comparing boundary elements isn't enough, since removing the leftmost element might remove the current min or max and cause us to lose track of these values.
# We need a way to store and quickly retrieve potential max and min values.
# two heaps能做的原因，首先每个元素最多push和pop一次
# 其次
import heapq
class Solution:
    def longestSubarray(self, nums: List[int], limit: int) -> int:
        min_heap = []
        max_heap = []

        heapq.heappush(min_heap, (nums[0], 0))
        heapq.heappush(max_heap, (-nums[0], 0))

        left = 0
        max_length = 1
        right = 1

        while right < len(nums):
            heapq.heappush(min_heap, (nums[right], right))
            heapq.heappush(max_heap, (-nums[right], right))

            while abs(-max_heap[0][0] - min_heap[0][0]) > limit:
                left = min(max_heap[0][1], min_heap[0][1]) + 1

                while max_heap[0][1] < left:
                    heapq.heappop(max_heap)
                while min_heap[0][1] < left:
                    heapq.heappop(min_heap)

            max_length = max(max_length, right - left + 1)
            right += 1

        return max_length
# 需要再heap中记录当前值对应的index，一直等到对应left位置的value pop出来后才停止
#
# Approach 3: Two Deques
# 这个方法就和之前做过的固定size的window求max那题很像了，单调栈，只不过这里不是固定size的
# 相比于之前heap每步操作需要logn，这里O(1)就能实现
# 还是需要存tuple，其他的步骤和上面heap的方法差不多
# 自己写一下
from collections import deque
class Solution:
    def longestSubarray(self, nums: List[int], limit: int) -> int:
        min_queue = deque()
        max_queue = deque()

        min_queue.append((nums[0], 0))
        max_queue.append((nums[0], 0))

        left = 0
        max_length = 1
        right = 1

        while right < len(nums):
            while max_queue and max_queue[-1][0] < nums[right]:
                max_queue.pop()
            max_queue.append((nums[right], right))

            while min_queue and min_queue[-1][0] > nums[right]:
                min_queue.pop()
            min_queue.append((nums[right], right))

            while max_queue[0][0] - min_queue[0][0] > limit:
                left = min(max_queue[0][1], min_queue[0][1]) + 1

                while max_queue[0][1] < left:
                    max_queue.popleft()
                while min_queue[0][1] < left:
                    min_queue.popleft()

            max_length = max(max_length, right - left + 1)
            right += 1
        
        return max_length
32565