美文网首页
贪心一:分发饼干

贪心一:分发饼干

作者: 程一刀 | 来源:发表于2021-06-01 09:37 被阅读0次

    题目地址: https://leetcode-cn.com/problems/assign-cookies/

    题目描述: 假设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。
    对每个孩子 i,都有一个胃口值 g[i],这是能让孩子们满足胃口的饼干的最小尺寸;并且每块饼干 j,都有一个尺寸 s[j] 。如果 s[j] >= g[i],我们可以将这个饼干 j 分配给孩子 i ,这个孩子会得到满足。你的目标是尽可能满足越多数量的孩子,并输出这个最大数值。

    参考代码:

    class Solution {
    public:
        int findContentChildren(vector<int>& g, vector<int>& s) {
            sort(g.begin(),g.end());
            sort(s.begin(), s.end());
            int index = s.size() - 1;
            int number = 0;
            for (int i = g.size() - 1;i>=0;i--) {// 孩子: 做循环
                if (index >= 0 &&s[index] >= g[i] ) { // 够吃
                    number ++;
                    index --;
                } // 不够吃,找小饭量的孩子
            }
            return number;
    
        }
    };
    

    参考链接: https://github.com/youngyangyang04/leetcode-master/blob/master/problems/0455.%E5%88%86%E5%8F%91%E9%A5%BC%E5%B9%B2.md

    相关文章

      网友评论

          本文标题:贪心一:分发饼干

          本文链接:https://www.haomeiwen.com/subject/vacwsltx.html