We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent cdea173 commit 20ed7a1Copy full SHA for 20ed7a1
3075. Maximize Happiness of Selected Children
@@ -0,0 +1,25 @@
1
+class Solution {
2
+public:
3
+ long long maximumHappinessSum(vector<int>& happiness, int k) {
4
+ // Sort the vector in ascending order
5
+ std::sort(happiness.begin(), happiness.end());
6
+
7
+ // Reverse the vector to get descending order
8
+ std::reverse(happiness.begin(), happiness.end());
9
10
+ int selected = 0;
11
+ long long happinessScore = 0; // Use long long to handle larger sums
12
13
+ // Iterate over the sorted happiness values
14
+ for (int score : happiness) {
15
+ if (selected == k) {
16
+ break; // Stop if 'k' elements have been selected
17
+ }
18
+ // Calculate and add the adjusted happiness value if it's positive
19
+ happinessScore += std::max(0, score - selected);
20
+ selected++;
21
22
23
+ return happinessScore;
24
25
+};
0 commit comments