作者dont (dont)
看板Marginalman
標題Re: [閒聊] 每日leetcode
時間Thu Dec 12 18:59:38 2024
2558. Take Gifts From the Richest Pile
## 思路
max heap
## Code
```python
class Solution:
def pickGifts(self, gifts: List[int], k: int) -> int:
max_heap = [-val for val in gifts]
heapq.heapify(max_heap)
for _ in range(k):
num = -heapq.heappop(max_heap)
heapq.heappush(max_heap, -isqrt(num))
return -sum(max_heap)
```
--
昨天的
2779. Maximum Beauty of an Array After Applying Operation
## 思路
num-k ~ num+k 可以轉換成num
先排序後 用sliding window
檢查nums[left]跟nums[right]差值
```python
class Solution:
def maximumBeauty(self, nums: List[int], k: int) -> int:
nums.sort()
res = left = 0
for right, num in enumerate(nums):
while num > nums[left] + 2 * k:
left += 1
res = max(res, right-left+1)
return res
```
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 94.156.205.95 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1734001181.A.2B4.html
→ DJYOMIYAHINA: 別卷了 12/12 19:19
推 sustainer123: 大師 12/12 20:21
→ sustainer123: 我一開始用暴力解 哇哇嗚嗚嗚 12/12 20:22