题目:

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:

  1. Input: [1,2,3], [1,1]
  2.  
  3. Output: 1
  4.  
  5. Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3.
  6. 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.
  7. You need to output 1.

Example 2:

  1. Input: [1,2], [1,2,3]
  2.  
  3. Output: 2
  4.  
  5. Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2.
  6. You have 3 cookies and their sizes are big enough to gratify all of the children,
  7. You need to output 2.

代码:

  1. class Solution {
  2. public:
  3. int findContentChildren(vector<int>& g, vector<int>& s) {
  4. int result = ;
  5. while ((!g.empty())&&(!s.empty())){
  6. auto gmax = max_element(g.begin(), g.end());
  7. auto smax = max_element(s.begin(), s.end());
  8. if (*gmax <= *smax){
  9. result++;
  10. g.erase(gmax);
  11. s.erase(smax);
  12. }
  13. else
  14. g.erase(gmax);
  15. }
  16. return result;
  17. }
  18. };

改良版:

  1. class Solution {
  2. public:
  3. int findContentChildren(vector<int>& g, vector<int>& s) {
  4. int result = ;
  5. sort(g.begin(), g.end(), greater<int>());
  6. sort(s.begin(), s.end(), greater<int>());
  7. vector<int>::iterator gtem = g.begin();
  8. vector<int>::iterator stem = s.begin();
  9.  
  10. while (gtem != g.end() && stem != s.end()){
  11. if (*gtem > *stem)
  12. gtem++;
  13. else{
  14. result++;
  15. gtem++;
  16. stem++;
  17. }
  18. }
  19. return result;
  20. }
  21. };

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. Hadoop切换namenode为active

    hadoop切换namenode为active 进入hadoop/bin目录下 ./yarn rmadmin -transitionToActive --forcemanual rm1 重新启动zkf ...

  2. 大话设计模式--策略模式 strategy -- C++实现实例

    1. 策略模式: 它定义了算法家族, 分别封装起来,使他们之间可以相互替换,此模式让算法变化, 不会影响到使用算法的客户. 用相同的方法调用不同的算法,减少各种算法类与使用算法类之间的耦合. 实例中策 ...

  3. OTSU大津法对图像二值化

    OTSU算法 (1)原理: 对于图像I(x,y),前景(即目标)和背景的分割阈值记作T,属于背景的像素个数占整幅图像的比例记为ω0,其平均灰度μ0:前景像素个数占整幅图像的比例为ω1,其平均灰度为μ1 ...

  4. python3与Redis连接操作

    Python3之redis使用   简介 redis是一个key-value存储系统,和Memcache类似,它支持存储的value类型相对更多,包括string(字符串),list(链表),set( ...

  5. Web中常用字体介绍

    1.在Web编码中,CSS默认应用的Web字体是有限的,虽然在新版本的CSS3,我们可以通过新增的@font-face属性来引入特殊的浏览器加载字体. 浏览器中展示网页文字内容时,文字字体都会按照设计 ...

  6. HashMap去重

    package util; import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import jav ...

  7. hdu--2570--迷瘴(贪心)

    #include<iostream> #include<vector> #include<algorithm> using namespace std; int m ...

  8. codeforces 707C C. Pythagorean Triples(数学)

    题目链接: C. Pythagorean Triples time limit per test 1 second memory limit per test 256 megabytes input ...

  9. 【二叉树的递归】02二叉树的最大深度【Maximum Depth of Binary Tree】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,找出他的最小的深度 ...

  10. ONVIF协议测试工具 ONVIF Device Test Tool 29 12.12 最新版

    ONVIF协议测试工具 ONVIF Device Test Tool 29 12.12 最新版 包含文档和工具,本人亲测,好用! http://download.csdn.net/detail/li_ ...