LeetCode--455.分发饼干

假设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。

对每个孩子 i,都有一个胃口值 g[i],这是能让孩子们满足胃口的饼干的最小尺寸;并且每块饼干 j,都有一个尺寸 s[j] 。如果 s[j] >= g[i],我们可以将这个饼干 j 分配给孩子 i ,这个孩子会得到满足。你的目标是尽可能满足越多数量的孩子,并输出这个最大数值。

示例 1:

输入: g = [1,2,3], s = [1,1]
输出: 1
解释: 
你有三个孩子和两块小饼干,3个孩子的胃口值分别是:1,2,3。
虽然你有两块小饼干,由于他们的尺寸都是1,你只能让胃口值是1的孩子满足。
所以你应该输出1。

示例 2:

输入: g = [1,2], s = [1,2,3]
输出: 2
解释: 
你有两个孩子和三块小饼干,2个孩子的胃口值分别是1,2。
你拥有的饼干数量和尺寸都足以让所有孩子满足。
所以你应该输出2.

提示:

  • 1 <= g.length <= 3 * 104
  • 0 <= s.length <= 3 * 104
  • 1 <= g[i], s[j] <= 231 - 1

思路:贪心算法

类别:简单

class Solution {
public:
    int findContentChildren(vector<int>& g, vector<int>& s) {

        sort(g.begin(), g.end()); //对孩子的胃口进行排序
        sort(s.begin(), s.end()); //对饼干的尺寸进行排序
        int child = 0;
        int cookie = 0;
        while(child < g.size() && cookie < s.size()){
            if(s[cookie]>=g[child]){
                child++;
            }
            cookie++;
        }
        return child;
    }

    int main(){
        vector<int> g = {1,2,3};
        vector<int> s = {1,1};
        cout << findContentChildren(g,s) <<endl;
        return 0;
    }
};

相关推荐

  1. LeetCode--455.分发饼干

    2024-02-04 07:48:02       56 阅读
  2. leetcode 455.分发饼干

    2024-02-04 07:48:02       38 阅读
  3. 455.分发饼干

    2024-02-04 07:48:02       29 阅读
  4. C++ 455. 分发饼干

    2024-02-04 07:48:02       24 阅读
  5. leetcode题解C++】455.分发饼干 and 376.摆动序列

    2024-02-04 07:48:02       57 阅读
  6. 力扣(leetcode)第455分发饼干(Python)

    2024-02-04 07:48:02       50 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-04 07:48:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-04 07:48:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-02-04 07:48:02       82 阅读
  4. Python语言-面向对象

    2024-02-04 07:48:02       91 阅读

热门阅读

  1. 区间DP,LeetCode 1690. 石子游戏 VII

    2024-02-04 07:48:02       47 阅读
  2. LeetCode每日一题 | 1690. 石子游戏 VII

    2024-02-04 07:48:02       48 阅读
  3. EmoLLM-心理健康大模型

    2024-02-04 07:48:02       46 阅读
  4. 如何在linux中安装多个版本的python

    2024-02-04 07:48:02       57 阅读
  5. 深度学习的进展

    2024-02-04 07:48:02       52 阅读
  6. gpt今日最新新闻:gpts的广泛应用

    2024-02-04 07:48:02       48 阅读
  7. Node.js-1

    Node.js-1

    2024-02-04 07:48:02      40 阅读