Skip to content

Commit 8b54638

Browse files
committed
Add solution #455
1 parent 1e3e69b commit 8b54638

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@
363363
452|[Minimum Number of Arrows to Burst Balloons](./0452-minimum-number-of-arrows-to-burst-balloons.js)|Medium|
364364
453|[Minimum Moves to Equal Array Elements](./0453-minimum-moves-to-equal-array-elements.js)|Medium|
365365
454|[4Sum II](./0454-4sum-ii.js)|Medium|
366+
455|[Assign Cookies](./0455-assign-cookies.js)|Easy|
366367
456|[132 Pattern](./0456-132-pattern.js)|Medium|
367368
459|[Repeated Substring Pattern](./0459-repeated-substring-pattern.js)|Easy|
368369
461|[Hamming Distance](./0461-hamming-distance.js)|Easy|

solutions/0455-assign-cookies.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* 455. Assign Cookies
3+
* https://leetcode.com/problems/assign-cookies/
4+
* Difficulty: Easy
5+
*
6+
* Assume you are an awesome parent and want to give your children some cookies. But, you
7+
* should give each child at most one cookie.
8+
*
9+
* Each child i has a greed factor g[i], which is the minimum size of a cookie that the
10+
* child will be content with; and each cookie j has a size s[j]. If s[j] >= g[i], we can
11+
* assign the cookie j to the child i, and the child i will be content. Your goal is to
12+
* maximize the number of your content children and output the maximum number.
13+
*/
14+
15+
/**
16+
* @param {number[]} g
17+
* @param {number[]} s
18+
* @return {number}
19+
*/
20+
var findContentChildren = function(g, s) {
21+
g.sort((a, b) => a - b);
22+
s.sort((a, b) => a - b);
23+
24+
let result = 0;
25+
for (let j = 0; result < g.length && j < s.length; j++) {
26+
if (s[j] >= g[result]) {
27+
result++;
28+
}
29+
}
30+
31+
return result;
32+
};

0 commit comments

Comments
 (0)