Re: [閒聊] 每日leetcode

看板 Marginalman
作者 Rushia (早瀬ユウカの体操服 )
時間 2024-05-09 09:17:47
留言 0則留言 (0推 0噓 0→)

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

您可能感興趣