Re: [閒聊] 每日leetcode

看板Marginalman作者 (dont)時間1年前 (2024/12/13 09:09), 編輯推噓1(101)
留言2則, 2人參與, 1年前最新討論串1202/1554 (看更多)
2593. Find Score of an Array After Marking All Elements ## 思路 建(num, idx) 的min heap 每次pop檢查idx有沒有mark過, 沒有就+score並mark ## Code ```python class Solution: def findScore(self, nums: List[int]) -> int: n = len(nums) heap = [] for idx, num in enumerate(nums): heapq.heappush(heap, (num, idx)) res = 0 while heap: num, idx = heapq.heappop(heap) if nums[idx] == num: res += num nums[idx] = 0 if idx: nums[idx-1] = 0 if idx < n-1: nums[idx+1] = 0 return res ``` -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 94.156.205.219 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1734052176.A.0C7.html

12/13 09:09, 1年前 , 1F
別卷了
12/13 09:09, 1F

12/13 09:11, 1年前 , 2F
大師
12/13 09:11, 2F
文章代碼(AID): #1dMubG37 (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1dMubG37 (Marginalman)