455. Assign Cookies

Easy

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的更多相关文章

  1. 【leetcode】455. Assign Cookies

    problem 455. Assign Cookies solution1: But, you should give each child at most one cookie. 对小朋友的满意程度 ...

  2. LeetCode:455. Assign Cookies

    package Others; import java.util.Arrays; //Question 455. Assign Cookies /* Assume you are an awesome ...

  3. 455. Assign Cookies - LeetCode

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

  4. [LeetCode] Assign Cookies 分点心

    Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...

  5. Leetcode: Assign Cookies

    Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...

  6. 12. leetcode 455.Assign Cookies

    Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...

  7. LeetCode 455. Assign Cookies (分发曲奇饼干)

    Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...

  8. 455. Assign Cookies.md

    Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...

  9. [Swift]LeetCode455. 分发饼干 | Assign Cookies

    Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...

随机推荐

  1. python--简单的文件断点续传实例

    一.程序说明 1.文件上传目标路径:home/file 2.目标文件:putfile.png 3.服务端代码:put_server.py 4.客户端代码:put_client.py 二.各部分代码 1 ...

  2. python-hashlib加密

    用于加密相关的操作,代替了md5模块和sha模块,主要提供SHA1,SHA224,SHA256,SHA512,MD5算法. 以下是算法示例: #!/usr/bin/env python # -*- c ...

  3. JavaScript基础12——运动

    运动原理 运动的原理:      让某件物品沿着某种方向随着时间的变化改变位置 setInterval(function(){ obox.style.left = obox.offsetLeft+10 ...

  4. Xamarin.Android开发

    使用 Visual Studio 生成第一个 Xamarin.Android 应用程序,并进一步了解使用 Xamarin 进行 Android 应用程序开发的基础知识.在此过程中,会介绍生成和部署 X ...

  5. 使用google autoservice 自动生成java spi 描述文件

    spi 是一种服务发现的标准,对于开发中我们通常需要编写 META-INF/services 文件夹中定义的类. google auto 中的autoservice 可以帮助我们生成对应的配置,很方便 ...

  6. 8.8poc包问题

    对于8.8的包的问题:zabbix server设备重启后 zabbix server,mariadb,zabbix agent启动不了.是因为在7代的centos中在主机重启后.自动删除了/var/ ...

  7. 套接字编程简介: IPV4套接字地址结构/ 通用套接字地址结构/ IPV6套接字地址结构/新通用套接字地址结构

    IPv4套接字地址结构通常也称为“网际套接字地址结构”,它以sockaddr_in命名,定义在<netinet/in.h>头文件中. struct in_addr { in_addr_t ...

  8. 同余and乘法逆元学习笔记

    目录 数学符号 快速幂 方法一 方法二 同余 概念 同余的性质 乘法逆元 概念: 求逆元的方法 扩展欧几里得 快速幂法\(o(n*log(n))\) 递推法\(o(n)\) sjp大佬让我写同余那就只 ...

  9. Pycharm 导入Selenium,出现错误

    问题 导入Selenium,出现红色波浪线. 解决 点击Pycharm左上角File>setting>Project Interpreter,双击PIP,搜索栏搜索Selenium 然后选 ...

  10. Xshell远程登录

    1.xshell由一台服务器a登录另一台服务器b sftp -oPort = root@ip 2.下载git /上传pull git b服务器想下载的目标文件目录 a服务器上的文件下载目录