【陪伴式刷题】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 的长度,空间复杂度主要是排序的额外空间开销。
相关推荐
蜗牛 | ICU14 分钟前
【推荐项目】 043-停车管理系统
java·vue.js·spring boot·前端框架
吱屋猪_19 分钟前
全面解析Tomcat:简介、安装与配置指南
java·tomcat
裁二尺秋风21 分钟前
CI/CD—Jenkins实现自动构建Docker镜像运行Java程序
java·ci/cd·jenkins
beite爱摸鱼28 分钟前
简单实现一个DEEPSEEK前端+后端
java·vue.js
Lx35228 分钟前
《从头开始学java,一天一个知识点》之:字符串处理:String类的核心API
java·后端
不止极客__JavaGuide_net29 分钟前
百万架构师第四十八课:并发编程的原理(三)|JavaGuide
java
Lx35231 分钟前
《从头开始学java,一天一个知识点》之:多维数组与常见操作
java·后端
Cache技术分享33 分钟前
16. Java 如何创建、初始化和访问数组
java·后端
啾啾Fun37 分钟前
[微服务设计]2_演化式架构
java·微服务·架构
好易学数据结构38 分钟前
可视化图解算法:反转链表
数据结构·算法·leetcode·链表·面试·力扣·笔试·牛客网·链表反转