Re: [閒聊] 每日leetcode
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
→
12/12 19:19,
1年前
, 1F
12/12 19:19, 1F
推
12/12 20:21,
1年前
, 2F
12/12 20:21, 2F
→
12/12 20:22,
1年前
, 3F
12/12 20:22, 3F
討論串 (同標題文章)
完整討論串 (本文為第 1200 之 1554 篇):