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.

Solution 1: Greedy, Time:O(nlogn)

Just assign the cookies starting from the child with less greediness to maximize the number of happy children .

 public class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
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;
}
}

Solution 2: Greedy + TreeMap,   Time: O(nlogm), n, m are the size of two arrays respectively

 public class Solution {
public int findContentChildren(int[] g, int[] s) {
TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
int res = 0;
for (int size : s) {
map.put(size, map.getOrDefault(size, 0)+1);
} for (int greed : g) {
if (map.ceilingKey(greed) != null) {
res++;
int value = map.ceilingKey(greed);
map.put(value, map.get(value)-1);
if (map.get(value) == 0) map.remove(value);
}
}
return res;
}
}

Leetcode: Assign Cookies的更多相关文章

  1. [LeetCode] Assign Cookies 分点心

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

  2. LeetCode:455. Assign Cookies

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

  3. 【leetcode】455. Assign Cookies

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

  4. 455. Assign Cookies - LeetCode

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

  5. LeetCode_455. Assign Cookies

    455. Assign Cookies Easy Assume you are an awesome parent and want to give your children some cookie ...

  6. 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 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

  8. 12. leetcode 455.Assign Cookies

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

  9. [LeetCode&Python] Problem 455. Assign Cookies

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

随机推荐

  1. iOS 开发小结

    一,经历 1> 在编写以前有过的类似的新功能时,如果以前的开发人员没有写明明确的注释和开发需求,一定要仔细阅读所有代码,每一句代码都有它存在的意义. 2> 例如,只以为是[self.ful ...

  2. get,post 区别,HTTP通信

    GET & POST GET      1.GET 的本质是"得"      2.从服务器拿数据,效率更高 3.从数学的角度来讲,GET 的结果是"幂等" ...

  3. PHP zendframework phpunit 深入

    安装包管理 curl -sS https://getcomposer.org/installer | /usr/local/php/bin/php 将证书安装到 ~$ mkdir ~/tools/ht ...

  4. Nginx做NodeJS应用负载均衡配置实例

    这篇文章主要介绍了Nginx做NodeJS应用负载均衡配置实例,本文直接给出配置实例,需要的朋友可以参考下. 负载均衡可以把用户的请求分摊到多个服务器上进行处理,从而实现了对海量用户的访问支持.负载均 ...

  5. Hibernate框架配置

     API package com.hanqi.test; import static org.junit.Assert.*; import org.hibernate.SessionFactory; ...

  6. MySQL数据库基本指令(全)

    数据库基本类型 CHAR 固定长度字符串 char(10) 存两位 占10位 查询有优势VARCHAR 可变长度字符串 varchar(10) 存两位 占两位 存储有优势 枚举类型 只能取一个set类 ...

  7. CWnd与HWND的简单辨析

    今天在写一个小的网络应用,需要用到HWND类型的一个参数.而程序中有的“窗口操作句柄”只有一个CWnd类型的指针.这俩不都是“窗口句柄”么?而且反正都是地址直接转换使用如何?结果出现了调用失效的情况. ...

  8. winform把图片存储到数据库

    1.先在Form中放一个PictureBox控件,再放三个按钮. 2.双击打开按钮,在里面写如下代码: OpenFileDialog open1 = new OpenFileDialog(); Dia ...

  9. linux笔记十----虚拟机网络配置

    首先,参考了博客http://blog.csdn.net/qianggezhishen/article/details/45841723,可以学会怎样确定界面类型

  10. linux应用于发展(下)

    X windows的特点 1.独立于操作系统. 2.网络特性. 3.源码开源. Unix图形环境主要还是CDE linux主要还是在网络应用和嵌入式上使用较多. 娱乐办公什么的去windows吧. 网 ...