1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit - sliding win +heap

T: O(nlogn)

s: O(n)

```java
class Solution {
    public int longestSubarray(int[] nums, int limit) {
        Queue<Integer> minHeap = new PriorityQueue<>();
        Queue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);

        int n = nums.length;
        int left = 0;
        int right = 0;
        int result = 0;

        while (right < n) {
            int rightInt = nums[right];
            right++;
            minHeap.offer(rightInt);
            maxHeap.offer(rightInt);

            while (maxHeap.peek() - minHeap.peek() > limit) {
                    int leftInt = nums[left];
                    left++;
                    minHeap.remove(leftInt);
                    maxHeap.remove(leftInt);
            }
            result = Math.max(result, right - left);
        }
        return result;
    }
}

/*
absolute difference between any two elements of this subarray is less than or equal to limit.

abs diff <= limit

[8,2,4,7] -> not work 8-2 > limit 4


[8,2,4,7]

maxHeap -> 8 2
minHeap -> 8 2

sliding win -> but needs max and min heap

use monoqueue or use maxheap, min heap

8 2 4 7
[]

*/
```

Last updated