[LeetCode] 455. Assign Cookies_Easy tag: Sort
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.
思路用sort, 然后把最小的饼干依次给需要最少的child, 直到没有饼干可给或者没有孩子.
Code
- class Solution:
- def findContentChildren(self, g, s):
- """
- :type g: List[int]
- :type s: List[int]
- :rtype: int
- """
- g.sort()
- s.sort()
- childi, cookiei = 0,0
- while childi < len(g) and cookiei < len(s):
- if s[cookiei] >= g[childi]:
- childi += 1
- cookiei += 1
- return childi
[LeetCode] 455. Assign Cookies_Easy tag: Sort的更多相关文章
- LeetCode:455. Assign Cookies
package Others; import java.util.Arrays; //Question 455. Assign Cookies /* Assume you are an awesome ...
- 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 (C++)
题目: Assume you are an awesome parent and want to give your children some cookies. But, you should gi ...
- LeetCode: 455 Assign Cookies(easy)
题目: Assume you are an awesome parent and want to give your children some cookies. But, you should gi ...
- 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] 252. Meeting Rooms_Easy tag: Sort
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- [LeetCode] 506. Relative Ranks_Easy tag: Sort
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- 【leetcode】455. Assign Cookies
problem 455. Assign Cookies solution1: But, you should give each child at most one cookie. 对小朋友的满意程度 ...
- 455. Assign Cookies - LeetCode
Question 455. Assign Cookies Solution 题目大意:数组g的大小表示有几个小孩,每个元素表示小孩的食量,数组s的大小表示有多少个饼干,每个元素的大小表示每个饼干的大小 ...
随机推荐
- JavaScript外部函数调用AngularJS的函数、$scope
x 场景: 需要在用FusionCharts画的柱状图中添加点击事件,But弹出框是Angularjs搞的,我想的是直接跳到弹出框的那个路由里,然后在弹出框的控制器中绑定数据即可: /* 点击事件 * ...
- [No0000114]远程桌面剪贴板无法同步本机,无法复制粘贴问题解决
远程桌面无法与桌面共享复制内容(远程桌面复制之后,无法在本地桌面粘贴.反之亦然.),这时候需要杀掉一个进程并重新启动.[重启 rdpclip.exe] 1.在远程桌面中右键点击,选择启动任务管理器: ...
- spring boot拦截器WebMvcConfigurerAdapter,以及高版本的替换方案
Springboot中静态资源和拦截器处理(踩了坑) 背景: 在项目中我使用了自定义的Filter 这时候过滤了很多路径,当然对静态资源我是直接放过去的,但是,还是出现了静态资源没办法访问到spr ...
- HiveQL之Sort by、Distribute by、Cluster by、Order By详解
在这里解释一下select语法中的order by.sort by.distribute by.cluster by.order by语法. 一.order by语法 在hiveQL中Order by ...
- influxdb服务器 relay
https://www.influxdata.com/time-series-platform/influxdb/ https://www.xusheng.org/blog/2016/07/30/pe ...
- dp 单调性优化总结
对于单调性优化其实更多的是观察dp的状态转移式子的单调性 进而用优先队列 单调队列 二分查找什么的找到最优决策 使时间更优. 对于这道题就是单调性优化的很好的例子 首先打一个暴力再说. f[i][j] ...
- (未完成)在block内如何修改block外部变量
变量必须用__block修饰,否则编译不通过 block内部会把变量拷贝到堆区 变量从栈区copy->堆区 通过对对象取地址,打印出对象在内存中的地址 &a block不允许修改外部变量 ...
- jquery-- json字符串没有自动包装为 json对象
jquery 的一个坑 页面使用 ajax ,回调函数中获取后端返回的 json 格式数据(ajax 未显式指定返回值类型),后端controller方法标注 @ResponseBody 并返回一个 ...
- AES加解密所遇问题
AES加解密后解密数据末尾携带多余空格,经查看是由于加密时数据不足16个字节自动补齐导致 解决办法:记录加密数据长度,解密后根据数据长度读取解密数据. 另外加密数据中可能存在0等数据,所以拷贝内容时最 ...
- java 网络编程(四)TCP通讯
客户端: package cn.sasa.TcpDemo; import java.io.IOException; import java.io.InputStream; import java.io ...