1002. Maximum Width Ramp
My accepted Python solution to LeetCode problem 1002, Maximum Width Ramp, running in 44ms.
- Difficulty: Medium
- Python
- Runtime 44ms
- Memory 26MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 44ms, memory 26MB, accepted 2026-01-02.
class Solution:
def maxWidthRamp(self, nums: List[int]) -> int:
n = len(nums)
# Build decreasing stack of indices
stack = []
for i in range(n):
if not stack or nums[stack[-1]] > nums[i]:
stack.append(i)
# Traverse from right and find max width
max_width = 0
for j in range(n - 1, -1, -1):
while stack and nums[stack[-1]] <= nums[j]:
max_width = max(max_width, j - stack.pop())
return max_width