-
Problem Summary
-
Constraints
-
Intuition
-
Approach
-
Data Structures Used
-
Operations & Behavior Summary
-
Complexity
-
Multi-language Solutions
- C++
- Java
- JavaScript
- Python3
- Go
-
Step-by-step Detailed Explanation
-
Examples
-
How to use / Run locally
-
Notes & Optimizations
-
Author
I am given an array happiness[] where each value represents a child’s happiness level.
There are n children standing in a queue.
I need to select exactly k children, one by one.
Each time I select a child:
- All unselected children lose
1happiness. - Happiness never goes below
0.
My task is to maximize the total happiness of the k selected children.
1 ≤ n ≤ 2 × 10⁵1 ≤ happiness[i] ≤ 10⁸1 ≤ k ≤ n
When I read the problem, I focused on one important thing:
Every time I pick a child, all remaining children lose happiness.
So if I delay picking a child who has high happiness, I lose value.
That’s why I thought:
“I should always pick the happiest child first.”
This immediately pointed me toward a greedy approach.
-
I sort the
happinessarray in descending order. -
I pick the first
kchildren. -
When I pick the
i-thchild:- Its happiness is reduced by
i - Because
ichildren were already picked before it.
- Its happiness is reduced by
-
If the value becomes negative, I take
0. -
I keep adding these values to get the final answer.
- Array / List
- Sorting (built-in)
No extra data structures like heap or map are required.
| Operation | Description |
|---|---|
| Sort | Arrange happiness values from highest to lowest |
| Greedy Pick | Always pick the next highest value |
| Decrement | Reduce happiness by number of previous picks |
| Clamp | Ensure happiness never goes below zero |
-
Time Complexity:
O(n log n)- Sorting the array takes
O(n log n)
- Sorting the array takes
-
Space Complexity:
O(1)- Only constant extra variables are used
class Solution {
public:
long long maximumHappinessSum(vector<int>& happiness, int k) {
sort(happiness.begin(), happiness.end(), greater<int>());
long long ans = 0;
for (int i = 0; i < k; i++) {
long long curr = happiness[i] - i;
if (curr > 0) ans += curr;
}
return ans;
}
};class Solution {
public long maximumHappinessSum(int[] happiness, int k) {
Arrays.sort(happiness);
long ans = 0;
int n = happiness.length;
for (int i = 0; i < k; i++) {
long curr = happiness[n - 1 - i] - i;
if (curr > 0) ans += curr;
}
return ans;
}
}var maximumHappinessSum = function(happiness, k) {
happiness.sort((a, b) => b - a);
let ans = 0;
for (let i = 0; i < k; i++) {
let curr = happiness[i] - i;
if (curr > 0) ans += curr;
}
return ans;
};class Solution:
def maximumHappinessSum(self, happiness, k):
happiness.sort(reverse=True)
ans = 0
for i in range(k):
curr = happiness[i] - i
if curr > 0:
ans += curr
return ansfunc maximumHappinessSum(happiness []int, k int) int64 {
sort.Slice(happiness, func(i, j int) bool {
return happiness[i] > happiness[j]
})
var ans int64 = 0
for i := 0; i < k; i++ {
curr := happiness[i] - i
if curr > 0 {
ans += int64(curr)
}
}
return ans
}-
I sort the happiness array in descending order.
-
I start selecting children from index
0. -
Before selecting the
i-thchild:- Happiness already reduced
itimes.
- Happiness already reduced
-
I calculate:
effective_happiness = happiness[i] - i -
If this value is negative, I ignore it.
-
I add valid happiness values to my answer.
-
After
kselections, I return the result.
Input: happiness = [1,2,3], k = 2
Output: 4
Explanation:
Pick 3 → remaining [0,1]
Pick 1 → total = 4
Input: happiness = [1,1,1,1], k = 2
Output: 1
Input: happiness = [2,3,4,5], k = 1
Output: 5
-
Clone the repository
-
Open the file for your preferred language
-
Run using:
g++for C++javacfor Javanodefor JavaScriptpython3for Pythongo runfor Go
- No need to simulate the entire process.
- Greedy + sorting is enough.
- This solution easily handles large inputs.
- Works perfectly for competitive programming and interviews.