当前位置:网站首页>LeetCode_455_分发饼干
LeetCode_455_分发饼干
2022-07-23 14:47:00 【Fitz1318】
题目链接
题目描述
假设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。
对每个孩子 i,都有一个胃口值 g[i],这是能让孩子们满足胃口的饼干的最小尺寸;并且每块饼干 j,都有一个尺寸 s[j] 。如果 s[j] >= g[i],我们可以将这个饼干 j 分配给孩子 i ,这个孩子会得到满足。你的目标是尽可能满足越多数量的孩子,并输出这个最大数值。
示例 1:
输入: g = [1,2,3], s = [1,1]
输出: 1
解释:
你有三个孩子和两块小饼干,3个孩子的胃口值分别是:1,2,3。
虽然你有两块小饼干,由于他们的尺寸都是1,你只能让胃口值是1的孩子满足。
所以你应该输出1。
示例 2:
输入: g = [1,2], s = [1,2,3]
输出: 2
解释:
你有两个孩子和三块小饼干,2个孩子的胃口值分别是1,2。
你拥有的饼干数量和尺寸都足以让所有孩子满足。
所以你应该输出2.
提示:
1 <= g.length <= 3 * 10^40 <= s.length <= 3 * 10^41 <= g[i], s[j] <= 2^(31) - 1
解题思路
贪心法
优先考虑饼干,小饼干先给小胃口
AC代码
class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int ans = 0;
int j = 0;
for (int i = 0; i < s.length && j < g.length; i++) {
if (s[i] >= g[j]) {
j++;
ans++;
}
}
return ans;
}
}
边栏推荐
- 深拷贝deepClone的实现
- 爱可可AI前沿推介(7.23)
- 在 Kotlin 中使用 Flow Builder 创建流
- 用pymysql封装项目通用的连接和查询
- Software configuration | pychart download, installation, environment configuration and uninstall
- sns_sensor_instance_api
- 食品安全|喝鲜奶可能感染结核病?带你了解什么是牛奶灭菌
- [MySQL]一、MySQL起步
- Agile testing practice in large-scale teams
- Opencv finding the intersection of two regions
猜你喜欢

SQL報錯盲注詳解

Failure analysis and solution of vscode PIO creation project

Compressed storage of arrays and special matrices

Pymoo学习 (2):带约束的双目标优化问题

死磕递归1:递推公式

Function secondary development / plug-in development of JMeter (detailed version)

Food safety chocolate is also true or false? How much do you know about it

SQL报错盲注详解

Kubernetes kubelet manages pod core process

How to refine the operation of small program mall?
随机推荐
Program environment and pretreatment
Pymoo学习 (1):基本概念
新零售电商平台怎么做?才能实现传统零售企业数字化转型?
股票历史数据下载接口汇总(动态更新)
小程序商城如何精细化运营?
食品安全|巧克力也有真假?关于它你了解多少
日期格式化
程序员最想干的三件事 |漫画
OpenIM重大优化-消息按需加载 一致性缓存 uniapp发布
wsus可以打mysql中间件补丁_加入WSUS补丁服务器并下载补丁
Keil errors and solutions (1): fcarm - output name not specified, please check 'options for target - Utilities‘
单细胞论文记录(part19)--A comprehensive comparison on cell-type composition inference for ST data
How to set up the router correctly
网络基础设施可视化
Scene notes
keil错误和解决办法(1):FCARM - Output Name not specified, please check ‘Options for Target - Utilities‘
Pymoo学习 (4): 多标准决策
单细胞文献学习(part6)--ForestFireClustering for sc sequencing combines iterative label propagation with ...
keras——accuracy_ Score formula
食品安全|爱吃烟熏食品的注意了,这些知识你知道吗