3075. Maximize Happiness of Selected Children
9th May 2024 LeetCode Daily
Problem Statement
You are given an array happiness
of length n
, and a positive integer k
.
There are n
children standing in a queue, where the i<sup>th</sup>
child has happiness value happiness[i]
. You want to select k
children from these n
children in k
turns.
In each turn, when you select a child, the happiness value of all the children that have not been selected till now decreases by 1
. Note that the happiness value cannot become negative and gets decremented only if it is positive.
Return the maximum sum of the happiness values of the selected children you can achieve by selecting k
children.
Examples
Example 1:
Input: happiness = [1,2,3], k = 2
Output: 4
Explanation: We can pick 2 children in the following way:
- Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1].
- Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0.
The sum of the happiness values of the selected children is 3 + 1 = 4.
Example 2:
Input: happiness = [1,1,1,1], k = 2
Output: 1
Explanation: We can pick 2 children in the following way:
- Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0].
- Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0].
The sum of the happiness values of the selected children is 1 + 0 = 1.
Example 3:
Input: happiness = [2,3,4,5], k = 1
Output: 5
Explanation: We can pick 1 child in the following way:
- Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3].
The sum of the happiness values of the selected children is 5.
Constraints
1 <= n == happiness.length <= 2 * 10<sup>5</sup>
1 <= happiness[i] <= 10<sup>8</sup>
1 <= k <= n
Intuition
In this problem we need find the max happiness while picking k
children. While we choose one child the happiness of every other child drops by 1. But happiness of a child can not drop below 0.
The first that comes to mind is to sort the happiness
array in descending order. Then loop over happiness array k
times each time picking the next highest happiness. This approach is called Greedy approach.
Read more about Greedy approach here:
Let's checkout the code solution for above approach
Code Solution
Python Code:
# TC: O(nlogn+k)
# SC: O(1)
class Solution:
def maximumHappinessSum(self, happiness: List[int], k: int) -> int:
# sort the happiness array in descending order
happiness.sort(reverse=True)
# initialize local variable which will store
# max happiness of kids while selection
max_happy=0
# iterate in range of 'k'
for index in range(k):
# reduce the happiness at index by index
# since happiness can't be -ve we taje minimum as zero
happiness[index]=max(happiness[index]-index,0)
# increment max_happy with current happiness
max_happy+=happiness[index]
# return max happiness of children
return max_happy
Time and Space Complexity
Time Complexity:
$$o(n\ logn\ +\ k), where\ k\ is\ the\ number\ of\ picks$$
n logn
is the complexity to sort and then we iterate k
times over the happiness array.
Space Complexity:
$$O(1)$$
As we're not allocating any extra space to solve this problem.
Conclusion
In this problem we get to learn what Greedy approach is and how it can be used.
Hope that explanation made sense and you found this blog helpful. If you've made it this far, please give it a like and drop a comment.
Happy Coding!๐