 All Problems
Maximum Width Ramp
medium
stack
arrays
monotonic stack
amazon
google

A ramp in an integer array nums is a pair (i, j) where i < j and nums[i] <= nums[j]. The width of such a ramp is j - i. Return the maximum width of a ramp in nums. If there is no ramp, return 0.

Example 1:

Input: 6 0 8 2 1 5
Output: 4
Explanation: (1, 5): nums[1]=0 ≤ nums[5]=5, width = 4.

Example 2:

Input: 9 8 1 0 1 9 4 0 4 1
Output: 7

Constraints:

  • 2 ≤ nums.length ≤ 5 × 10⁴
  • 0 ≤ nums[i] ≤ 5 × 10⁴

Input format: One line — space-separated integers.

Output format: Maximum ramp width.

Run to check your code against the sample cases, or submit to run every case