← 返回 roblox 的题目列表Minimum Height Difference Between Distant Peaks
类型:qbank
Select two sufficiently distant peaks with minimum height difference, using sorting or indexed scanning to satisfy the distance constraint.
Examples
Example 1:
Input: heights = [1,5,3,9,7], gap = 2
Output: 2
Explanation:
Valid pairs (|i - j| >= 2): (0,2)|1-3|=2, (0,3)|1-9|=8, (0,4)|1-7|=6, (1,3)|5-9|=4, (1,4)|5-7|=2, (2,4)|3-7|=4. Minimum is 2.
Example 2:
Input: heights = [10,20,30,40], gap = 3
Output: 30
Explanation:
Only (0,3) satisfies |i - j| >= 3, giving |10 - 40| = 30.
Example 3:
Input: heights = [1,2,3], gap = 5
Output: -1
Explanation:
No index pair is 5 apart in a length-3 array, so return -1.
Constraints
1 <= heights.length <= 10^4
0 <= heights[i] <= 10^9
1 <= gap <= 10^4