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. TabHost

    (一) 知识点:id使用系统自带 1.效果图: 2.布局 activity_main.xml <?xml version="1.0" encoding="utf-8 ...

  2. HashMap源码-使用说明部分

    /* * Implementation notes. * 使用说明 * * This map usually acts as a binned (bucketed) hash table, but * ...

  3. 使用.reg文件删除暴风影视库图标和注册信息

    暴风播放器安装后会自动安装一个叫暴风影音库的软件,在你的电脑资源管理器中增加了一个“暴风影视库”的图标.看着很烦,删起来还比较麻烦,于是搜索了相关资源,自己写了个注册表处理文件,方便大家一键删除之. ...

  4. unity backbuffer

    拿unity backbuffer的方法 var  backbuffer = new RenderTargetIdentifier(BuiltinRenderTextureType.None); 这个 ...

  5. JavaScript里面向对象的继承:不使用构造函数实现"继承"

    一.什么是"非构造函数"的继承? //比如,现在有一个对象,叫做"中国人". var Chinese = { nation:'中国' }; //还有一个对象,叫 ...

  6. 为甚么要将某个方法声明为final呢?

    他可以防止其他人覆盖该方法.但更重要的一点或许是:这样做可以有效的"关闭"动态绑定,或者说, 告诉编译器不需要对其进行丰台绑定.这样,编译器就可以为final方法调用生成更有效的代 ...

  7. IStorage

    IStorage 接口支持结构化存储对象的创建和管理. 结构化存储允许分层存储在单个文件的信息,和通常被称为“文件系统内文件”. 元素的结构化存储对象存储和小溪. 存储类似于目录,和流类似于文件. 在 ...

  8. UVALive 3971 Assemble(模拟 + 二分)

    UVALive 3971 题意:有b块钱.想要组装一台电脑,给出n个配件的种类,名字,价格,品质因子.若各种类配件各买一个,总价格<=b,求最差品质配件的最大品质因子. 思路: 求最大的最小值一 ...

  9. 如何使用FlashgameMaster修改游戏

    1 打开任意一款游戏,注意最好是AS2.0版的,我们以黄金矿工为例. 2 使用方法: 点击编辑按钮的打开编辑面板出现编辑面板后在对话框中输入要查找的数值,点击查找按钮,会列表出所以等于该值的变量. 如 ...

  10. SSH框架优势

    SSH框架优势 1.    典型的三层构架体现MVC(模型Model,视图View和控制)思想,可以让开发人员减轻重新建立解决复杂问题方案的负担和精力.便于敏捷开发出新的需求,降低开发时间成本. 2. ...