Given an integer array representing the happiness of the children, select $k$ children such that the sum of their happiness is maximized.
Intuition
Since happiness of all rest children after choosing one child will decrease by 1, their relative order will still the same. So this problem is actually requiring us to select $k$ most happy children.
Approach
Use sorting.
Complexity
Time complexity: Sort the array. $$O(n\log n)$$
Space complexity: No extra spaces are needed. $$O(1)$$
Code
|
|