【陪伴式刷题】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 的长度,空间复杂度主要是排序的额外空间开销。
相关推荐
BD_Marathon几秒前
【IDEA】Debug(调试)
java·ide·intellij-idea
做怪小疯子13 分钟前
LeetCode 热题 100——二叉树——翻转二叉树
算法·leetcode·职场和发展
嘟嘟w13 分钟前
JVM性能调优
java
Godson_beginner15 分钟前
Sa-Token (java权限认证框架)
java·开发语言
头发那是一根不剩了16 分钟前
Spring Boot「多数据源并存」的设计思路,它与动态数据源又有什么区别?
java·spring boot·后端
o***592720 分钟前
spring注入static属性
java·后端·spring
风象南24 分钟前
Spring Boot实现HTTPS双向认证
java·spring boot·后端
青春不流名31 分钟前
Java List初始化的例子
java·windows·list
4***172732 分钟前
【MySQL篇】使用Java操作MySQL实现数据交互
java·mysql·交互
sheji341634 分钟前
【开题答辩全过程】以 基于Spring Boot的流浪动物救助系统设计为例,包含答辩的问题和答案
java·spring boot·后端