Skip to content

Commit bbcc2d7

Browse files
author
zongyanqi
committed
add Easy_455_Assign_Cookies
1 parent 86fa4fe commit bbcc2d7

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

Easy_455_Assign_Cookies.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a cookie that the child will be content with; and each cookie j has a size sj. If sj >= gi, we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number.
3+
4+
Note:
5+
You may assume the greed factor is always positive.
6+
You cannot assign more than one cookie to one child.
7+
8+
Example 1:
9+
10+
Input: [1,2,3], [1,1]
11+
12+
Output: 1
13+
14+
Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3.
15+
And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content.
16+
You need to output 1.
17+
18+
*/
19+
20+
/**
21+
* @param {number[]} g
22+
* @param {number[]} s
23+
* @return {number}
24+
*/
25+
var findContentChildren = function (g, s) {
26+
27+
var ret = 0;
28+
g.sort((a, b) => a - b);
29+
s.sort((a, b) => a - b);
30+
var gIndex = 0;
31+
var sIndex = 0;
32+
33+
while (gIndex < g.length && sIndex < s.length) {
34+
var gi = g[gIndex];
35+
var si = s[sIndex];
36+
if (gi <= si) {
37+
ret += 1;
38+
gIndex++;
39+
sIndex++;
40+
} else if (gi > si) {
41+
sIndex++;
42+
} else {
43+
break;
44+
}
45+
}
46+
47+
return ret;
48+
49+
};
50+
51+
console.log(findContentChildren([1, 2, 3], [1, 1]));
52+
console.log(findContentChildren([1,2], [1,2,3]));
53+
54+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy