LeetCode455. Assign Cookies
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的更多相关文章
- Leetcode455.Assign Cookies分发饼干
假设你是一位很棒的家长,想要给你的孩子们一些小饼干.但是,每个孩子最多只能给一块饼干.对每个孩子 i ,都有一个胃口值 gi ,这是能让孩子们满足胃口的饼干的最小尺寸:并且每块饼干 j ,都有一个尺寸 ...
- LeetCode:455. Assign Cookies
package Others; import java.util.Arrays; //Question 455. Assign Cookies /* Assume you are an awesome ...
- 【leetcode】455. Assign Cookies
problem 455. Assign Cookies solution1: But, you should give each child at most one cookie. 对小朋友的满意程度 ...
- LeetCode_455. Assign Cookies
455. Assign Cookies Easy Assume you are an awesome parent and want to give your children some cookie ...
- 455. Assign Cookies - LeetCode
Question 455. Assign Cookies Solution 题目大意:数组g的大小表示有几个小孩,每个元素表示小孩的食量,数组s的大小表示有多少个饼干,每个元素的大小表示每个饼干的大小 ...
- [Swift]LeetCode455. 分发饼干 | Assign Cookies
Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...
- [LeetCode] Assign Cookies 分点心
Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...
- Leetcode: Assign Cookies
Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...
- 12. leetcode 455.Assign Cookies
Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...
随机推荐
- iOS 公司开发者账号申请清单
公司开发者账号申请清单: Apple ID账号申请: (有账号请提供账号密码) Apple ID: (最好是公司邮箱账号) Apple ID密码: (大于8位, 字母或数字组成, 包含 ...
- 线程协作-Semaphore并发限制
Semaphore(信号量)是用来控制同时访问特定资源的线程数量,它通过协调各个线程,以保证合理的使用公共资源.
- Debian安装VirtualBox增强工具
切换到root用户: apt-get install build-essential 或者 apt-get install gcc make apt-get install dkms apt-get ...
- Malware
电脑病毒的一种, 中文名为“马威尔”, 有多种病毒变种. 1概述 Malware这个单词来自于Malicious和Software两个单词的合成,是恶意软件的专业术语,专指那些泛滥于网络中的恶意代码. ...
- smokeping网络监控
一.smokeping介绍 我们在选择机房的时候,如何知道这个机房的网络情况,这个时候就要用到网络监控软件:smokeping 本文主要介绍如何使用smokeping来监控idc机房的网络质量情况,从 ...
- Kafka server.properties配置,集群部署
server.properties中所有配置参数说明(解释) broker.id =0每一个broker在集群中的唯一表示,要求是正数.当该服务器的IP地址发生改变时,broker.id没有变化,则不 ...
- druid.io使用技术简介: Hyperloglog
druid.io 使用Hyperloglog 估计基数 参照如下连接 http://blog.codinglabs.org/articles/algorithms-for-cardinality-es ...
- 转:facebook 开源工具集合
http://codekk.com/blogs/detail/Trinea/Facebook%20%E7%9A%84%E9%82%A3%E4%BA%9B%E5%BC%80%E6%BA%90%E9%A1 ...
- hdu1863 畅通project(判定最小生成树)
畅通project Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- varchar2 和varchar区别
1.varchar2把所有字符都占两字节处理(一般情况下),varchar只对汉字和全角等字符占两字节,数字,英文字符等都是一个字节:2.VARCHAR2把空串等同于null处理,而varchar仍按 ...