LeetCode_455. Assign Cookies
455. Assign Cookies
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.
package leetcode.easy;
public class AssignCookies {
public int findContentChildren(int[] g, int[] s) {
java.util.Arrays.sort(g);
java.util.Arrays.sort(s);
int i = 0;
for (int j = 0; i < g.length && j < s.length; j++) {
if (g[i] <= s[j]) {
i++;
}
}
return i;
}
@org.junit.Test
public void test() {
int[] g1 = { 1, 2, 3 };
int[] s1 = { 1, 1 };
int[] g2 = { 1, 2 };
int[] s2 = { 1, 2, 3 };
System.out.println(findContentChildren(g1, s1));
System.out.println(findContentChildren(g2, s2));
}
}
LeetCode_455. Assign Cookies的更多相关文章
- 【leetcode】455. Assign Cookies
problem 455. Assign Cookies solution1: But, you should give each child at most one cookie. 对小朋友的满意程度 ...
- LeetCode:455. Assign Cookies
package Others; import java.util.Arrays; //Question 455. Assign Cookies /* Assume you are an awesome ...
- 455. Assign Cookies - LeetCode
Question 455. Assign Cookies Solution 题目大意:数组g的大小表示有几个小孩,每个元素表示小孩的食量,数组s的大小表示有多少个饼干,每个元素的大小表示每个饼干的大小 ...
- [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 ...
- 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.md
Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...
- [Swift]LeetCode455. 分发饼干 | Assign Cookies
Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...
随机推荐
- php中的设计模式---工厂模式及单例模式
这两个练习放在一起处理. 在python中,这些模式都有的. 要记得三大类模式:创建型,结构型,行为型. NotFoundException.php <?php namespace Bookst ...
- Linux的sz和rz命令
工作中需要在Linux和Windows之间传输文件时,一般使用winscp或者ftp工具来完成,最近才知道有sz和rz这两个命令,方便好用. sz 下载 从Linux下载文件到本机 , 在Linux ...
- Solr的集群搭建(索引库)
Solr的集群的搭建 Solr集群原理 SolrCloud概念以及结构 概念: SolrCloud(Solr云)是Solr提供的分布式搜索方案,当你需要大规模,容错,分布式索引和检索能力时使用Solr ...
- HDU - 6643: Ridiculous Netizens(点分治+依赖背包+空间优化)
题意:给定带点权的树,问多少个连通块,其乘积<=M; N<=2000,M<1e6; 思路:连通块-->分治: 由于普通的树DP在合并的时候复杂度会高一个M,所以用依赖背包来做. ...
- bytes与网络通信
字符串是人机交互的符号: bytes是最接近计算机本身的信息表示方法. 网络通信是计算机与计算机之间的通信. 所有的通信信息,必须转化为bytes流的方式在计算机间传递. bytes与数据类型无关,与 ...
- linux 查看硬盘使用情况
在windows系统中,我们可以很容易的查看磁盘的使用情况,在linux系统中,我们可以使用命令来查看磁盘使用情况. 1.df命令 作用:用来查看硬盘的挂载点,以及对应的硬盘容量信息.包括硬盘的总大小 ...
- 第2章 Spring中的Bean
2.1 Bean的配置 Bean本质是Java中的类.Spring可以被看做一个大型工厂,这个工厂的作用就是生产和管理Spring容器zho中的Bean.想在项目中使用这个工厂,就需要对Spring的 ...
- MySQL中SUM和COUNT的区别
COUNT:是对记录进行汇总,即计数 SUM:是对符合条件的数值列字段进行求和 原表数据如下: 1,当在where子句中使用Price>25时, COUNT函数返回的是符合条件的记录,SUM函数 ...
- Hibernate的批量查询——原生sql查询
1.查询所有的学生信息: (1)查询结果中,一条信息放入到一个数组中,从list集合中取出数组,并对数组进行遍历. public class GeneratorTest { public static ...
- dolt 基于git协议的数据管理工具
dolt 基于git 协议提供了多版本,分支特性的数据管理工具,使用简单,同时也提供了类似github 的一个云服务 安装 下载地址 https://github.com/liquidata-inc/ ...