当前位置:网站首页>【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;
}
}
边栏推荐
- 【LeetCode-162】寻找峰值
- Use the adb command to manage applications
- 厂商推送平台-华为接入
- 【无标题】
- Day 76
- Event Preview | On April 23, a number of wonderful sharing sessions of OpenMLDB will come, which will live up to the good time of the weekend
- JS进阶网页特效(pink老师笔记)
- Building a data ecology for feature engineering - Embrace the open source ecology, OpenMLDB fully opens up the MLOps ecological tool chain
- Invalid revision: 3.18.1-g262b901-dirty
- Asis2016 books null off by one
猜你喜欢
随机推荐
mk file introduction
Byte (byte) and bit (bit)
Day 70
Day 87
深度学习Matlab工具箱代码注释
The whole process of Tinker access --- configuration
buuctf hacknote
PAT乙级刷题之路
开发公众号授权遇到的redirect_uri参数错误
JS事件循环机制
js 学习进阶(Dom部分 pink老师教学笔记)
品优购项目实战笔记
C语言实现三子棋(代码详解)
基于微信小程序云开发实现的电商项目,可以自行定制开发
[Meetup Preview] OpenMLDB+OneFlow: Link feature engineering to model training to accelerate machine learning model development
星盟-pwn-babyheap
Visual studio2019 配置使用pthread
Jetpack's dataBinding
Invalid revision: 3.18.1-g262b901-dirty
stack stack









