当前位置:网站首页>【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;
}
}
边栏推荐
猜你喜欢

Jetpack之dataBinding

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

经纬度求距离

Jetpack's dataBinding

Day 76

C-8月1日-递归与动态内存管理

Matplotlib找不到字体,打印乱码

连接数据库时出现WARN: Establishing SSL connection without server‘s identity verification is not recommended.

开发公众号授权遇到的redirect_uri参数错误
![[Meetup] OpenMLDBxDolphinScheduler engineering and scheduling link link characteristics, building the end-to-end MLOps workflow](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[Meetup] OpenMLDBxDolphinScheduler engineering and scheduling link link characteristics, building the end-to-end MLOps workflow
随机推荐
Thesis unscramble TransFG: A Transformer Architecture for Fine - grained Recognition
函数使用记录
自己动手写RISC-V的C编译器-01实现加减法
Use the adb command to manage applications
C语言实现三子棋(代码详解)
解决AttributeError: ‘NoneType‘ object has no attribute ‘val‘ if left.val!=right.val:Line 17 问题
开发公众号授权遇到的redirect_uri参数错误
C语言-6月8日-求两个数的最小公倍数和最大公因数;判断一个数是否为完数,且打印出它的因子
js 学习进阶(事件高级 pink老师教学笔记)
微信小程序_开发工具的安装
8-byte standard request parsing during USB enumeration
Goldbach's conjecture and the ring of integers
Intelligent risk control China design and fall to the ground
解决npm warn config global `--global`, `--local` are deprecated. use `--location=global` instead.
C语言-7月21日-指针的深入
第一章 Verilog语言和Vivado初步使用
PAT乙级刷题之路
【LeetCode-162】寻找峰值
OpenMLDB v0.5.0 released | Performance, cost, flexibility reach new heights
连接数据库时出现WARN: Establishing SSL connection without server‘s identity verification is not recommended.