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. 51nod百度之星2016练习赛

    今天看了看51nod发现有这样一个练习赛,就做了做.因为实力太弱想不出E题,各位神犇勿D. (5.26UPD:E题想粗来了) A 区间交 不难发现若干线段[li,ri]的交就是[max(li),min ...

  2. javascript 时间操作

    javascript时间函数 javascript提供了Date对象来进行时间和日期的计算.Date对象有多种构造函数: 1.dateObj=new Date() //当前时间 2.dateObj=n ...

  3. nodejs高大上的部署方式-PM2

    1.最常用的属nohup了,其实就是在后台执行进程,末尾加个&   [zhoujie@ops-dev ~]$ nohup node /home/zhoujie/ops/app.js & ...

  4. 李洪强漫谈iOS开发[C语言-049]-猜数字游戏

  5. 黑马程序员----java基础笔记中(毕向东)

    <p>------<a href="http://www.itheima.com" target="blank">Java培训.Andr ...

  6. winfrom 无边框窗体移动和阴影

    无边框窗体移动: //窗体移动API [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [ ...

  7. thinkphp关联模型的用法

    HAS_ONE(值得注意的是,这是主动关联,外键必须是被关联的表): <?php namespace Home\Model; use Think\Model\RelationModel; cla ...

  8. 段落的展开收起(substring的应用)

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  9. 注入语句详解(get注入,cookie注入,搜索型注入等)

    注意:对于普通的get注入,如果是字符型,前加'   后加 and ''=' 拆半法 ###################################### and exists (select ...

  10. zendFream 中的用到了Ajax(其中有搜索)分页

    最近在用ZendFreamwork开发一个后台,其中用到了分页,ZendFreamwork自带的分页挺好用的,可是我其中用到了Ajax的局部刷新,在加上一些搜索条件,所以分页有点无头绪了.下面我来介绍 ...