Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbbcc2d7

Browse files
author
zongyanqi
committed
add Easy_455_Assign_Cookies
1 parent86fa4fe commitbbcc2d7

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+
varfindContentChildren=function(g,s){
26+
27+
varret=0;
28+
g.sort((a,b)=>a-b);
29+
s.sort((a,b)=>a-b);
30+
vargIndex=0;
31+
varsIndex=0;
32+
33+
while(gIndex<g.length&&sIndex<s.length){
34+
vargi=g[gIndex];
35+
varsi=s[sIndex];
36+
if(gi<=si){
37+
ret+=1;
38+
gIndex++;
39+
sIndex++;
40+
}elseif(gi>si){
41+
sIndex++;
42+
}else{
43+
break;
44+
}
45+
}
46+
47+
returnret;
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)

[8]ページ先頭

©2009-2025 Movatter.jp