Question

455. Assign Cookies

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的更多相关文章

  1. LeetCode:455. Assign Cookies

    package Others; import java.util.Arrays; //Question 455. Assign Cookies /* Assume you are an awesome ...

  2. 【leetcode】455. Assign Cookies

    problem 455. Assign Cookies solution1: But, you should give each child at most one cookie. 对小朋友的满意程度 ...

  3. LeetCode 455. Assign Cookies (分发曲奇饼干)

    Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...

  4. 【LeetCode】455. Assign Cookies 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

  5. 12. leetcode 455.Assign Cookies

    Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...

  6. [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 ...

  7. LeetCode 455. Assign Cookies (C++)

    题目: Assume you are an awesome parent and want to give your children some cookies. But, you should gi ...

  8. [leetcode greedy]455. Assign Cookies

    Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...

  9. LeetCode: 455 Assign Cookies(easy)

    题目: Assume you are an awesome parent and want to give your children some cookies. But, you should gi ...

随机推荐

  1. 理解Android Framework

    一 . Android 系统架构 Android是一个包括操作系统,中间件和关键应用的移动设备软件堆: 作为一个开源的软件,android包含了众多的功能和庞大的代码,他的代码基于linux. 1. ...

  2. Android开发 之 理解Handler、Looper、MessageQueue、Thread关系

    本文转自博客:http://blog.csdn.net/he90227/article/details/43567073 一. 图解与概述 首先Android中 的每一个线程都会对应一个Message ...

  3. Blazor组件自做六 : 使用JS隔离封装Baidu地图

    1. 运行截图 演示地址 2. 在文件夹wwwroot/lib,添加baidu子文件夹,添加baidumap.js文件 2.1 跟上一篇类似,用代码方式异步加载API,脚本生成新的 body > ...

  4. LC-209

    给定一个含有 n 个正整数的数组和一个正整数 target . 找出该数组中满足其和 ≥ target 的长度最小的 连续子数组 [numsl, numsl+1, ..., numsr-1, nums ...

  5. 在Blazor中实现拖放(drag and drop)

    前言 我在实现一个含有待办列表功能的页面时,发现了一个好看的设计,它将待办分为--"待办","正在进行",和"已完成"三种状态,并且将待办通 ...

  6. Kafka 使用-安装

    Kafka 使用-安装 官方网站 http://kafka.apache.org/ 官方文档 Kafka 是什么? Apache Kafka is an open-source distributed ...

  7. 5.Java程序运行机制

    一.编译型和解释型语言区别 计算机是不能理解高级语言的,更不能直接执行高级语言,它只能直接理解机器语言,所以任何的高级语言编写的程序都必须转换成计算机语言,也就是机器码.而这种转换的方式有两种: 编译 ...

  8. css 实现输入效果

    <template>   <h1>Pure CSS Typing animation.</h1> </template> <script> ...

  9. SimpleDateFormat类的安全问题,这6个方案总有一个适合你

    摘要:你使用的SimpleDateFormat类还安全吗?为什么说SimpleDateFormat类不是线程安全的?带着问题从本文中寻求答案. 本文分享自华为云社区<[高并发]SimpleDat ...

  10. liunx ip无法显示问题解决

    目录 出现现象描述 解决方案步骤: 1.停止NetworkManager 2.清空NetworkManager 3. 重启net service 4. 再次查看网络状态ifconig 出现现象描述 e ...