|
| 1 | +# [Problem 3075: Maximize Happiness of Selected Children](https://leetcode.com/problems/maximize-happiness-of-selected-children/description/?envType=daily-question) |
| 2 | + |
| 3 | +## Initial thoughts (stream-of-consciousness) |
| 4 | +We pick k children one-by-one. Every time we pick one, every remaining (unpicked) child's happiness decreases by 1 if positive. So if a child with initial happiness h is picked as the t-th pick (0-based), its contribution becomes max(h - t, 0). We need to assign k distinct children to pick positions 0..k-1 to maximize the total sum of max(h_i - t_i, 0). |
| 5 | + |
| 6 | +That suggests two choices: which k children to pick, and in what order to pick them. For any fixed set of k children, we should pick them in nonincreasing order of their original happiness (so the largest h suffer the smallest t), because that pairs larger h with smaller subtractions (rearrangement-inequality intuition). Given that, which k children to choose? It seems intuitive to pick the k largest happiness values overall, because replacing any chosen child by a larger unused one cannot decrease any max(h - t, 0) when the chosen set is sorted descending. So a natural greedy solution: sort happiness in descending order and take the top k, summing max(h[i] - i, 0) for i = 0..k-1. |
| 7 | + |
| 8 | +## Refining the problem, round 2 thoughts |
| 9 | +- Confirm ordering: For a fixed set, ordering them descending is optimal because for indices 0..k-1, subtracting 0..k-1 from the chosen values, matching largest values with smallest subtractions maximizes sum. |
| 10 | +- Confirm selection: If we had chosen some smaller element instead of a larger available one, substituting the larger for the smaller (and keeping descending order) will not reduce any term and typically increases sum. |
| 11 | +- Edge cases: many small values might become 0 when their index >= h, so we clip with max(..., 0). If k = n or k = 1 works fine. Large happiness values (up to 1e8) are fine with int arithmetic. |
| 12 | +- Complexity: sorting dominates O(n log n), memory O(1) extra (aside from sort). |
| 13 | + |
| 14 | +## Attempted solution(s) |
| 15 | +```python |
| 16 | +from typing import List |
| 17 | + |
| 18 | +class Solution: |
| 19 | + def pickGifts(self, happiness: List[int], k: int) -> int: |
| 20 | + # Sort in nonincreasing order |
| 21 | + happiness.sort(reverse=True) |
| 22 | + total = 0 |
| 23 | + # For the first k picks, the i-th pick contributes max(happiness[i] - i, 0) |
| 24 | + for i in range(k): |
| 25 | + val = happiness[i] - i |
| 26 | + if val > 0: |
| 27 | + total += val |
| 28 | + # if val <= 0, contribution is 0, so we can break early if desired |
| 29 | + else: |
| 30 | + # further i will be larger so val will be <= 0 too; can break |
| 31 | + break |
| 32 | + return total |
| 33 | +``` |
| 34 | +- Notes: |
| 35 | + - Approach: Sort happiness descending, pick the top k values and assign picks in descending order so the i-th selected child contributes max(h[i] - i, 0). |
| 36 | + - Time complexity: O(n log n) due to sorting. |
| 37 | + - Space complexity: O(1) extra (in-place sort), or O(n) if the language's sort requires extra space. |
0 commit comments