补一下昨天的
乖乖stack 速度很慢
def minAddToMakeValid(self, s: str) -> int:
stk = []
for c in s:
if c == ')':
if len(stk)>0 and stk[-1]=='(':
stk.pop()
else:
stk.append(c)
else:
stk.append(c)
return len(stk)
今天的
对不起 我没想到
姆咪我好烂
def maxWidthRamp(self, nums: List[int]) -> int:
stk = []
for idx,num in enumerate(nums):
if len(stk)==0 or num<stk[-1][0]:
stk.append((num,idx))
ans = 0
for idx in range(len(nums)-1, -1, -1):
while len(stk)>0 and stk[-1][0]<=nums[idx]:
_, pop_idx = stk.pop()
ans = max(ans, idx-pop_idx)
return ans