Re: [闲聊] 每日leetcode

楼主: dont   2024-12-13 09:09:34
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
```
作者: DJYOMIYAHINA (通通打死)   2023-12-13 09:09:00
别卷了
作者: Furina (芙宁娜)   2024-12-13 09:11:00
大师

Links booklink

Contact Us: admin [ a t ] ucptt.com