题目:

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++)的更多相关文章

  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(easy)

    题目: 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. week3编程作业: Logistic Regression中一些难点的解读

    %% ============ Part : Compute Cost and Gradient ============ % In this part of the exercise, you wi ...

  2. zabbix 自定义key与参数Userparameters监控脚本输出

    1.修改agent配置文件: 通过yum安装的zabbix-agent配置文件路径为/etc/zabbix/zabbix_agentd.conf 里面定义我们自己配置文件路径:Include=/etc ...

  3. [原创]升级Gerrit的commit-msg,检查git commit时必须填写开发任务编号TaskID

    公司使用git+gerrit+jenkins进行持续集成实践,其中gerrit用来进行Code Review.另外我们自己研发了一套敏捷项目管理系统TPM(TeamPlus Management),用 ...

  4. 使用Jquery Viewer 展示图片信息

    <!DOCTYPE html><html lang="en"><head> <meta charset="utf-8" ...

  5. Echarts根据数据长度变换柱状图柱状的颜色

    //查询图表数据            function GetData() {                var qs = $("#qs").val();          ...

  6. Lucene-----信息检索技术

    1 信息检索概述 1.1 传统检索方式的缺点     • 文件检索        操作系统常见的是硬盘文件检索        文档检索:整个文档打开时已经加载到内存了;        缺点:全盘遍历, ...

  7. C++ STL 学习笔记__(6)优先级队列priority_queue基本操作

    10.2.7优先级队列priority_queue v  最大值优先级队列.最小值优先级队列 v  优先级队列适配器 STL priority_queue v  用来开发一些特殊的应用,请对stl的类 ...

  8. [Luogu4921]情侣?给我烧了![错位排列]

    题意 题意很清楚 \滑稽 分析 对于每一个询问 \(k\) ,记 \(g(x)\) 表示 \(x\) 对情侣都错开的方案总数,那么答案可以写成如下形式: \[ {ans}_k= \binom{n}{k ...

  9. c# table 怎么在前台循环展示 ViewBag

    后台 public ActionResult DoctorEvaluation()//前台页面 { HE_Department HE_dt = new HE_Department(); DataTab ...

  10. Mweb发布博客

    1.设置 + 选择红色 2     输入该输入的帐号和密码  博客网址就是你的博客文章页的地址,然后输入帐号和密码 3 API 地址在 设置-->博客设置,红色标记就是API地址 4 发布, ...