← 返回 uber 的题目列表Longest Stable Subarray
类型:online_judge
Given an integer array nums and an integer limit, return the length of the longest contiguous subarray such that the absolute difference between any two elements in the subarray is at most limit.
Equivalently, the chosen subarray must satisfy:
max(subarray) - min(subarray) <= limit
Example 1
Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: [2,4] and [4,7] are valid; the maximum length is 2.
Example 2
Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The longest valid subarray is [2,4,7,2].
Constraints
1 <= nums.length <= 100,000
1 <= nums[i] <= 1,000,000,000
0 <= limit <= 1,000,000,000
Example
Input
4 4
8 2 4 7
Output
2