要求

  • 贪心算法的关键:判断问题是否可以用贪心算法解决
  • 给小朋友们分饼干,每个小朋友“贪心指数”为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. angularjs 图片上传

    <input type="file" file-model="myFile"/><div class="col-md-12" ...

  2. 【解决】Could not GET 'https://maven.google.com

    现象 解决方案 1. 由于Google被墙导致的问题 参考 配置阿里云源修改maven的源地址. 2. 由于错误配置代理导致的问题(提示400) 查看工程目录下的gradle.properties和C ...

  3. buuctf --pwn part2

    pwn难啊! 1.[OGeek2019]babyrop 先check一下文件,开启了NX 在ida中没有找到system.'/bin/sh'等相关的字符,或许需要ROP绕过(废话,题目提示了) 查看到 ...

  4. Spring学习笔记(六):MyBatis集成

    1 概述 MyBaits是一个著名的持久层框架,本文首先介绍了MyBatis的简单使用,接着与Spring进行整合,最后简单地使用了Generator去自动生成代码. 2 MyBatis简介 MyBa ...

  5. 我与Git的那些破事(下)--分支模型

    在上篇文章中,我提到了Git的基本概念和一些本人实际项目中的总结.然而,最近读了Vincent Driessen写的一篇文章,觉得他总结的太好了,站在他肩膀上忍不住将自己的理解分享出来.Vincent ...

  6. 《Effective C++》部分内容学习笔记整理

    简介 此笔记为<Effective C++>中部分内容的学习笔记. 目录 文档:<Effective C++>

  7. Mysql之读写分离架构-Atlas

    Atlas介绍 1.png ​ Atlas是由 Qihoo 360, Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目. 它是在mysql-proxy 0.8.2版本的基础上, ...

  8. python 闭包函数与装饰器

    1.什么是闭包函数 (1):什么是闭包函数: #内部函数包含对外部作用域而非全局作用域的引用, 简而言之, 闭包的特点就是内部函数引用了外部函数中的变量. 在Python中,支持将函数当做对象使用,也 ...

  9. Cobalt-Strike Office宏利用与免杀

    1.打开Cobalt-Strike生产Office宏病毒. 首先需要设置监听器.因为钓鱼的目标比较单纯,在这里就不采用域前置技术. 然后使用攻击模块,生产Office宏病毒. 设置好监听器. 生成宏病 ...

  10. Algorithm(4th) 1.5 union-find算法

    问题描述 问题输入是一对整数对,每个整数都代表一个对象,一对整数"p,q"表示 "p与q相连"(具有自反性,传递性,对称性,被归到一个等价类里),要求编写程序来 ...