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. iOS开发 Swift开发数独游戏(五)显示游戏答案

          要点是设置好Tag就好,通过代码找到并初始化即可. 1: // 2: // ShowAnswerController.swift 3: // sudoku-v02 4: // 5: // ...

  2. 重大新闻:腾讯大杀器来了,QQ浏览器微信版推出

    今日,腾讯在推出windows桌面版的微信后,又发布了一个重量级产品:QQ浏览器微信版 我们在PC端用微信又多了一种方式,而且比windows桌面版本更加友好,更加方便. 我相信:对于我们绝大多数办公 ...

  3. Sublime Text:格式化插件HTML-CSS-JS Prettify

    Sublime Text:插件HTML-CSS-JS Prettify可以格式化HMTL/CSS/JS 1.安装Node.js 2.Sublime中ctrl+shift+p,输入ip: 3.点击Ins ...

  4. WebApi单元测试记录

    一.MessageHandler不一定是全局的,也可以只应用到指定的Router上 .定义一个handler // Pipelines HttpMessageHandler affiliateShip ...

  5. easyui datagrid加载成功之后选定并获取首行数据

    //加载成功之后,选定并获取首行数据 onLoadSuccess:function(data){ alert("grid加载成功"); var rows=$('test').dat ...

  6. 更改Windows用户文件夹(Users)默认位置到其它盘

    一.把 C盘Users文件夹里的用户数据,迁移到D盘Users文件夹中 系统环境:windows7 1.mklink命令详解 C:>mklink 创建符号链接. MKLINK [[/D] | [ ...

  7. 【前端自动化构建 grunt、gulp、webpack】

    参考资料: 用自动化构建工具增强你的工作流程!:http://www.gulpjs.com.cn/ gulp详细入门教程:http://www.ydcss.com/ JavaScript构建(编绎)系 ...

  8. asp.net权限控制的方式

    我们在使用asp.net开发Web程序的时候经常需要进行一些权限控制,如: 限制用户没有登陆就无法查看一些页面,又或者是说登陆之后如果不是管理员,或是没有响应的权限就无法进行相关的操作. 实现的方法有 ...

  9. redux-actions

    其作用都是用来简化action.reducer. 1.安装 npm install --save redux-actions // 或 yarn add redux-actions 2.使用 crea ...

  10. 在Ubuntu 12.04上配置iSCSI Target服务

      今天自己按照网上搜来的教程自己在Ubuntu 12.04上配置了iSCSI Target服务,在这里简单地做个纪录.操作系统是全新安装的Ubuntu 12.04,配置一块500 GB的SATA笔记 ...