题目:

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. js小的小马克

    ajax前后端配合,马克一下,方便查询 jquery开头和取得相应id的值 $(document).ready(function(){ $("#sj").click(functio ...

  2. Python快速学习-基础语法

  3. matlab画圆

    MATLAB rectangle函数1 语法说明rectangle('Position', pos)rectangle('Position', pos, 'Curvature', cur)rectan ...

  4. python第六篇:Python复制超大文件、复制二进制文件

    Python文件复制 # 写程序实现复制文件的功能 # 要求: # 1. 源文件路径和目标文件路径需要手动输入 # 2. 要考虑文件关闭的问题 # 3. 要考虑复制超大文件的问题 # 4. 要能复制二 ...

  5. delphi完美经典-第16章 Delphi数据库程序设计----使用BDE组件

    第16章 Delphi数据库程序设计----使用BDE组件 Delphi访问数据库的方式有:ADO.BDE.dbExpress.InterBase Express. 一.TDataSet组件 虽然De ...

  6. Android模拟器adb命令介绍

    在SDK的Tools文件夹下包含着Android模拟器操作的重要命令adb,adb的全称为Android Debug Bridge,就是调试桥的作用 在SDK的Tools文件夹下包含着Android模 ...

  7. 【leetcode刷题笔记】Reverse Linked List II

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...

  8. mci播放mp3

    1MIDI的播放---- 乐器数字化接口(MIDI)是由音乐界的一些大公司(包括生产电子音乐合成器的公司)制订的一项协议,后来被计算机产业所采用并成为多媒体音乐文件的标准格式.MIDI文件一般较小,对 ...

  9. sqlite表结构动态读取工具(Chole ORM框架)

    Chole ORM框架 sqlIte于嵌入式数据库读取比较有利,不需要安装office也可以进行,可以在服务器系统当中使用. 所以我开发了这款工具,然后就是为了动态的读取表结构,然后根据表结构加载所有 ...

  10. ACM学习历程—HDU 5443 The Water Problem(RMQ)(2015长春网赛1007题)

    Problem Description In Land waterless, water is a very limited resource. People always fight for the ...