Assume you are an awesomeparent and want to give your children some cookies. But, you should give eachchild at most one cookie. Each child i has a greed factor gi, which is the minimum size of a cookie that the child will becontent with; and each cookie j has a size sj. If sj >= gi, we can assign the cookiej to the child i, and the child i will be content. Your goal is to maximize thenumber of your content children and output the maximum number.
Note:
You may assume the greed factor is always positive.
You cannot assign more than one cookie to one child.
Example1:
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.
Andeven though you have 2 cookies, since their size is both 1, you could only makethe child whose greed factor is 1 content.
Youneed to output 1.
Example2: 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.
Youhave 3 cookies and their sizes are big enough to gratify all of the children,
You need to output 2.
谷歌翻译: 假设你是一个很好的父母,并想给你的孩子一些饼干。但是,你应该给每个孩子最多一个饼干。每个孩子i都有一个贪心因子gi,这是孩子将要拥有的饼干的最小大小; 并且每个饼干j具有大小sj。如果sj> = gi,我们可以将cookie j分配给子i,而子i将是内容。您的目标是最大化满足您孩子的数量,并输出最大数量。
注意:
你可以假定贪心因素总是正的。您不能向一个孩子分配多个饼干。
实例1:输入:[1,2,3],[1,1] 输出:1
说明:您有3个孩子和2个饼干。 3个孩子的贪婪因素是1,2,3。即使你有2个饼干,因为他们的大小都是1,你只能让孩子的贪婪因素是1的内容。您需要输出1。
实例2: 输入:[1,2],[1,2,3] 输出:2
说明:您有2个孩子和3个饼干。 2个孩子的贪婪因素是1,2。你有3个饼干,他们的大小足够大,以满足所有的孩子,你需要输出2。
这题我看英文题目也看了半天,最后用谷歌翻译才出来。题目真心不难,只要把题目弄懂就行。建议多看几次题目和实例。简单说就是,g数组值每个孩子想要的饼干数,s数组是饼干的数目,一个孩子最多一份。最后输出能够满足多少个孩子。代码如下:
public class Solution {
public int findContentChildren(int[] g,int[] s) {
//先将两个数组排序
Arrays.sort(g);
Arrays.sort(s);
int i=0,j=0;
int count=0;
while(i<s.length&&j<g.length){
if(s[i]>=g[j]){
count++;
j++;
}
i++;
}
return count;
}
}