Assume you are a awesome parent and want to give your cookies. But, you should give the each child at most one cookie. Each child I has a greed factor gi, which are the minimum size a cookie that's the child'll 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 I'll be content. Your goal is to maximize the number of Your content children and output the maximum number.
Note:
Your may assume the greed factor is always positive.
You are cannot assign more than one cookie to one child.
Example 1:
Input: [1,2,3], [1,1]
output:1
explanation:you have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3.
And even though you have 2 cookies, since their size are both 1, you could only make the child whose greed factor are 1 cont Ent.
You are need to output 1.
Example 2:
Input: [1,2], [1,2,3]
output:2
explanation:you have 2 children and 3 cookies. The greed factors of 2 children are 1, 2.
You are have 3 cookies and their sizes are big enough to gratify all of the children, you are
need to output 2.
public class Solution {
//give everyone as much as possible to accept the smallest cake on the good, direct sort, two arrays from scratch on the good public
int Findcontentchildren (int[] g, int[] s) {
Arrays.sort (g);
Arrays.sort (s);
int res = 0;
int i = 0;
int j = 0;
while (I < g.length && J < s.length) {
if (G[i]<=s[j]) {
i++;
j + +;
res++;
} else{
j + +;
}
}
return res;
}