Algorithm: Leetcode Contest 388
3075. Maximize Happiness of Selected Children
Solution
class Solution {
public long maximumHappinessSum(int[] happiness, int k) {
Arrays.sort(happiness);
long res = 0;
int turn = 1;
int cur = 0;
while(turn <= k) {
cur = happiness[happiness.length - turn] - (turn - 1);
res += Math.max(cur, 0);
turn++;
}
return res;
}
}
Retro
Overall this is a pretty easy challenge, but I cannot submit it succesfully…
- notice the return type, I almost figured out the solution, but since I return a int, I cannot submit it successfully, what a pity.
- to sort array descding, using
Arrays.sort(array, Collections.reverseOrder())