Given an integer array and a limit, return the length of the longest contiguous subarray where the absolute difference between any two elements is at most the limit. Pattern focus: Deque for Sliding Window Maximum. Use two monotonic deques to track the maximum and minimum values in the current window.
nums = array of integers, limit = maximum allowed difference
length of the longest valid subarray
Example 1:
Input:
nums = [8,2,4,7] limit = 4
Output:
2
Explanation:
The longest valid subarray has length 2.
Example 2:
Input:
nums = [10,1,2,4,7,2] limit = 5
Output:
4
Explanation:
The subarray [2,4,7,2] is the longest valid one.