less than 1 minute read

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…

  1. notice the return type, I almost figured out the solution, but since I return a int, I cannot submit it successfully, what a pity.
  2. to sort array descding, using Arrays.sort(array, Collections.reverseOrder())