Re: [闲聊] 每日leetcode

楼主: DJYOSHITAKA (Evans)   2024-10-10 22:45:59
补一下昨天的
乖乖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
作者: RinNoKareshi (立石凛的男友)   2024-10-10 22:46:00
别卷了
作者: NTUtriangle (国立台湾大学联盟)   2024-10-10 22:46:00
大师

Links booklink

Contact Us: admin [ a t ] ucptt.com