455. Assign Cookies - LeetCode
Question

Solution
题目大意:数组g的大小表示有几个小孩,每个元素表示小孩的食量,数组s的大小表示有多少个饼干,每个元素的大小表示每个饼干的大小,把饼干分给小孩,每个小孩只能分一个饼干,问最多能满足多少个小孩.
思路:遍历小孩,为每个小孩遍历饼干
Java实现:
public int findContentChildren(int[] g, int[] s) {
int ans = 0;
Arrays.sort(s);
for (int i = 0; i < g.length; i++) {
for (int j = 0; j < s.length; j++) {
if (g[i] <= s[j]) {
s[j] = -1;
ans ++;
break;
}
}
}
return ans;
}
优化:先把小孩和饼干排序,再遍历
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int ans = 0;
int i=0;
int j=0;
while (i<g.length && j < s.length) {
if (g[i] <= s[j]) {
s[j] = -1;
ans ++;
i++;
}
j++;
}
return ans;
}
455. Assign Cookies - LeetCode的更多相关文章
- 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. 对小朋友的满意程度 ...
- 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 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- 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&Python] Problem 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 greedy]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(easy)
题目: Assume you are an awesome parent and want to give your children some cookies. But, you should gi ...
随机推荐
- 【C语言】预处理、宏定义、内联函数
一.由源码到可执行程序的过程 1. 预处理: 源码经过预处理器的预处理变成预处理过的.i中间文件 1 gcc -E test.c -o test.i 2. 编译: 中间文件经过编译器编译形成.s的汇编 ...
- IPython是什么?
参考:IPython 中常用的魔法命令 Ipython中的魔法命令总结 IPython 是一个 python 的交互式 shell,比默认的python shell 好用得多,支持变量自动补全,自动缩 ...
- 5_系统的可控性_Controllability
- 前端每日实战:133# 视频演示如何用 CSS 和 GSAP 创作有多个关键帧的连续动画
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/eLMKJG 可交互视频 此视频是可 ...
- Android Studio安装问题
安装问题可以参考:https://blog.csdn.net/y74364/article/details/96121530 但是gradle安装缓慢,需要FQ.有加速器FQ的可以开加速器安装,没有的 ...
- 【VUE】 前端面试题小结
1,对代码重构的理解: 2,http和https协议有什么区别 3,从输入URL到页面加载全过程 4,前端怎么控制管理路由 5,缓存机制(描述一下 cookies,sessionStorage 和 l ...
- java的命令行参数到底怎么用,请给截图和实际的例子
8.2 命令行参数示例(实验) public class Test { public static void main(String[] args){ if(args.length ...
- CSS简单样式练习(一)
运行效果: 源代码: 1 <!DOCTYPE html> 2 <html lang="zh"> 3 <head> 4 <meta char ...
- numpy教程06---ndarray的进阶操作
欢迎关注公众号[Python开发实战], 获取更多内容! 工具-numpy numpy是使用Python进行数据科学的基础库.numpy以一个强大的N维数组对象为中心,它还包含有用的线性代数,傅里叶变 ...
- matplotlib---Annotation标注
import numpy as np import matplotlib.pyplot as plt x = np.linspace(-3, 3, 50) y = 2 * x + 1 plt.figu ...