题目链接:. - 力扣(LeetCode)
这是一道入门题,主要是为了理解从局部最优解得了全局最优解
难度不大,主要思路就是从尺寸最大的饼干开始,喂给胃口最大的孩子,这样能充分利用没一块饼干。主要要对饼干尺寸和孩子胃口进行排序。
class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int res = 0;
int j = s.length-1;
for(int i = g.length-1;i >=0;i--){
while(j >= 0){
if(s[j] >= g[i]){
res++;
j--;
}
break;
}
}
return res;
}
}