-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path962.py
27 lines (27 loc) · 1.02 KB
/
962.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
__________________________________________________________________________________________________
sample 320 ms submission
class Solution:
def maxWidthRamp(self, A: List[int]) -> int:
s = []
res = 0
for i in range(len(A)):
if not s or A[s[-1]] > A[i]:
s.append(i)
for i in range(len(A)-1, -1, -1):
while s and A[s[-1]] <= A[i]:
res = max(res, i - s.pop())
return res
__________________________________________________________________________________________________
sample 324 ms submission
class Solution:
def maxWidthRamp(self, A: List[int]) -> int:
st = []
for i in range(len(A)):
if (not st) or A[i] < A[st[-1]]:
st.append(i)
res = 0
for i in range(len(A))[::-1]:
while st and A[st[-1]] <= A[i]:
res = max(res, i - st.pop())
return res
__________________________________________________________________________________________________