Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2811dde

Browse files
committed
Add solution #3075
1 parent 62d047b commit 2811dde

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2158,6 +2158,7 @@
21582158
3070|[Count Submatrices with Top-Left Element and Sum Less Than k](./solutions/3070-count-submatrices-with-top-left-element-and-sum-less-than-k.js)|Medium|
21592159
3071|[Minimum Operations to Write the Letter Y on a Grid](./solutions/3071-minimum-operations-to-write-the-letter-y-on-a-grid.js)|Medium|
21602160
3074|[Apple Redistribution into Boxes](./solutions/3074-apple-redistribution-into-boxes.js)|Easy|
2161+
3075|[Maximize Happiness of Selected Children](./solutions/3075-maximize-happiness-of-selected-children.js)|Medium|
21612162
3105|[Longest Strictly Increasing or Strictly Decreasing Subarray](./solutions/3105-longest-strictly-increasing-or-strictly-decreasing-subarray.js)|Easy|
21622163
3108|[Minimum Cost Walk in Weighted Graph](./solutions/3108-minimum-cost-walk-in-weighted-graph.js)|Hard|
21632164
3110|[Score of a String](./solutions/3110-score-of-a-string.js)|Easy|
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 3075. Maximize Happiness of Selected Children
3+
* https://leetcode.com/problems/maximize-happiness-of-selected-children/
4+
* Difficulty: Medium
5+
*
6+
* You are given an array happiness of length n, and a positive integer k.
7+
*
8+
* There are n children standing in a queue, where the ith child has happiness value
9+
* happiness[i]. You want to select k children from these n children in k turns.
10+
*
11+
* In each turn, when you select a child, the happiness value of all the children that
12+
* have not been selected till now decreases by 1. Note that the happiness value cannot
13+
* become negative and gets decremented only if it is positive.
14+
*
15+
* Return the maximum sum of the happiness values of the selected children you can
16+
* achieve by selecting k children.
17+
*/
18+
19+
/**
20+
* @param {number[]} happiness
21+
* @param {number} k
22+
* @return {number}
23+
*/
24+
var maximumHappinessSum = function(happiness, k) {
25+
const sortedHappiness = happiness.sort((a, b) => b - a);
26+
let result = 0;
27+
28+
for (let i = 0; i < k; i++) {
29+
const currentHappiness = sortedHappiness[i] - i;
30+
if (currentHappiness <= 0) break;
31+
result += currentHappiness;
32+
}
33+
34+
return result;
35+
};

0 commit comments

Comments
 (0)