Description
A ramp is (i, j) where i < j and nums[i] <= nums[j]. The width is j - i. Return the maximum width of a ramp, or 0 if none exists.
Examples
Input:
nums = [6,0,8,2,1,5]Output:
4Explanation:
Max width ramp i=1, j=5.
Input:
nums = [3,4,1,2]Output:
1Explanation:
Find the maximum j-i where nums[i] <= nums[j]. Checking all valid pairs: i=0,j=1 gives width 1-0=1 (since 3<=4), i=0,j=3 gives width 3-0=3 (but 3>2, invalid), i=2,j=3 gives width 3-2=1 (since 1<=2). The maximum width is 1.
Input:
nums = [1,2,3,4,5]Output:
4Explanation:
In this strictly increasing array, it is possible to form a ramp from any earlier element to any later element. The maximum width ramp is from i=0 to j=4, giving us width 4-0=4, where nums[0]=1 <= nums[4]=5.
Constraints
- •
2 ≤ nums.length ≤ 5 × 10⁴