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.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
读题错了,饼干能满足的>=人
[一句话思路]:
同向指针互相比较即可
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
分配问题,用同向指针即可
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
省空间
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
class Solution {
public int findContentChildren(int[] g, int[] s) {
//ini:sort
int i = 0;
Arrays.sort(g);
Arrays.sort(s); //for loop
for (int j = 0; j < s.length && i < g.length; j++) {
if (s[j] >= g[i]) i++;
} //return
return i;
}
}
455. Assign Cookies 满足欲望 分配饼干的更多相关文章
- 455. Assign Cookies - LeetCode
Question 455. Assign Cookies Solution 题目大意:数组g的大小表示有几个小孩,每个元素表示小孩的食量,数组s的大小表示有多少个饼干,每个元素的大小表示每个饼干的大小 ...
- LeetCode:455. Assign Cookies
package Others; import java.util.Arrays; //Question 455. Assign Cookies /* Assume you are an awesome ...
- 【leetcode】455. Assign Cookies
problem 455. Assign Cookies solution1: But, you should give each child at most one cookie. 对小朋友的满意程度 ...
- [刷题] 455 Assign Cookies
要求 贪心算法的关键:判断问题是否可以用贪心算法解决 给小朋友们分饼干,每个小朋友"贪心指数"为g(i),饼干大小值s(i) g(i):小朋友需要的饼干大小的最小值 若s(j)&g ...
- 455 Assign Cookies 分发饼干
假设你是一位很棒的家长,想要给你的孩子们一些小饼干.但是,每个孩子最多只能给一块饼干.对每个孩子 i ,都有一个胃口值 gi ,这是能让孩子们满足胃口的饼干的最小尺寸:并且每块饼干 j ,都有一个尺寸 ...
- 【LeetCode】455. Assign Cookies 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- [leetcode greedy]455. 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 ...
随机推荐
- Requst Servervariables
Request.ServerVariables("Url") 返回服务器地址 Request.ServerVariables("Path_Info") 客户端提 ...
- 用JQuery写出登录弹出框
类似百度的登录弹出框,可用jquery的fadeIn(),hide(),show(),slideDown()等动画函数实现,一下为html5 代码: <!DOCTYPE html> < ...
- opencv 学习笔记集锦
整理了我所了解的有关OpenCV的学习笔记.原理分析.使用例程等相关的博文.排序不分先后,随机整理的.如果有好的资源,也欢迎介绍和分享. 1:OpenCV学习笔记 作者:CSDN数量:55篇博文网址: ...
- iTunes文件共享
在Info.plist文件中添加UIFileSharingEnabled键,并将键值设置为YES.将您希望共享的文件放在应用程序的Documents目录.
- python 函数 hex()
hex(x)作用:hex() 函数用于将10进制整数转换成16进制整数. x-10进制整数,返回16进制整数 实例: >>>hex(255) '0xff' >>> ...
- FastAdmin 一键 CRUD 生成时方法不存在的问题分析
FastAdmin 一键 CRUD 生成时方法不存在的问题分析 有群友反馈 使用 一键 CRUD 生成时不成功. 我试了以下命令 php think crud -t test -u 1 是成功的. 再 ...
- 关于yum
1. yum的本地安装 yum install --downloadonly --downloaddir=/opt/software cd /opt/software yum localinstall ...
- Erlang基础 -- 介绍 -- Wordcount示例演示
在前两个blog中,已经说了Erlang的历史.应用场景.特点,这次主要演示一个Wordcount的示例,就是给定一个文本文件,统计这个文本文件中的单词以及该单词出现的次数. 今天和群友们讨论了一个问 ...
- CodeReview是开发中的重要一个环节,整理了一些关于jupiter for java
什么是代码评审(CodeReview)? 代码评审也称代码复查,是指通过阅读代码来检查源代码与编码标准的符合性以及代码质量的活动. Jupiter提供了代码行级别的评审批注功能,方便评审参与人了解具体 ...
- 复制书稿(book) (二分,贪心+dp)
复制书稿(book) 时间限制: 1 Sec 内存限制: 128 MB提交: 3 解决: 1[提交][状态][讨论版][命题人:quanxing] 题目描述 现在要把m本有顺序的书分给k个人复制( ...