Skip to content

Practice Maximize Happiness of Selected Children

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#3075
LeetCode Problem

Maximize Happiness of Selected Children

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 ith 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.

Source: Maximize Happiness of Selected Children on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
happiness = [1,2,3], k = 2
Output
4

# Example 2

Input
happiness = [1,1,1,1], k = 2
Output
1

# Example 3

Input
happiness = [2,3,4,5], k = 1
Output
5

Constraints

1 <= n == happiness.length <= 2 * 10^5
1 <= happiness[i] <= 10^8
1 <= k <= n

Related LeetCode Problems

Practice more problems similar to Maximize Happiness of Selected Children.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.