Description

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 numb


er.

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.

这个题目简单的思路就是:

1、首先把两个数组排序

2、如果当前饼干大小小于满足感,指针前移,否则两个指针都前移。

3、最后返回指向孩子指针的指针位置

 int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(),g.end());
sort(s.begin(),s.end());
int i=g.size()-1, j=s.size()-1,count = 0;
while(i>=0 && j>=0)
{
if(g[i]>s[j]) i--;
else if(g[i--]<=s[j--]) count++;
}
return count;
}

LeetCode455. Assign Cookies的更多相关文章

  1. Leetcode455.Assign Cookies分发饼干

    假设你是一位很棒的家长,想要给你的孩子们一些小饼干.但是,每个孩子最多只能给一块饼干.对每个孩子 i ,都有一个胃口值 gi ,这是能让孩子们满足胃口的饼干的最小尺寸:并且每块饼干 j ,都有一个尺寸 ...

  2. LeetCode:455. Assign Cookies

    package Others; import java.util.Arrays; //Question 455. Assign Cookies /* Assume you are an awesome ...

  3. 【leetcode】455. Assign Cookies

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

  4. LeetCode_455. Assign Cookies

    455. Assign Cookies Easy Assume you are an awesome parent and want to give your children some cookie ...

  5. 455. Assign Cookies - LeetCode

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

  6. [Swift]LeetCode455. 分发饼干 | Assign Cookies

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

  7. [LeetCode] Assign Cookies 分点心

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

  8. Leetcode: Assign Cookies

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

  9. 12. leetcode 455.Assign Cookies

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

随机推荐

  1. net core 踩坑

    1.压缩静态文件,要nuget  BuildBundlerMinifier 2.DI 自动注入 添加了两个类,才能完成 3.所有静态文件必须放在wwwroot下,不然访问不到 4.ajax 提交Typ ...

  2. C#基础之 派生类

    1: 当创建派生类的实例时,会自动调用基类的默认构造函数 namespace parentTest { public class Reader { public Reader() { Console. ...

  3. easyui textbox获取焦点事件

    $('#textboxid').textbox().next('span').find('input').focus(); $('#id').textbox('textbox').focus();

  4. Mac下Git的安装和卸载

    1.安装最新版本:https://git-scm.com/download/mac,下载pkg进行安装 2.卸载:运行/usr/local/git/uninstall.sh

  5. Less使用说明

    使用koala编译 Koala 是一款由国人开发的开源预处理语言图形编译工具,目前已支持 Less.Sass.Compass 与CoffeeScript. 目前支持以下系统:Windows,Mac, ...

  6. Yii添加验证码

    添加带验证码的登陆: 1.先在模型modules下的LoginForm.php定义一个存储验证码的变量:public $verfyCode: 2.然后在rules()方法里定义:array('veri ...

  7. 【DP】【单调队列】【NOI2005】瑰丽华尔兹

    340. [NOI2005] 瑰丽华尔兹 ★★★ 输入文件:adv1900.in 输出文件:adv1900.out 简单对照 时间限制:1 s 内存限制:128 MB [任务描写叙述] 你跳过华尔兹吗 ...

  8. B7:访问者模式 Visitor

    表示一个作用于某对象结构中各元素的操作.它使你可以在不改变各元素类的前提下定义作用于这些元素的新操作. 适用场景:1.适用于元素类数据结构相对稳定(类的方法固定,但属性可以变化,如果方法变化很大,就无 ...

  9. 应用程序池和应用程序域的区别(Difference between application pool and application domain)

    来自StackOverFlow:  http://stackoverflow.com/questions/8486335/difference-between-an-application-domai ...

  10. 【Python3 爬虫】15_Fiddler抓包分析

    我们要抓取一些网页源码看不到的信息,例如:淘宝的评论等 我们可以使用工具Fiddler进行抓取 软件下载地址:https://pan.baidu.com/s/1nPKPwrdfXM62LlTZsoiD ...