【陪伴式刷题】Day 27|贪心算法|455.分发饼干(Assign Cookies)

刷题顺序按照代码随想录建议

题目描述

英文版描述

Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie.

Each child i has a greed factor g[i], which is the minimum size of a cookie that the child will be content with; and each cookie j has a size s[j]. If s[j] >= g[i], we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number.

Example 1:

Input: g = [1,2,3], s = [1,1] Output: 1 Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3. And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content. You need to output 1.

Example 2:

Input: g = [1,2], s = [1,2,3] Output: 2 Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2. You have 3 cookies and their sizes are big enough to gratify all of the children, You need to output 2.

Constraints:

  • 1 <= g.length <= 3 * 10^4
  • 0 <= s.length <= 3 * 10^4
  • 1 <= g[i], s[j] <= 2^31 - 1

英文版地址

leetcode.com/problems/as...

中文版描述

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

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

示例 1:

输入: g = [1,2,3], s = [1,1] 输出: 1 解释: 你有三个孩子和两块小饼干,3个孩子的胃口值分别是:1,2,3。 虽然你有两块小饼干,由于他们的尺寸都是1,你只能让胃口值是1的孩子满足。 所以你应该输出1。

示例 2:

输入: g = [1,2], s = [1,2,3] 输出: 2 解释: 你有两个孩子和三块小饼干,2个孩子的胃口值分别是1,2。 你拥有的饼干数量和尺寸都足以让所有孩子满足。 所以你应该输出2.

提示:

  • 1 <= g.length <= 3 * 10^4
  • 0 <= s.length <= 3 * 10^4
  • 1 <= g[i], s[j] <= 2^31 - 1

中文版地址

leetcode.cn/problems/as...

解题方法

俺这版

java 复制代码
class Solution {
    public int findContentChildren(int[] g, int[] s) {
        if (g == null || s == null || g.length == 0 || s.length == 0) {
            return 0;
        }
        Arrays.sort(g);
        Arrays.sort(s);
        int gLength = g.length - 1;
        int count = 0;
        for (int j = s.length - 1; j >= 0; j--) {
            for (int i = gLength; i >= 0; i--) {
                if (g[i] <= s[j]) {
                    count++;
                    gLength = gLength - (gLength - i) - 1;
                    break;
                }
            }
        }
        return count;
    }
}

复杂度分析

  • 时间复杂度:O(n^2),n为数组g和s长度的较大的值,后半部分嵌套了两次for循环,虽然实际不会每次都遍历全部的元素,因为gLength的长度一直在缩小,但理论上的时间复杂度应该还是O(n^2),而数组排序的时间复杂度是O(nlogn),因此整体的时间复杂度是O(n^2),n为数组g和s长度的较大的值
  • 空间复杂度:O(nlogn),n为数组g和s长度的较大的值,整个方法主要占用额外内存的是一开始的两次数组排序,其余为O(1),因此整体的空间复杂度是O(nlogn),n为数组g和s长度的较大的值

官方版

java 复制代码
class Solution {
    public int findContentChildren(int[] g, int[] s) {
        Arrays.sort(g);
        Arrays.sort(s);
        int m = g.length, n = s.length;
        int count = 0;
        for (int i = 0, j = 0; i < m && j < n; i++, j++) {
            while (j < n && g[i] > s[j]) {
                j++;
            }
            if (j < n) {
                count++;
            }
        }
        return count;
    }
}

复杂度分析

官方给出的>>

  • 时间复杂度:O(mlog⁡m+nlog⁡n),其中 m 和 n 分别是数组 g 和 s 的长度,对两个数组排序的时间复杂度是 O(mlog⁡m+nlog⁡n),遍历数组的时间复杂度是 O(m+n),因此总时间复杂度是 O(mlog⁡m+nlog⁡n)
  • 空间复杂度:O(log⁡m+log⁡n),其中 m 和 n 分别是数组 g 和 s 的长度,空间复杂度主要是排序的额外空间开销。
相关推荐
极客先躯33 分钟前
高级java每日一道面试题-2024年10月3日-分布式篇-分布式系统中的容错策略都有哪些?
java·分布式·版本控制·共识算法·超时重试·心跳检测·容错策略
夜月行者1 小时前
如何使用ssm实现基于SSM的宠物服务平台的设计与实现+vue
java·后端·ssm
程序猿小D1 小时前
第二百六十七节 JPA教程 - JPA查询AND条件示例
java·开发语言·前端·数据库·windows·python·jpa
潘多编程1 小时前
Java中的状态机实现:使用Spring State Machine管理复杂状态流转
java·开发语言·spring
_阿伟_2 小时前
SpringMVC
java·spring
代码在改了2 小时前
springboot厨房达人美食分享平台(源码+文档+调试+答疑)
java·spring boot
猿java2 小时前
使用 Kafka面临的挑战
java·后端·kafka
wclass-zhengge2 小时前
数据结构篇(绪论)
java·数据结构·算法
何事驚慌2 小时前
2024/10/5 数据结构打卡
java·数据结构·算法
结衣结衣.2 小时前
C++ 类和对象的初步介绍
java·开发语言·数据结构·c++·笔记·学习·算法