|
| 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 | + |