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 ...
随机推荐
- 最简单的记录程序运行时间的方法:[记录PHP程序运行消耗时间]
比较实用的debug方法:具体参考地址已经记不清了,这里重写成类方便调用 /** * @author logonmy@126.com * @Date: 13-7-23 * @desc example ...
- ACM ICPC 2018 青岛赛区 部分金牌题题解(K,L,I,G)
目录: K Airdrop I Soldier Game L Sub-cycle Graph G Repair the Artwork ———————————————————— ps:楼主脑残有点严 ...
- GlassFish的安装与使用(Windows)
前言 Glassfish是一款由Sun公司开发的(现由甲骨文公司赞助)开源的免费的应用服务器,它既是EJB容器也是WEB容器.Glassfish支持最新版的Java EE标准. Glassfish与T ...
- LOJ#2351. 「JOI 2018 Final」毒蛇越狱
LOJ#2351. 「JOI 2018 Final」毒蛇越狱 https://loj.ac/problem/2351 分析: 首先有\(2^{|?|}\)的暴力非常好做. 观察到\(min(|1|,| ...
- git撤销各种状态下的操作
使用Git时会出现各种各样的问题,下面是几种情况下怎么反悔的操作 一,未加入缓存区,撤销文件修改 git checkout -- file 二,已加入缓存区,撤销文件提交 git reset HEAD ...
- 学习动态性能表(4)--v$sqltext&v$sqlarea
学习动态性能表 第四篇-(1)-V$SQLTEXT 2007.5.29 本视图包括Shared pool中SQL语句的完整文本,一条SQL语句可能分成多个块被保存于多个记录内. 注:V$SQLARE ...
- linux基础命令复习
1.ls 查看文件和文件夹 1).ls -a 查看文件和文件夹,包括隐藏的 2).ls -l 查看文件和文件夹详情 3).ls -lh 查看文件和文件夹详情,自动生成文件大小单位 4).ls ...
- MVC 控制器之间传值学习——session
刚接触MVC不久,写的一些代码自己都不忍心看下去.路漫漫其修远兮,宝宝还需努力!之前只用过Session做登录时用户信息的储存,今天对集合类数据做了小小的尝试:利用session在控制器之间传值,以减 ...
- 编写一个jQuery的扩展方法(插件)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- PHP函数(二)-不定参数的传递
如果要传递不定数量的参数,需要使用func_get_args()函数来传递 func_num_args()函数用来返回参数的总数 <?php function more_args(){ $arg ...