Maximum Width Ramp LeetCode solution in Python

1
# Runtime: 635 ms (Top 30.72%) | Memory: 21.1 MB (Top 46.08%)
2
class Solution:
3
def maxWidthRamp(self, nums: List[int]):
4
st = []
5
n = len(nums)
6
for i in range(n):
7
if len(st) == 0 or nums[st[-1]] >= nums[i]:
8
st.append(i)
9
print(st)
10
max_idx = -1
11
for j in range(n - 1, -1, -1):
12
while len(st) and nums[st[-1]] <= nums[j]:
13
prev = st.pop()
14
max_idx = max(max_idx, j - prev)
15

16
return max_idx

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0