455 Assign Cookies 分发饼干
假设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。对每个孩子 i ,都有一个胃口值 gi ,这是能让孩子们满足胃口的饼干的最小尺寸;并且每块饼干 j ,都有一个尺寸 sj 。如果 sj >= gi ,我们可以将这个饼干 j 分配给孩子 i ,这个孩子会得到满足。你的目标是尽可能满足越多数量的孩子,并输出这个最大数值。
注意:
你可以假设胃口值为正。
一个小朋友最多只能拥有一块饼干。
示例 1:
输入: [1,2,3], [1,1]
输出: 1
解释:
你有三个孩子和两块小饼干,3个孩子的胃口值分别是:1,2,3。
虽然你有两块小饼干,由于他们的尺寸都是1,你只能让胃口值是1的孩子满足。
所以你应该输出1。
示例 2:
输入: [1,2], [1,2,3]
输出: 2
解释:
你有两个孩子和三块小饼干,2个孩子的胃口值分别是1,2。
你拥有的饼干数量和尺寸都足以让所有孩子满足。
所以你应该输出2.
详见:https://leetcode.com/problems/assign-cookies/description/
C++:
方法一:
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s)
{
int res = 0, p = 0;
sort(g.begin(), g.end());
sort(s.begin(), s.end());
for (int i = 0; i < s.size(); ++i)
{
if (s[i] >= g[p])
{
++res;
++p;
if (p >= g.size())
{
break;
}
}
}
return res;
}
};
方法二:
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s)
{
int j = 0;
sort(g.begin(), g.end());
sort(s.begin(), s.end());
for (int i = 0; i < s.size() && j < g.size(); ++i)
{
if (s[i] >= g[j])
{
++j;
}
}
return j;
}
};
455 Assign Cookies 分发饼干的更多相关文章
- Leetcode455.Assign Cookies分发饼干
假设你是一位很棒的家长,想要给你的孩子们一些小饼干.但是,每个孩子最多只能给一块饼干.对每个孩子 i ,都有一个胃口值 gi ,这是能让孩子们满足胃口的饼干的最小尺寸:并且每块饼干 j ,都有一个尺寸 ...
- 455. Assign Cookies - LeetCode
Question 455. Assign Cookies Solution 题目大意:数组g的大小表示有几个小孩,每个元素表示小孩的食量,数组s的大小表示有多少个饼干,每个元素的大小表示每个饼干的大小 ...
- 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 (分发曲奇饼干)
Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...
- 455. Assign Cookies 满足欲望 分配饼干
[抄题]: Assume you are an awesome parent and want to give your children some cookies. But, you should ...
- [leetcode greedy]455. Assign Cookies
Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...
- 【LeetCode】455. Assign Cookies 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- 12. leetcode 455.Assign Cookies
Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...
随机推荐
- android <application> 开发文档翻译
由于本人英文能力实在有限,不足之初敬请谅解 本博客仅仅要没有注明"转",那么均为原创.转贴请注明本博客链接链接 <application>语法: <appl ...
- 我的gulp.js清单
var gulp = require('gulp'), cssmin = require('gulp-clean-css'), //压缩css文件 concat = require('gulp-con ...
- ubuntu hadoop伪分布式部署
环境 ubuntu hadoop2.8.1 java1.8 1.配置java1.8 2.配置ssh免密登录 3.hadoop配置 环境变量 配置hadoop环境文件hadoop-env.sh core ...
- Eclipse 变量点击高亮显示以及自己定义高亮显示颜色
1.方法一:alt+shift+o 打开/关闭,该功能 2.方法二:windows-> preferences->java->Editor->Mark Occurences ( ...
- 契约式设计 契约式编程 Design by contract
Design by contract - Wikipedia https://en.wikipedia.org/wiki/Design_by_contract What is the use of & ...
- The android gradle plugin version 2.3.0-beta2 is too old, please update to the latest version.
编译项目的时候,报如下错误: Error:(, ) A problem occurred evaluating project ':app'. > Failed to apply plugin ...
- YTU 1011: Rails
1011: Rails 时间限制: 1000 Sec 内存限制: 64 MB 提交: 16 解决: 9 题目描述 There is a famous railway station in PopP ...
- YTU 1075: Time
1075: Time 时间限制: 1 Sec 内存限制: 128 MB 提交: 7 解决: 7 [提交][状态][讨论版] 题目描述 Digital clock use 4 digits to e ...
- 深入了解以太坊虚拟机第4部分——ABI编码外部方法调用的方式
在本系列的上一篇文章中我们看到了Solidity是如何在EVM存储器中表示复杂数据结构的.但是如果无法交互,数据就是没有意义的.智能合约就是数据和外界的中间体. 在这篇文章中我们将会看到Solidit ...
- [HEOI 2012] 采花
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2743 [算法] 首先预处理nxt[]数组 , 其中 , nxt[i]表示下一个和i号 ...