The problem requires us to maximize happiness of a group with k
children. The caveat is that for every child included in the group, the happiness for all other children decrements by 1, though it caps at 0 and cannot be negative.
Thinking logically, we can see that the children order is not our concern, we can sort the input array. Since, we need to maximize happiness, by sorting we would have access to increasing or decreasing values. This is one of the helpful patterns in problems asking top k
or bottom k
values.
We iterate from the back of the array (highest happiness), and traverse till (n-k)
(since we want only the top k children. We also maintain a increasing counter, that we subtract from every children - to simulate turns.
Code:
Python
Big O Analysis
-
Runtime
The runtime here is since we are sorting the array.
-
Memory
Constant space!
— A