Longest Continuous Subarray Within an Absolute-Difference Limit
Problem statement
Given an integer array and a nonnegative limit, return the maximum length of a nonempty contiguous subarray whose maximum and minimum values differ by at most limit. Return 0 for an empty array.
Function
longestSubarrayWithinLimit(nums: int[], limit: int) → intExamples
Example 1
nums = [8,2,4,7]limit = 4return = 2The longest valid window has length two.
Example 2
nums = [10,1,2,4,7,2]limit = 5return = 4The window [2,4,7,2] has range five.
Example 3
nums = [4,2,2,2,4,4,2,2]limit = 0return = 3The longest constant run has length three.
Constraints
0 <= nums.length <= 100000.0 <= limit <= 10^9.