精華區beta Marginalman 關於我們 聯絡資訊
思路: 跟大家作法差不多 就先排序然後stack 我是回傳那邊卡很久 想說問題在哪 後來發現題目有說明要求 靠北 Python Code: class Solution: def survivedRobotsHealths(self, positions: List[int], healths: List[int], directions: str) -> List[int]: n = len(positions) original_indices = list(range(n)) zipped = sorted(zip(positions, healths, directions, original_indices)) positions, healths, directions, original_indices = zip(*zipped) healths = list(healths) r = [] for i in range(len(positions)): if directions[i] == "R": r.append(i) else: while r and healths[i] > 0: if healths[i] == healths[r[-1]]: healths[i] = 0 healths[r[-1]] = 0 r.pop() elif healths[i] > healths[r[-1]]: healths[i] -= 1 healths[r[-1]] = 0 r.pop() else: healths[i] = 0 healths[r[-1]] -= 1 survivors = [(original_indices[i], healths[i]) for i in range(n) if healths[i] > 0] survivors.sort() res = [health for _, health in survivors] return res -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.137.37.148 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1720856337.A.D7A.html
CanIndulgeMe: 技術大神 07/13 15:41
smart0eddie: 大師 07/13 16:21