Maximize Happiness of Selected Children (LeetCode 3075): Solution and Explanation

Medium
Array
Greedy
Sorting

Pick k children over k turns. Every unpicked child loses 1 happiness per turn (never below 0). Return the largest total happiness you can collect.

Intuition

The child you pick on turn i has waited i turns, so it is worth its happiness minus i, floored at 0. That penalty depends only on the turn number, not on who you pick.

Since every turn costs the same no matter who waits, the early turns should go to the biggest values. An exchange argument confirms it: swapping a smaller early pick with a larger later one never lowers the sum.

After sorting in descending order, the answer is the sum of max(happiness[i] - i, 0) for the first k values.

Approach: Sort descending, then sum with decay

  1. Sort happiness from largest to smallest.
  2. For i from 0 to k - 1, compute gain = happiness[i] - i.
  3. If gain <= 0, stop: every later child is no happier and has waited longer, so all later gains are 0 too.
  4. Otherwise add gain to the total.
  5. Return the total, using a 64-bit integer because it can reach about 2 * 10^13.

Step-by-step example

happiness = [1,2,3], k = 2
  1. Sort descending: [3,2,1].
  2. Turn 0: pick 3, it waited 0 turns, gain 3. total = 3.
  3. Turn 1: pick 2, it waited 1 turn, gain 2 - 1 = 1. total = 4.
  4. k picks are done. Return 4.

Code: Python, Java, C++

1class Solution:
2    def maximumHappinessSum(self, happiness: List[int], k: int) -> int:
3        happiness.sort(reverse=True)
4        total = 0
5        for i in range(k):
6            gain = happiness[i] - i
7            if gain <= 0:
8                break
9            total += gain
10        return total
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n log n)

Sorting dominates. The summing loop runs at most k times.

Space

O(1)

Only the total and the loop index, beyond whatever the sort uses.

Other approaches

Max-heap (time O(n + k log n), space O(n))

Push every value into a max-heap and pop k times, subtracting the turn number each time. Same answer, useful when k is much smaller than n.

Simulate every turn (time O(nk), space O(1))

Pick the maximum, then decrement every remaining value by 1, k times. Correct but O(nk), far too slow for n = 2 * 10^5.

Common mistakes

  • Forgetting the floor at 0 and subtracting negative gains from the total.
  • Using a 32-bit total: up to 2 * 10^5 picks of 10^8 each overflows an int.
  • Actually decrementing every remaining value each turn, which turns an O(n log n) idea into O(nk).

Frequently asked questions

Why is it safe to stop at the first non-positive gain?

The values are sorted in descending order and the turn number only grows, so happiness[i] - i never increases. Once it reaches 0, every later pick also contributes 0.

Why does the order of picking not matter beyond sorting?

The penalty on turn i is i for whoever is picked. To maximize the sum you want the k largest values, and pairing larger values with smaller penalties only helps when a value would otherwise be clamped at 0.

What return type does this problem use here?

LeetCode returns a 64-bit integer. The reference solutions keep a 64-bit total, and the hidden tests on this page keep the answer within the 32-bit range so every language driver can print it; the C# version returns int.