题目:

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) {
int result = ;
while ((!g.empty())&&(!s.empty())){
auto gmax = max_element(g.begin(), g.end());
auto smax = max_element(s.begin(), s.end());
if (*gmax <= *smax){
result++;
g.erase(gmax);
s.erase(smax);
}
else
g.erase(gmax);
}
return result;
}
};

改良版:

 class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
int result = ;
sort(g.begin(), g.end(), greater<int>());
sort(s.begin(), s.end(), greater<int>());
vector<int>::iterator gtem = g.begin();
vector<int>::iterator stem = s.begin(); while (gtem != g.end() && stem != s.end()){
if (*gtem > *stem)
gtem++;
else{
result++;
gtem++;
stem++;
}
}
return result;
}
};

LeetCode: 455 Assign Cookies(easy)的更多相关文章

  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 (分发曲奇饼干)

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

  3. 12. leetcode 455.Assign Cookies

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

  4. LeetCode 455. Assign Cookies (C++)

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

  5. 【leetcode】455. Assign Cookies

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

  6. 455. Assign Cookies - LeetCode

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

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

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

  8. [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 ...

  9. [leetcode greedy]455. Assign Cookies

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

随机推荐

  1. LINQ 学习路程 -- 查询操作 Deferred Execution of LINQ Query 延迟执行

    延迟执行是指一个表达式的值延迟获取,知道它的值真正用到. 当你用foreach循环时,表达式才真正的执行. 延迟执行有个最重要的好处:它总是给你最新的数据 实现延迟运行 你可以使用yield关键字实现 ...

  2. JavaWeb -- Cookie应用实例 -- 购物历史记录

    1. 页面一:主页面                                         页面二: 详细显示页面   Demo2 负责页面一, 显示商品清单和历史记录 Demo3负责页面二 ...

  3. linux新增用户和删除用户

    新增用户 新增用户命令:useradd 参数: 参数 说明 -u 指定UID,也就是自定义UID -g 知道GID,也就是初始化用户组,/etc/passwd文件中的第四个字段. -G 后面接用户组的 ...

  4. BZOJ 2243 [SDOI2011]染色:树剖【维护路径上颜色段】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2243 题意: 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径 ...

  5. R数据类型

    2.2.1 向量向量是用于存储数值型.字符型或逻辑型数据的一维数组.执行组合功能的函数c()可用来创建向量.各类向量如下例所示: a <-c(1, 2, 5, 3, 6, -2, 4) b &l ...

  6. java:eclipse安装tomcat插件及配置tomcat

    我们都知道myeclipse自带tomat图标,而eclipse却没有,若想eclipse实现带有tomcat图标可以下载tomcat插件 tomcat插件下载地址:http://www.eclips ...

  7. 二 Django框架,urls.py模块,views.py模块,路由映射与路由分发以及逻辑处理——url控制器

    Django框架,urls.py模块,views.py模块,路由映射与路由分发以及逻辑处理——url控制器 这一节主讲url控制器 一.urls.py模块 这个模块是配置路由映射的模块,当用户访问一个 ...

  8. Filter/replace - VBA

    Auto filter: ActiveSheet.Range("A:F").AutoFilter Field:=3, Criteria1:="*Agent*" ...

  9. Linux-awk command

    简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再 ...

  10. Android repo 学习参考

    /*************************************************************************** * Android repo 学习参考 * 说 ...