【LeetCode】455. Assign Cookies 解题报告(Java & Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
[LeetCode]
题目地址:https://leetcode.com/problems/assign-cookies/
- Difficulty: 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 jto 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.
题目大意
我们需要把饼干分给一群小孩,每个小孩最多只有一个饼干。每个小孩有自己的欲望,每个饼干有自己的大小,给小孩分饼干的时候最少也要给他欲望大小的饼干。求能让多少个小孩满意。
解题方法
明显的贪心算法,尽可能给小孩满足他欲望的最小饼干。
Java解法
这个题目简单的思路就是:
1、首先把两个数组排序
2、如果当前满足感小于等于饼干,两个指针都后移,否则,只有满足感后移,然后再和当期前的满足感比较
3、最后返回指向孩子满足感指针的指针位置
public class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int i=0, j=0;
while(i<g.length && j<s.length){
if(g[i]<=s[j])
i++;
j++;
}
return i;
}
}
AC:17ms
Python解法
二刷,Python解法。
class Solution:
def findContentChildren(self, g, s):
"""
:type g: List[int]
:type s: List[int]
:rtype: int
"""
g.sort()
s.sort()
sp = 0
res = 0
for gi in g:
while sp < len(s) and s[sp] < gi:
sp += 1
if sp < len(s) and s[sp] >= gi:
res += 1
sp += 1
return res
日期
2017 年 1 月 7 日
2018 年 11 月 16 日 —— 又到周五了!
【LeetCode】455. Assign Cookies 解题报告(Java & Python)的更多相关文章
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode:455. Assign Cookies
package Others; import java.util.Arrays; //Question 455. Assign Cookies /* Assume you are an awesome ...
- 【LeetCode】383. Ransom Note 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- 【LeetCode】575. Distribute Candies 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 设置当前节点的值为下一个 日期 [LeetCode] ...
- 【LeetCode】349. Intersection of Two Arrays 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:Java解法,HashSet 方法二:Pyt ...
- 【LeetCode】136. Single Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】283. Move Zeroes 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:首尾指针 方法二:头部双指针+双循环 方法三 ...
- 【LeetCode】459. Repeated Substring Pattern 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历子串 日期 [LeetCode] 题目地址:ht ...
随机推荐
- PCA方差解释比例求解与绘图?
目录 主成分方差解释率计算 绘图示例 主成分方差解释率计算 通常,求得了PCA降维后的特征值,我们就可以绘图,但各个维度的方差解释率没有得到,就无法获得PC坐标的百分比. 有些工具的结果是提供了维度标 ...
- 毕业设计之zabbix---自带模板监控mysql内容
自带模板是不能直接建立连接就可以用的 必须经历一下几步: 建立用户权限: [root@mysql.quan.bbs lib]$mysql -u root -p Enter password: Welc ...
- window10快捷键 + 浏览器常用通用快捷键
一.window10快捷键 1.win+tab 缩小版的显示出桌面打开的所有窗口,然后再结合上下左右键加enter选择想要的窗口: 如果不想选择或者保留原有显示窗口,再按win+tab 或者 ...
- 在 vscode.dev 中直接运行 Python !纯浏览器环境,无后端!
其实有挺长一段时间没有写自己的 VS Code 插件了! 还是要感谢我们 DevDiv 组的 Flexible Friday 活动,让我可以在工作日研究自己感兴趣的项目. Flexible Frida ...
- 我好像发现了一个Go的Bug?
从一次重构说起 这事儿还得从一次重构优化说起. 最近在重构一个路由功能,由于路由比较复杂,需求变化也多,于是想通过责任链模式来重构,刚好这段时间也在 Sentinel-Go 中看到相关源码. 用责任链 ...
- A Child's History of England.27
Then, the Red King went over to Normandy, where the people suffered greatly under the loose rule of ...
- 乱序拼图验证的识别并还原-puzzle-captcha
一.前言 乱序拼图验证是一种较少见的验证码防御,市面上更多的是拖动滑块,被完美攻克的有不少,都在行为轨迹上下足了功夫,本文不讨论轨迹模拟范畴,就只针对拼图还原进行研究. 找一个市面比较普及的顶像乱序拼 ...
- Ecshop 后台导出订单Excel时, 内存溢出的解决方法
今天继续跟大家分享一下,在我配置Ecshop时的问题. 今天的问题是在后台想要导出订单列表Excel时出现的内存溢出.错误提示如下 问题: Fatal error: Allowed memory s ...
- Xcode中匹配的配置包的存放目录
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport
- velocity示例
创建maven项目 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns ...