Re: [閒聊] 每日leetcode已回收

看板Marginalman作者 (通通打死)時間1年前 (2024/07/13 11:56), 編輯推噓4(404)
留言8則, 5人參與, 1年前最新討論串486/1553 (看更多)
蝦J8亂寫 最後zip unpack那邊 因為空list會出錯 搞了好久== 就先這樣了 應該很多地方可以優化 def survivedRobotsHealths(self, positions: List[int], healths: List[int], directions: str) -> List[int]: n = len(positions) idx = [i+1 for i in range(n)] positions, healths, directions, idx = zip(*sorted(zip(positions, healths, directions, idx))) positions, healths, directions, idx = list(positions), list(healths), list(directions), list(idx) print(positions) stk = [] ans = [] for i in range(n): if directions[i] == "R": stk.append(i) else: while stk: if healths[stk[-1]] < healths[i]: stk.pop() healths[i] -= 1 elif healths[stk[-1]] == healths[i]: stk.pop() healths[i] = 0 break else: healths[stk[-1]] -= 1 break if not stk and healths[i] > 0: ans.append(i) ans = ans + stk ans = [(idx[i], healths[i]) for i in ans] if ans: _, ans = zip(*sorted(ans)) return list(ans) else: return [] -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.228.146.144 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1720843000.A.293.html

07/13 11:58, 1年前 , 1F
大師
07/13 11:58, 1F

07/13 12:00, 1年前 , 2F
你們寫for迴圈都不會用掛號包起來嗎
07/13 12:00, 2F

07/13 12:00, 1年前 , 3F
大師
07/13 12:00, 3F

07/13 12:01, 1年前 , 4F
寶 py沒括號
07/13 12:01, 4F

07/13 12:01, 1年前 , 5F
哲維 這py
07/13 12:01, 5F

07/13 12:01, 1年前 , 6F
好吧 我只學過C++
07/13 12:01, 6F

07/13 12:02, 1年前 , 7F
c++如果只有一行的話 好像也可以不用括號
07/13 12:02, 7F

07/13 12:05, 1年前 , 8F
是沒錯 但是我強迫症會發作
07/13 12:05, 8F
文章代碼(AID): #1caVhuAJ (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1caVhuAJ (Marginalman)