links: Algorithms MOC


Problem

A ramp in an integer array nums is a pair (i, j) for which i < j and nums[i] <= nums[j]. The width of such a ramp is j - i.

Given an integer array nums, return the maximum width of a ramp in nums. If there is no ramp in nums, return 0.

Constraints:

  • 2 nums.length 5 *
  • 0 nums[i] 5 *

Approach 1

Using two pointer approach. Right pointer expands the range and Left pointer contracts it.


tags: array stack monotonic-stack

source: