Return the length of the longest contiguous subarray in which the difference between the largest and the smallest value is at most limit.
For nums = [8, 2, 4, 7] and limit = 4 the answer is 2 (the subarray [2, 4]).
The starter code recomputes the largest and smallest value of the window at every step. It is correct but too slow for long arrays.
Example 1
Input:
longest_within_limit([8, 2, 4, 7], 4)
Output:
2
Example 2
Input:
longest_within_limit([10, 1, 2, 4, 7, 2], 5)
Output:
4
0 <= len(nums) <= 1000001 <= nums[i] <= 10**90 <= limit <= 10**9No results yet
Run your code to check it against the examples, or submit to run every test.