LeetCode 455. Assign Cookies (C++)
题目:
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.
分析:
有两个数组,分别代表孩子需要曲奇的大小,和你所拥有曲奇的大小,求最大满足孩子的数目。
经典的贪心问题,我们可以这样想,如果一个曲奇不能满足当前这个孩子的话,那么一定不能满足比这个孩子还要大的尺寸。我们还希望满足一个孩子曲奇的尺寸尽量和他需要的接近,也就是不要浪费大尺寸的曲奇。所以我们可以先将两个数组有小到大排序。遍历曲奇数组,如果当前这个曲奇不满足第一个孩子,那么就看下一个曲奇。如果满足,则数目加一,并且孩子和曲奇的指针也同时加一。这样可以满足我们上面提到的两个要求。
程序:
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(), g.end());
sort(s.begin(), s.end());
int sum = ;
int i = ;
int j = ;
while(i < g.size() && j < s.size()){
while(j < s.size()){
if(g[i] <= s[j]){
sum++;
j++;
break;
}
j++;
}
i++;
}
return sum;
}
};
LeetCode 455. Assign Cookies (C++)的更多相关文章
- LeetCode:455. Assign Cookies
package Others; import java.util.Arrays; //Question 455. Assign Cookies /* Assume you are an awesome ...
- LeetCode 455. Assign Cookies (分发曲奇饼干)
Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...
- 12. leetcode 455.Assign Cookies
Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...
- LeetCode: 455 Assign Cookies(easy)
题目: Assume you are an awesome parent and want to give your children some cookies. But, you should gi ...
- 【leetcode】455. Assign Cookies
problem 455. Assign Cookies solution1: But, you should give each child at most one cookie. 对小朋友的满意程度 ...
- 455. Assign Cookies - LeetCode
Question 455. Assign Cookies Solution 题目大意:数组g的大小表示有几个小孩,每个元素表示小孩的食量,数组s的大小表示有多少个饼干,每个元素的大小表示每个饼干的大小 ...
- 【LeetCode】455. Assign Cookies 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- [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 ...
- [leetcode greedy]455. Assign Cookies
Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...
随机推荐
- package.xml
package.xml 也是一个catkin的package必备文件, 它是这个软件包的描述文件, 在较早的ROS版本(rosbuild编译系统)中, 这个文件叫做 manifest.xml , 用于 ...
- USB耳机声卡-音频输入/输出控制器:DP108替代兼容CM108
DP108是一款完全替代CM108的高度集成的单芯片USB音频解决方案芯片.方便的USB即插即用的兼容性,用户可以快速创建易用性,高质量和便携式USB音频产品基于高度集成的单芯片解决方案.所有重要的模 ...
- 1350: To Add Which? (优先队列+贪心 或者 数组模拟)
1350: To Add Which? Submit Page Summary Time Limit: 1 Sec Memory Limit: 128 Mb Submitt ...
- 静态库打包——.a和.framework文件
参考链接 步骤:适配所有的模拟器和真机 ——生成.a文件 <1>建一个静态库工程 <2>生成.a文件(注意添加类.h和.m文件) 同理:接入任意款真机,同上述操作,生成真机的. ...
- 前端实现文件在线预览txt,pdf,doc,xls,ppt几种格式
做法就是使用iframe标签 1.text,pdf的文件预览 <iframe class="filename" :src="文件的地址" width='1 ...
- 五、Delphi10.3通过REST单元使类和JSON数据互相转换
一.我们定义一个简单的类 TPeople = class private FName: string; FScore: Integer; FAge: TDateTime; public propert ...
- JavaWeb基础—EL表达式与JSTL标签库
EL表达式: EL 全名为Expression Language.EL主要作用 获取数据(访问对象,访问数据,遍历集合等) 执行运算 获取JavaWeb常用对象 调用Java方法(EL函数库) 给出一 ...
- c++ 指针访问数组
用指针访问一维数组 用指针访问二维数组 用指针访问三维数组 一. 用指针访问一维数组 //代码 ; ]={,}; int *p=&a; //int *p=&a[0]; printf(& ...
- 【HNOI2013】比赛
题面 题解 \(n \leq 9 \to\)爆搜 对每一场的结果进行搜索,最后进行\(\mathrm{check}\) 然后会发现没有什么分 搜索最重要的就是剪枝 接下来就列出一些剪枝 搜索时,强制每 ...
- NOIp2018 pj 滚粗记
NOIp2018 pj 滚粗记 考前 一个午觉睡完就到了考场 考中 \(T1\)水题切了 \(T2\)水题切了 \(T3\)好像是\(dp\),不会,先跳 \(T4\)像树上莫队一样,然后再欧拉序上面 ...