962. Maximum Width Ramp

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 962

class Solution:
    def maxWidthRamp(self, nums: List[int]) -> int:
        stk = []
        for i, v in enumerate(nums):
            if not stk or nums[stk[-1]] > v:
                stk.append(i)
        res = 0
        for i in range(len(nums) - 1, -1, -1):
            while stk and nums[stk[-1]] <= nums[i]:
                res = max(res, i - stk.pop())
            if not stk:
                break
        return res