当前位置:网站首页>【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;
}
}
边栏推荐
猜你喜欢
基于微信小程序云开发实现的电商项目,可以自行定制开发
C语言-7月31日-指针的总结以及typedef关键字
C语言-7月22日- NULL和nullptr的深入了解以及VScode对nullptr语句报错问题的解决
Manufacturer Push Platform-Huawei Access
The third phase of the contributor task is wonderful
自己动手写RISC-V的C编译器-02语法描述方法和递归下降解析
第六届蓝帽杯 EscapeShellcode
Invalid revision: 3.18.1-g262b901-dirty
JS小技巧,让你编码效率杠杠的,快乐摸鱼
ARM assembly instruction ADR and LDR
随机推荐
Day 73
The whole process of Tinker access --- configuration
Tinker接入全流程---编译篇
JS进阶网页特效(pink老师笔记)
C语言-6月8日-给定一个字符数组‘i am a student’ 统计字符a的个数并进行输出
使用adb命令管理应用
gerrit configure SSH Key and account, email information
SearchGuard configuration
ARM assembly instruction ADR and LDR
mk文件介绍
Interpretation of the paper: Cross-Modality Fusion Transformer for Multispectral Object Detection
经纬度求距离
Day 72
详解程序执行过程
【LeetCode-205】同构字符串
The role of the port
Day 82
Some formulas for system performance and concurrency
buuctf hacknote
Certificate of SearchGuard configuration