Re: [閒聊] 每日leetcode

看板Marginalman作者 (dont)時間1年前 (2024/12/12 18:59), 編輯推噓1(102)
留言3則, 2人參與, 1年前最新討論串1200/1554 (看更多)
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
文章代碼(AID): #1dMi8TAq (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1dMi8TAq (Marginalman)