[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 ...
随机推荐
- (原)用pixi.js 实现 方块阵点击后原地自转效果
源码 各位,请教一个问题,我这个还有BUG,我是想实现,点击一下可以 停止转动,然后再点一下重新转动.而不是一直加速,有没有什么好办法? PS:问题已经解决,谢谢评论的大神@Antineutrino ...
- XLT架构图(自己 画的)
- Servlet3.0的注解
1.@WebListener注解 表示的就是我们之前的在xml中配置的 <listener> <listener-class>ListenerClass</listene ...
- php函数获取真实客户端IP地址
function getIPaddress(){ $IPaddress=''; if (isset($_SERVER)){ if (isset($_SERVER["HTTP_X_FORWAR ...
- Nginx配置文件nginx.conf中文详解(转)
######Nginx配置文件nginx.conf中文详解##### #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_ ...
- SQLServer2005+获取表结构信息
SELECT d.name TableName, a.colorder FieldNo,a.name FieldName, (case when COLUMNPROPERTY( a.id,a.name ...
- 菜鸟快飞之JavaScript函数
1.复制变量值 在说函数前,我觉得需要先说说关于变量值的复制,以便后面的理解. 复制基本类型的值: 当一个变量复制另外一个值为基本类型的变量时,两个变量可以参与任何操作而不会互相影响 var num1 ...
- querySelector系列方法相比 getElementsBy 系列方法有什么区别?
querySelector 和 querySelectorAll 相比下面这些方法有什么区别? getElementsByTagName getElementsByClassName getElem ...
- swift 学习笔记
1. 数组中取出字符串的方法: 1)let string = "\arr[0]" 2) let string = String(stringInterpolationSegment ...
- MPAndroidChart 3.0——LineChart(折线图)
显示效果 MPAndroidChart每一种图表的基本使用方式都基本相同 了解一种图表的实现 参考项目源码其他的图表也就差不多哩 在布局文件中定义 <com.github.mikephil.ch ...