← 返回 amazon 的题目列表Longest Near-Consecutive Sequence (Adjacent Difference < k)
类型:online_judge
Longest Near-Consecutive Sequence (Adjacent Difference < k)
Given an integer array nums and a positive integer k.
A sequence seq is formed by selecting distinct elements from nums (each element used at most once). After sorting seq in ascending order, if every adjacent pair satisfies:
seq[i+1] - seq[i] < k
then seq is considered "near-consecutive".
Return the maximum possible length of such a sequence.
Input
Line 1: integer array like [100,4,200,1,3,2]
Line 2: integer k
Output
An integer: the maximum length
Constraints
1 <= len(nums) <= 2e5
-1e9 <= nums[i] <= 1e9
1 <= k <= 1e9
Example 1
Input:
[100,4,200,1,3,2]
2
Output:
4
Explanation: choose {1,2,3,4}; adjacent differences are 1 < 2.
Example 2
Input:
[1,10,20,30]
5
Output:
1
Explanation: any two numbers differ by at least 5, so the maximum is 1.
Example
Input
[100,4,200,1,3,2]
2
Output
4