贪心算法 455

455. 分发饼干

class Solution {
    public int findContentChildren(int[] g, int[] s) {

        Arrays.sort(g); //孩子列表
        Arrays.sort(s); //饼干列表
        int start=0;  //先把饼干和孩子从小到大排好序
        for(int i=0;i<s.length&&start<g.length;i++){ //先满足胃口小的孩子 从小到大顺序来      
            if(s[i]>=g[start]){				
                start++;
            }
        }
        return start;
    }
}
class Solution {
    public int findContentChildren(int[] g, int[] s) {

        Arrays.sort(g); //孩子列表
        Arrays.sort(s); //饼干列表        
        int start=s.length-1;
        int count=0;
        for(int i=g.length-1;i>=0&&start>=0;i--){ //遍历孩子    (外加条件 饼干还有的情况下继续遍历)
            if(s[start]>=g[i]){
                start--;
                count++;
            }
        }
        return count;
    }
}



posted on   你是千堆雪我是长街7  阅读(5)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示