455. 分发饼干
作者:
名字是乱打的 | 来源:发表于
2025-04-17 18:08 被阅读0次
一 题目:
二 思路:
- 我们应该优先尝试用最小的饼干进行供给,这样不会浪费
- 然后排序之后我们就可以知道当前胃口最小的人了,每次尝试用最小的能量满足胃口最小的人。超棒
三 代码:
class Solution {
public int findContentChildren(int[] g, int[] s) {
//目标用最小的代价满足孩子,那么就是每次找能量值差最小的进行安排
Arrays.sort(s);
//按胃口排序
Arrays.sort(g);
//总共可以满足的孩子
int res=0;
// 尝试满足的孩子
int cIndex=0;
for (int i = 0; i < s.length; i++) {
// 如果该孩子胃口可以满足
if (s[i]>=g[cIndex]){
res++;
//下一个孩子
cIndex++;
}
// 如果所有的都满足了
if (cIndex==g.length-1){
return g.length;
}
}
return res;
}
}
本文标题:455. 分发饼干
本文链接:https://www.haomeiwen.com/subject/datubjtx.html
网友评论