LeetCode 题解工作台

装满石头的背包的最大数量

现有编号从 0 到 n - 1 的 n 个背包。给你两个下标从 0 开始的整数数组 capacity 和 rocks 。第 i 个背包最大可以装 capacity[i] 块石头,当前已经装了 rocks[i] 块石头。另给你一个整数 additionalRocks ,表示 你可以放置的额外石头数量,…

category

3

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

中等 · 贪心·invariant

bolt

答案摘要

我们首先将每个背包的剩余容量计算出来,然后对剩余容量进行排序,接着我们从小到大遍历剩余容量,将额外的石头放入背包中,直到额外的石头用完或者背包的剩余容量用完为止,返回此时的背包数量即可。 时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 为背包的数量。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 贪心·invariant 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

现有编号从 0n - 1n 个背包。给你两个下标从 0 开始的整数数组 capacityrocks 。第 i 个背包最大可以装 capacity[i] 块石头,当前已经装了 rocks[i] 块石头。另给你一个整数 additionalRocks ,表示你可以放置的额外石头数量,石头可以往 任意 背包中放置。

请你将额外的石头放入一些背包中,并返回放置后装满石头的背包的 最大 数量

 

示例 1:

输入:capacity = [2,3,4,5], rocks = [1,2,4,4], additionalRocks = 2
输出:3
解释:
1 块石头放入背包 0 ,1 块石头放入背包 1 。
每个背包中的石头总数是 [2,3,4,4] 。
背包 0 、背包 1 和 背包 2 都装满石头。
总计 3 个背包装满石头,所以返回 3 。
可以证明不存在超过 3 个背包装满石头的情况。
注意,可能存在其他放置石头的方案同样能够得到 3 这个结果。

示例 2:

输入:capacity = [10,2,2], rocks = [2,2,0], additionalRocks = 100
输出:3
解释:
8 块石头放入背包 0 ,2 块石头放入背包 2 。
每个背包中的石头总数是 [10,2,2] 。
背包 0 、背包 1 和背包 2 都装满石头。
总计 3 个背包装满石头,所以返回 3 。
可以证明不存在超过 3 个背包装满石头的情况。
注意,不必用完所有的额外石头。

 

提示:

  • n == capacity.length == rocks.length
  • 1 <= n <= 5 * 104
  • 1 <= capacity[i] <= 109
  • 0 <= rocks[i] <= capacity[i]
  • 1 <= additionalRocks <= 109
lightbulb

解题思路

方法一:排序 + 贪心

我们首先将每个背包的剩余容量计算出来,然后对剩余容量进行排序,接着我们从小到大遍历剩余容量,将额外的石头放入背包中,直到额外的石头用完或者背包的剩余容量用完为止,返回此时的背包数量即可。

时间复杂度 O(n×logn)O(n \times \log n),空间复杂度 O(logn)O(\log n)。其中 nn 为背包的数量。

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution:
    def maximumBags(
        self, capacity: List[int], rocks: List[int], additionalRocks: int
    ) -> int:
        for i, x in enumerate(rocks):
            capacity[i] -= x
        capacity.sort()
        for i, x in enumerate(capacity):
            additionalRocks -= x
            if additionalRocks < 0:
                return i
        return len(capacity)
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Can the candidate efficiently handle large inputs (up to 50,000 bags)?

  • question_mark

    Is the candidate familiar with sorting algorithms and their time complexity?

  • question_mark

    Does the candidate understand the trade-off between greedy algorithms and optimal solutions?

warning

常见陷阱

外企场景
  • error

    Not sorting the bags by remaining capacity, which could result in suboptimal placement of rocks.

  • error

    Overlooking edge cases where additional rocks are not needed or cannot be used effectively.

  • error

    Failing to account for the possibility that some bags might already be full before distributing the extra rocks.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    What if there are multiple ways to distribute the additional rocks but only one optimal solution exists?

  • arrow_right_alt

    How would you approach the problem if the additional rocks can be distributed only among certain bags (restricted bags)?

  • arrow_right_alt

    What if the bags had different weight limits, and only a limited number of bags could be chosen for rock distribution?

help

常见问题

外企场景

装满石头的背包的最大数量题解:贪心·invariant | LeetCode #2279 中等