[LeetCode] Assign Cookies 分点心
Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a cookie that the child will be content with; and each cookie j has a size sj. If sj >= gi, we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number.
Note:
You may assume the greed factor is always positive.
You cannot assign more than one cookie to one child.
Example 1:
- Input: [1,2,3], [1,1]
- Output: 1
- Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3.
- And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content.
- You need to output 1.
Example 2:
- Input: [1,2], [1,2,3]
- Output: 2
- Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2.
- You have 3 cookies and their sizes are big enough to gratify all of the children,
- You need to output 2.
这道题给了我们一堆cookie,每个cookie的大小不同,还有一堆小朋友,每个小朋友的胃口也不同的,问我们当前的cookie最多能满足几个小朋友。这是典型的利用贪婪算法的题目,我们可以首先对两个数组进行排序,让小的在前面。然后我们先拿最小的cookie给胃口最小的小朋友,看能否满足,能的话,我们结果res自加1,然后再拿下一个cookie去满足下一位小朋友;如果当前cookie不能满足当前小朋友,那么我们就用下一块稍大一点的cookie去尝试满足当前的小朋友。当cookie发完了或者小朋友没有了我们停止遍历,参见代码如下:
解法一:
- class Solution {
- public:
- int findContentChildren(vector<int>& g, vector<int>& s) {
- int res = , p = ;
- sort(g.begin(), g.end());
- sort(s.begin(), s.end());
- for (int i = ; i < s.size(); ++i) {
- if (s[i] >= g[p]) {
- ++res;
- ++p;
- if (p >= g.size()) break;
- }
- }
- return res;
- }
- };
我们可以对上述代码进行精简,我们用变量j既可以表示小朋友数组的坐标,同时又可以表示已满足的小朋友的个数,因为只有满足了当前的小朋友,才会去满足下一个胃口较大的小朋友,参见代码如下:
解法二:
- class Solution {
- public:
- int findContentChildren(vector<int>& g, vector<int>& s) {
- int j = ;
- sort(g.begin(), g.end());
- sort(s.begin(), s.end());
- for (int i = ; i < s.size() && j < g.size(); ++i) {
- if (s[i] >= g[j]) ++j;
- }
- return j;
- }
- };
参考资料:
https://discuss.leetcode.com/topic/67676/simple-greedy-java-solution
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Assign Cookies 分点心的更多相关文章
- Leetcode: Assign Cookies
Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...
- 455. Assign Cookies - LeetCode
Question 455. Assign Cookies Solution 题目大意:数组g的大小表示有几个小孩,每个元素表示小孩的食量,数组s的大小表示有多少个饼干,每个元素的大小表示每个饼干的大小 ...
- LeetCode:455. Assign Cookies
package Others; import java.util.Arrays; //Question 455. Assign Cookies /* Assume you are an awesome ...
- 【leetcode】455. Assign Cookies
problem 455. Assign Cookies solution1: But, you should give each child at most one cookie. 对小朋友的满意程度 ...
- LeetCode_455. Assign Cookies
455. Assign Cookies Easy Assume you are an awesome parent and want to give your children some cookie ...
- pandas 新增数据列(直接赋值、apply,assign、分条件赋值)
# pandas新增数据列(直接赋值.apply.assign.分条件赋值) # pandas在进行数据分析时,经常需要按照一定条件创建新的数据列,然后进行进一步分析 # 1 直接赋值 # 2 df. ...
- 【LeetCode】455. Assign Cookies 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- LeetCode 455. Assign Cookies (分发曲奇饼干)
Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...
- [leetcode greedy]455. Assign Cookies
Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...
随机推荐
- 使用MATLAB对图像处理的几种方法(下)
试验报告 一.试验原理: 图像点处理是图像处理系列的基础,主要用于让我们熟悉Matlab图像处理的编程环境.灰度线性变换和灰度拉伸是对像素灰度值的变换操作,直方图是对像素灰度值的统计,直方图均衡是对 ...
- 利用js取到下拉框中选择的值
现在的需求是:下拉框中要是选择加盟商让其继续选择学校,要是选择平台管理员则不需要选择学校.隐藏选择下拉列表. 选择枚举值: /// <summary> /// 平台角色 /// </ ...
- 【译】Asp.net mvc 使用ITextSharp PDF to HTML (解决img标签问题)
前言:因项目需求,需要将HTML代码转成PDF.大致上已经实现了,可以是发现使用ITextSharp(我现在的版本是5.5.9)的时候,img标签中的src只能跟绝对路径. 在百度上找了一个上午,有一 ...
- Yii2.X 多语言-类图
- 【Tips】史上最全H1B问题合辑——保持H1B身份终级篇
[Tips]史上最全H1B问题合辑——保持H1B身份终级篇 2015-04-10留学小助手留学小助手 留学小助手 微信号 liuxue_xiaozhushou 功能介绍 提供最真实全面的留学干货,帮您 ...
- PHP多图片上传实例demo
upload.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...
- java中动态代理的实现
动态代理的实现 使用的模式:代理模式. 代理模式的作用是:为其他对象提供一种代理以控制对这个对象的访问.类似租房的中介. 两种动态代理: (1)jdk动态代理,jdk动态代理是由Java内部的反射机制 ...
- Springboot框架
本片文章主要分享一下,Springboot框架为什么那么受欢迎以及如何搭建一个Springboot框架. 我们先了解一下Springboot是个什么东西,它是干什么用的.我是刚开始接触,查了很多资料, ...
- 课堂Java小程序(加减乘除与验证码)
一.编写一个程序,用户输入两个数,求出其加减乘除,并用消息框 显示计算结果. 1.设计思想:从键盘输入两个数字和运算符,然后计算.将输入的数字及运算符由字符型转换为整型,再用if判断输入的运算符,根据 ...
- window下的各种宽高度小结
详细的请打开这里看console.log window.innerWidth: 文档显示区(body)的宽度window.innerHeight 文档显示区(body)的高度window.outr ...