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 ...
随机推荐
- ubuntu 安装 mysql mariadb
本教程面向Ubuntu服务器,适用于Ubuntu的任何LTS版本,包括Ubuntu 14.04,Ubuntu 16.04,Ubuntu 18.04,甚至非LTS版本(如Ubuntu 17.10和其他基 ...
- LQR (线性二次型调节器)的直观推导及简单应用
转自:https://blog.csdn.net/heyijia0327/article/details/39270597 本文主要介绍LQR的直观推导,说明LQR目标函数J选择的直观含义以及简单介绍 ...
- 11_滞后补偿器_Lag Compensator_Matlab_Simulink
下图中左边没有补偿器的稳态误差,右边是有只猴补偿器的稳态误差,H(s)为滞后补偿器的原因是H(s)bode图的相位图是负的 其中黄线是没有滞后补偿器的,蓝线是滞后补偿器中p = 1 ,q = 9的曲线 ...
- 删除html标签或标签属性以及样式
JavaScript module for stripping HTML tags and/or HTML element attributes from strings. 安装 npm instal ...
- 【LeetCode】567. 字符串的排列
567. 字符串的排列 知识点:字符串:滑动窗口 题目描述 给你两个字符串 s1 和 s2 ,写一个函数来判断 s2 是否包含 s1 的排列.如果是,返回 true :否则,返回 false . 换句 ...
- Vue实战-购物车案例
Vue实战-购物车案例 普通购物车 实现的功能:添加商品到购物车,计算总价 <!DOCTYPE html> <html lang="en"> <hea ...
- docker基础_docker引擎内部原理
docker引擎内部原理 docker主要由以下主要组件构成:docker客户端.docker守护进程(daemon).containerd.runc.shim daemon daemon的主要功能包 ...
- transform动画
1. html 结构 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- who 的页面制作
1. html 结构 <!-- section: Who we are --> <section id="who"> <div class=" ...
- 基于pgrouting的路径规划处理
对于GIS业务来说,路径规划是非常基础的一个业务,一般公司如果处理,都会直接选择调用已经成熟的第三方的接口,比如高德.百度等.当然其实路径规划的算法非常多,像比较著名的Dijkstra.A*算法等.当 ...