当前位置:网站首页>【LeetCode-455】方法饼干
【LeetCode-455】方法饼干
2022-08-11 05:30:00 【Ring*】
6.10 方法饼干【455】
6.10.1 题目描述
假设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。
对每个孩子 i,都有一个胃口值 g[i],这是能让孩子们满足胃口的饼干的最小尺寸;并且每块饼干 j,都有一个尺寸 s[j] 。如果 s[j] >=g[i],我们可以将这个饼干 j 分配给孩子 i ,这个孩子会得到满足。你的目标是尽可能满足越多数量的孩子,并输出这个最大数值。
6.10.2 方法一:排序+贪心

class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int numOfChildren = g.length, numOfCookies = s.length;
int count = 0;
for (int i = 0, j = 0; i < numOfChildren && j < numOfCookies; i++, j++) {
while (j < numOfCookies && g[i] > s[j]) {
j++;
}
if (j < numOfCookies) {
count++;
}
}
return count;
}
}
复杂度分析
6.10.3 my answer—排序
class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int n = g.length + s.length;
int p1=0,p2=0;
int sum =0;
for(int i = 0;i<n;i++){
if(p1==g.length || p2 == s.length)break;
if(s[p2]>=g[p1]){
// 第p2+1块饼干满足第p1+1个孩子
sum++;
p1++;
p2++;
}else{
// 不满足该孩子则后移一位选取饼干大一点的
p2++;
}
}
return sum;
}
}
边栏推荐
- Asis2016 books null off by one
- Day 82
- Compilation exception resolution
- 2022DASCTF X SU 三月春季挑战赛 checkin ROPgadget进阶使用
- Goldbach's conjecture and the ring of integers
- 本地缓存cookie的使用
- js 学习进阶(Dom部分 pink老师教学笔记)
- Some formulas for system performance and concurrency
- C语言-7月22日- NULL和nullptr的深入了解以及VScode对nullptr语句报错问题的解决
- OpenMLDB + Jupyter Notebook: Quickly Build Machine Learning Applications
猜你喜欢
随机推荐
The role of the port
【LeetCode-205】同构字符串
字节(byte)和位(bit)
Use c language to implement tic-tac-toe chess (with source code, you can run it directly)
Building a data ecology for feature engineering - Embrace the open source ecology, OpenMLDB fully opens up the MLOps ecological tool chain
无效的修订:3.18.1-g262b901-dirty
【无标题】
Node 踩坑之80端口被占用
C语言-7月18日-二维数组的学习
Matplotlib找不到字体,打印乱码
[Meetup Preview] OpenMLDB+OneFlow: Link feature engineering to model training to accelerate machine learning model development
一文看懂注解与反射
品优购项目实战笔记
Intelligent risk control China design and fall to the ground
Manufacturer Push Platform-Huawei Access
C-动态内存管理
微信小程序_开发工具的安装
2022DASCTF X SU 三月春季挑战赛 checkin ROPgadget进阶使用
C语言-6月8日-求两个数的最小公倍数和最大公因数;判断一个数是否为完数,且打印出它的因子
JVM tuning and finishing









