Re: [閒聊] 每日leetcode已回收
看板Marginalman作者sustainer123 (caster )時間1年前 (2024/05/09 22:31)推噓4(4推 0噓 6→)留言10則, 8人參與討論串203/1549 (看更多)
※ 引述《Rushia (早瀬ユウカの体操服 )》之銘言:
: 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
: ---------------------------------------
思路:
一樣排序 本來想建堆 不過我現在腦子像一團糨糊
還是用習慣的寫法就好
Python Code:
class Solution:
def maximumHappinessSum(self, happiness: List[int], k: int) -> int:
happiness.sort()
res = 0
for i in range(k):
x = happiness[-1]-i
if x > 0:
res += x
happiness.pop()
else:
break
return res
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.43.143.130 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1715265071.A.E23.html
→
05/09 22:32,
1年前
, 1F
05/09 22:32, 1F
→
05/09 22:32,
1年前
, 2F
05/09 22:32, 2F
推
05/09 22:33,
1年前
, 3F
05/09 22:33, 3F
推
05/09 22:36,
1年前
, 4F
05/09 22:36, 4F
→
05/09 22:41,
1年前
, 5F
05/09 22:41, 5F
→
05/09 22:41,
1年前
, 6F
05/09 22:41, 6F
→
05/09 22:47,
1年前
, 7F
05/09 22:47, 7F
推
05/09 22:48,
1年前
, 8F
05/09 22:48, 8F
推
05/09 23:04,
1年前
, 9F
05/09 23:04, 9F
→
05/09 23:13,
1年前
, 10F
05/09 23:13, 10F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 203 之 1549 篇):