要求

  • 贪心算法的关键:判断问题是否可以用贪心算法解决
  • 给小朋友们分饼干,每个小朋友“贪心指数”为g(i),饼干大小值s(i)
  • g(i):小朋友需要的饼干大小的最小值
  • 若s(j)>=g(i),则将饼干j分给小朋友i,小朋友会开心
  • 给定s、g,问如何分配饼干,能让最多的小朋友开心

示例

  • g=[1,2,3], s=[1,1], 输出1
  • g=[1,2], s=[1,2,3], 输出2

思路

  • 将最大的饼干给最贪心的小朋友
  • 先对数组排序
  • 复杂度:nlogn

实现

  • 从大到小排序
  • si:指向饼干
  • gi:指向小朋友
  • res:记录有多少小朋友开心

 1 class Solution {
2 public:
3 int findContentChildren(vector<int>& g, vector<int>& s) {
4
5 sort(g.begin(), g.end(), greater<int>());
6 sort(s.begin(), s.end(), greater<int>());
7
8 int si = 0 , gi = 0 ;
9 int res = 0 ;
10 while( gi < g.size() && si < s.size() ){
11
12 if( s[si] >= g[gi] ){
13 res ++ ;
14 si ++ ;
15 gi ++ ;
16 }
17 else
18 gi ++;
19 }
20 return res;
21 }
22 };

相关

  • 392 Is Subsequence

[刷题] 455 Assign Cookies的更多相关文章

  1. LeetCode:455. Assign Cookies

    package Others; import java.util.Arrays; //Question 455. Assign Cookies /* Assume you are an awesome ...

  2. 【leetcode】455. Assign Cookies

    problem 455. Assign Cookies solution1: But, you should give each child at most one cookie. 对小朋友的满意程度 ...

  3. 455. Assign Cookies - LeetCode

    Question 455. Assign Cookies Solution 题目大意:数组g的大小表示有几个小孩,每个元素表示小孩的食量,数组s的大小表示有多少个饼干,每个元素的大小表示每个饼干的大小 ...

  4. 455. Assign Cookies 满足欲望 分配饼干

    [抄题]: Assume you are an awesome parent and want to give your children some cookies. But, you should ...

  5. 【LeetCode】455. Assign Cookies 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

  6. 12. leetcode 455.Assign Cookies

    Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...

  7. LeetCode 455. Assign Cookies (分发曲奇饼干)

    Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...

  8. 455. Assign Cookies.md

    Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...

  9. [LeetCode&Python] Problem 455. Assign Cookies

    Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...

随机推荐

  1. C++并发与多线程学习笔记--线程之间调度

    condition_variable wait() notify_one notify_all condition_variable 条件变量的实际用途: 比如有两个线程A和B,在线程A中等待一个条件 ...

  2. 翻译:《实用的Python编程》08_03_Debugging

    目录 | 上一节 (8.2 日志) | 下一节 (9 包) 8.3 调试 调试建议 假设程序崩溃了: bash % python3 blah.py Traceback (most recent cal ...

  3. Python基础(二十):面向对象“类”第三课——类成员

    知识点: 类属性与实例属性: 类方法与实例方法: 静态方法: 类属性与实例属性 类属性与实例属性的区别 属性的绑定不同 类属性与当前类相关(绑定的是当前类),与当前类创建的任何对象无关: 实例属性与当 ...

  4. Visual Studio 2015 无法加载.Net FrameWork4.6.2

    默认的VS2015是没有.Net Framework4.6.2的 需要我们去到微软官网下载对应的.NET Framework 4.6.2的安装包 安装包分两种,一种是应用级别的还一种是开发级别的,如果 ...

  5. 记一次metasploitable2内网渗透之8180端口tomcat

    扫描网段存活主机,确定内网metasploitable主机位置 nmap -T4 -sP 192.168.1.0/24 对目标主机进行扫描端口开放和系统信息 nmap -T4 -sV -Pn 192. ...

  6. 【深度学习】PyTorch CUDA环境配置及安装

    Pytorch版本介绍 torch:1.6 CUDA:10.2 cuDNN:8.1.0 安装 NVIDIA 显卡驱动程序 一般 电脑出厂/装完系统 会自动安装显卡驱动 如果有 可直接进行下一步 下载链 ...

  7. Squares UVA - 201

    A children's board game consists of a square array of dots that contains lines connecting some of th ...

  8. Ubuntu20.04安装Redis

    本文介绍了如何在Ubuntu20.04上安装Redis. 安装Redis sudo apt install redis-server 检查服务的状态 安装完成后可以通过以下命令检查服务的状态 sudo ...

  9. thinkPHP5中的与原本的字母方法用什么东西替代了?

    过去的单字母函数已完全被替换掉,如下:S=>cache,C=>config,M/D=>model,U=>url,I=>input,E=>exception,L=&g ...

  10. HTML5 表单新增元素与属性

    1 form 属性和 formaction 属性 本课时讲解在 HTML4 中,表单内的从属元素必须书写在表单内部,而在 HTML5 中,可以把他们书写在页面上任何地方,然后为该元素指定一个 form ...