Re: [閒聊] 每日leetcode
看板Marginalman作者DJYOSHITAKA (franchouchouISBEST)時間1年前 (2024/10/10 22:46)推噓0(1推 1噓 1→)留言3則, 3人參與討論串972/1554 (看更多)
補一下昨天的
乖乖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
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.229.37.69 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1728571562.A.5C4.html
→
10/10 22:46,
1年前
, 1F
10/10 22:46, 1F
推
10/10 22:46,
1年前
, 2F
10/10 22:46, 2F
噓
10/10 22:54,
1年前
, 3F
10/10 22:54, 3F
討論串 (同標題文章)
完整討論串 (本文為第 972 之 1554 篇):