首页 > 其他分享 >【LeeCode】455. 分发饼干

【LeeCode】455. 分发饼干

时间:2023-01-14 23:00:22浏览次数:54  
标签:count 饼干 int 455 Solution LeeCode new findContentChildren

【题目描述】

假设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。

对每个孩子 ​​i​​​,都有一个胃口值 ​​g[i]​这是能让孩子们满足胃口的饼干的最小尺寸;并且每块饼干 ​​j​​​,都有一个尺寸 ​​s[j]​ 。如果 ​​s[j] >= g[i]​​​,我们可以将这个饼干 ​​j​​​ 分配给孩子 ​​i​​ ,这个孩子会得到满足。你的目标是尽可能满足越多数量的孩子,并输出这个最大数值。

​https://leetcode.cn/problems/assign-cookies/​

【示例】

【LeeCode】455. 分发饼干_i++

【代码】​​代码随想录​

import java.util.*;
// 2023-1-14

class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int count = 0;
int index = 0;
// 这里优先考虑饼干, 小饼干喂饱小胃口
for (int i = 0; i < s.length && index < g.length; i++) {
if (s[i] >= g[index]){
count++;
index++;
}
}
System.out.println(count);
return count;
}
}


public class Main {
public static void main(String[] args) {
new Solution().findContentChildren(new int[]{1,2,3}, new int[]{1, 1}); // 输出: 1
new Solution().findContentChildren(new int[]{1,2}, new int[]{1, 2, 3}); // 输出: 2
new Solution().findContentChildren(new int[]{1,2,3}, new int[]{3}); // 输出: 1
new Solution().findContentChildren(new int[]{10,9,8,7}, new int[]{5,6,7,8}); // 输出: 2
}
}


【代码】admin

通过率 19/21; 这里的思路其实有点问题, 因为是对饼干排序后,是从小到大的顺序。如果最大的能够满足孩子最大的胃口肯定也可以满足最小的胃口;所以这里能够倒序应该就可以了

import java.util.*;
// 2023-1-14

class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int count = 0;
int index = 0;
for (int i = 0; i < g.length; i++) {
for (int j = index; j < s.length; j++) {
if (s[j] >= g[i]){
count++;
index +=1;
break;
}
}
}
System.out.println(count);
return count;
}
}


public class Main {
public static void main(String[] args) {
new Solution().findContentChildren(new int[]{1,2,3}, new int[]{1, 1}); // 输出: 1
new Solution().findContentChildren(new int[]{1,2}, new int[]{1, 2, 3}); // 输出: 2
new Solution().findContentChildren(new int[]{1,2,3}, new int[]{3}); // 输出: 1
}
}

标签:count,饼干,int,455,Solution,LeeCode,new,findContentChildren
From: https://blog.51cto.com/u_13682316/6007955

相关文章

  • 【LeeCode】637.二叉树的层平均值
    【题目描述】给定一个非空二叉树的根节点 ​​​root​​​ ,以数组的形式返回每一层节点的平均值。与实际答案相差 ​​​10-5​​​ 以内的答案可以被接受。​​http......
  • 【LeeCode】429.N叉树的层序遍历
    【题目描述】给定一个N叉树,返回其节点值的层序遍历。(即从左到右,逐层遍历)。树的序列化输入是用层序遍历,每组子节点都由null值分隔(参见示例)。​​https://leetcode.cn/pro......
  • 【LeeCode】199.二叉树的右视图
    【题目描述】给定一个二叉树的 根节点 ​​​​root​​​​,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值​​​​https://leetcode.cn/proble......
  • 455. 分发饼干
    问题链接https://leetcode.cn/problems/assign-cookies/description/解题思路这个题目,同样可以运用贪心的思路。我们首先进行排序。对孩子需要的饼干大小,和能够提供的......
  • 【LeeCode】202. 快乐数
    【题目描述】编写一个算法来判断一个数 ​​n​​ 是不是快乐数。「快乐数」 定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和。然后重复这个过程直......
  • 【LeeCode】1295. 统计位数为偶数的数字
    【题目描述】给你一个整数数组 ​​nums​​,请你返回其中位数为 偶数 的数字的个数。​​​https://leetcode.cn/problems/find-numbers-with-even-number-of-digits/des......
  • 【LeeCode】7. 整数反转
    【题目描述】给你一个32位的有符号整数 ​​x​​​ ,返回将 ​​x​​ 中的数字部分反转后的结果。如果反转后整数超过32位的有符号整数的范围 ​​[−231, 231......
  • 【LeeCode】377. 组合总和 Ⅳ
    【题目描述】给你一个由 不同 整数组成的数组 ​​nums​​​ ,和一个目标整数 ​​target​​​ 。请你从 ​​nums​​​ 中找出并返回总和为 ​​target​​ 的......
  • CVE-2007-4556 s2-001
    漏洞名称S2-001远程代码执行利用条件WebWork2.1(withaltSyntaxenabled),WebWork2.2.0-WebWork2.2.5,Struts2.0.0-Struts2.0.8不受影响的版本:WebWork2.......
  • P4551 最长异或路径 : 01tire + 树 + 异或
    题P4551最长异或路径https://www.luogu.com.cn/problem/P4551知识背景01tire树,可以用来查找异或的最大值。经典问题如下。在nums中,哪两个数中异或值最大。解决方法:......