BookShu

原题

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

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

解法

``````public int findContentChildren(int[] g, int[] s) {
int ret = 0;

Arrays.sort(g);
Arrays.sort(s);

int i = 0, j = 0;
while (i < g.length && j < s.length) {
if (g[i] <= s[j]) {
ret++;
i++;
j++;
}else if (g[i] > s[j]) {
j++;
}
}

return ret;
}
``````

``````public static void main(String[] args) {
Solution so = new Solution();
{
int []g = {1, 2, 3};
int []s = {1, 1};
assert(so.findContentChildren(g, s) == 1);
}
{
int []g = {1, 2};
int []s = {1, 2, 3};
assert(so.findContentChildren(g, s) == 2);
}
{
int []g = {};
int []s = {};
assert(so.findContentChildren(g, s) == 0);
}
}
``````

评论(0)

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

yysue
2018/07/05
25
0
LeetCode：安排工作以达到最大收益【455】

LeetCode：安排工作以达到最大收益【455】 题目描述 有一些工作：表示第个工作的难度，表示第个工作的收益。 现在我们有一些工人。是第个工人的能力，即该工人只能完成难度小于等于的工作。 ...

osc_3om50641
2018/08/19
2
0

2019/01/18
0
0
2019上半年总结——Github上那些Java面试、学习相关仓库

osc_ihb4vmpt
2019/08/04
15
0

LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 1 两数之和 给定一个整数数列，找出其中和为特定值的那两个数,你可以假设每个输入都只...

osc_f1goeecw
2018/04/08
1
0

kafka重要概念与集群重点配置详解

trayvon
12分钟前
28
0

Lighttpd 像 Ngnix 一样，是被设计运行在低内存，低 CPU 负载的设备上，它们都非常适合在树莓派上运行。 本文将介绍如何在树莓派上运行基本配置的 Lighttpd ，以及如何与 PHP-FRM 一起使用。...

12分钟前
10
0
Service Mesh 高可用在企业级生产中的实践 | 线上直播回顾

Service Mesh Virtual Meetup 是 ServiceMesher 社区和 CNCF 联合主办的线上系列直播。本期为 Service Mesh Virtual Meetup#1 ，邀请了四位来自不同公司的嘉宾，从不同角度展开了 Service Me...

SOFAStack
21分钟前
34
0
word转pdf软件有哪些？word转pdf软件怎么操作？

35分钟前
39
0
Java创建对象的过程（类实例化）

1.检查类是否被加载。 当虚拟机遇到new指令后，会先去常量池检查有没有该类的符号引用，并且检查这个类有没有进行加载、解析、初始化过，没有就先执行类加载过程。 2.为对象分配内存空间*。 ...

39分钟前
26
0