Re: [閒聊] 每日leetcode已回收

看板Marginalman作者 (早瀬ユウカの体操服 )時間1年前 (2024/05/09 09:17), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串202/1548 (看更多)
https://leetcode.com/problems/maximize-happiness-of-selected-children/description 3075. Maximize Happiness of Selected Children 給你一個正整數陣列happiness,我們可以從裡面挑出k個數字相加,每挑出一個數字其他 數字就會遞減,遞減最多只會遞減為0,求出怎麼挑可以得到最大和。 思路: 1.排序,每次都挑最大的然後挑k個,下次挑的時候要減去已經挑的數量。 py code: --------------------------------------- class Solution: def maximumHappinessSum(self, happiness: List[int], k: int) -> int: happiness.sort(reverse=True) res = 0 for i, x in enumerate(happiness[:k]): if x <= i: break res += x - i return res --------------------------------------- -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 101.138.74.198 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1715217469.A.1E0.html
文章代碼(AID): #1cF2Gz7W (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1cF2Gz7W (Marginalman)