|
| 1 | +--- |
| 2 | +title: 3074.重新分装苹果:排序 |
| 3 | +date: 2025-12-24 18:48:30 |
| 4 | +tags: [题解, LeetCode, 简单, 贪心, 数组, 排序] |
| 5 | +categories: [题解, LeetCode] |
| 6 | +--- |
| 7 | + |
| 8 | +# 【LetMeFly】3074.重新分装苹果:排序 |
| 9 | + |
| 10 | +力扣题目链接:[https://leetcode.cn/problems/apple-redistribution-into-boxes/](https://leetcode.cn/problems/apple-redistribution-into-boxes/) |
| 11 | + |
| 12 | +<p>给你一个长度为 <code>n</code> 的数组 <code>apple</code> 和另一个长度为 <code>m</code> 的数组 <code>capacity</code> 。</p> |
| 13 | + |
| 14 | +<p>一共有 <code>n</code> 个包裹,其中第 <code>i</code> 个包裹中装着 <code>apple[i]</code> 个苹果。同时,还有 <code>m</code> 个箱子,第 <code>i</code> 个箱子的容量为 <code>capacity[i]</code> 个苹果。</p> |
| 15 | + |
| 16 | +<p>请你选择一些箱子来将这 <code>n</code> 个包裹中的苹果重新分装到箱子中,返回你需要选择的箱子的<strong> 最小</strong> 数量。</p> |
| 17 | + |
| 18 | +<p><strong>注意</strong>,同一个包裹中的苹果可以分装到不同的箱子中。</p> |
| 19 | + |
| 20 | +<p> </p> |
| 21 | + |
| 22 | +<p><strong class="example">示例 1:</strong></p> |
| 23 | + |
| 24 | +<pre> |
| 25 | +<strong>输入:</strong>apple = [1,3,2], capacity = [4,3,1,5,2] |
| 26 | +<strong>输出:</strong>2 |
| 27 | +<strong>解释:</strong>使用容量为 4 和 5 的箱子。 |
| 28 | +总容量大于或等于苹果的总数,所以可以完成重新分装。 |
| 29 | +</pre> |
| 30 | + |
| 31 | +<p><strong class="example">示例 2:</strong></p> |
| 32 | + |
| 33 | +<pre> |
| 34 | +<strong>输入:</strong>apple = [5,5,5], capacity = [2,4,2,7] |
| 35 | +<strong>输出:</strong>4 |
| 36 | +<strong>解释:</strong>需要使用所有箱子。 |
| 37 | +</pre> |
| 38 | + |
| 39 | +<p> </p> |
| 40 | + |
| 41 | +<p><strong>提示:</strong></p> |
| 42 | + |
| 43 | +<ul> |
| 44 | + <li><code>1 <= n == apple.length <= 50</code></li> |
| 45 | + <li><code>1 <= m == capacity.length <= 50</code></li> |
| 46 | + <li><code>1 <= apple[i], capacity[i] <= 50</code></li> |
| 47 | + <li>输入数据保证可以将包裹中的苹果重新分装到箱子中。</li> |
| 48 | +</ul> |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +## 解题方法:排序 |
| 53 | + |
| 54 | +每个<font title="2025.12.24">苹果</font>都要有它的归宿,一共有多少个苹果呢?算一算就知道了。 |
| 55 | + |
| 56 | +如何使用最少的箱子?当然是用尽可能大的箱子! |
| 57 | + |
| 58 | +把箱子从大到小排个序,遍历箱子并依次消费苹果,直到苹果消费完毕。 |
| 59 | + |
| 60 | ++ 时间复杂度$O(n+m\log m)$ |
| 61 | ++ 空间复杂度$O(\log m)$ |
| 62 | + |
| 63 | +### AC代码 |
| 64 | + |
| 65 | +#### C++ |
| 66 | + |
| 67 | +```cpp |
| 68 | +/* |
| 69 | + * @LastEditTime: 2025-12-24 13:32:26 |
| 70 | + */ |
| 71 | +class Solution { |
| 72 | +public: |
| 73 | + int minimumBoxes(vector<int>& apple, vector<int>& capacity) { |
| 74 | + sort(capacity.begin(), capacity.end(), greater<>()); |
| 75 | + int cnt = 0; |
| 76 | + for (int t : apple) { |
| 77 | + cnt += t; |
| 78 | + } |
| 79 | + int ans = 0; |
| 80 | + for (int t : capacity) { |
| 81 | + cnt -= t; |
| 82 | + ans++; |
| 83 | + if (cnt <= 0) { |
| 84 | + return ans; |
| 85 | + } |
| 86 | + } |
| 87 | + return -1; // Fake Return |
| 88 | + } |
| 89 | +}; |
| 90 | +``` |
| 91 | +
|
| 92 | +#### Python |
| 93 | +
|
| 94 | +```python |
| 95 | +''' |
| 96 | +LastEditTime: 2025-12-24 13:40:50 |
| 97 | +''' |
| 98 | +from typing import List |
| 99 | +
|
| 100 | +class Solution: |
| 101 | + def minimumBoxes(self, apple: List[int], capacity: List[int]) -> int: |
| 102 | + cnt = sum(apple) |
| 103 | + ans = 0 |
| 104 | + for t in sorted(capacity, reverse=True): |
| 105 | + cnt -= t |
| 106 | + ans += 1 |
| 107 | + if cnt <= 0: |
| 108 | + return ans |
| 109 | +
|
| 110 | +
|
| 111 | +# if __name__ == "__main__": |
| 112 | +# sol = Solution() |
| 113 | +# print(sol.minimumBoxes([1,3,2], [4,3,1,5,2])) |
| 114 | +``` |
| 115 | + |
| 116 | +#### Java |
| 117 | + |
| 118 | +```java |
| 119 | +/* |
| 120 | + * @LastEditTime: 2025-12-24 18:35:17 |
| 121 | + */ |
| 122 | +import java.util.Arrays; |
| 123 | + |
| 124 | +class Solution { |
| 125 | + public int minimumBoxes(int[] apple, int[] capacity) { |
| 126 | + int cnt = 0; |
| 127 | + for (int t : apple) { |
| 128 | + cnt += t; |
| 129 | + } |
| 130 | + Arrays.sort(capacity); |
| 131 | + int ans = 0; |
| 132 | + for (int i = capacity.length - 1; i >= 0; i--) { |
| 133 | + cnt -= capacity[i]; |
| 134 | + ans++; |
| 135 | + if (cnt <= 0) { |
| 136 | + return ans; |
| 137 | + } |
| 138 | + } |
| 139 | + return -1; // FAKE RETURN |
| 140 | + } |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +#### Go |
| 145 | + |
| 146 | +```go |
| 147 | +/* |
| 148 | + * @LastEditTime: 2025-12-24 13:45:02 |
| 149 | + */ |
| 150 | +package main |
| 151 | + |
| 152 | +import "sort" |
| 153 | + |
| 154 | +func minimumBoxes(apple []int, capacity []int) int { |
| 155 | + cnt := 0 |
| 156 | + for _, t := range apple { |
| 157 | + cnt += t |
| 158 | + } |
| 159 | + sort.Sort(sort.Reverse(sort.IntSlice(capacity))) |
| 160 | + for i, t := range capacity { |
| 161 | + cnt -= t |
| 162 | + if cnt <= 0 { |
| 163 | + return i + 1 |
| 164 | + } |
| 165 | + } |
| 166 | + return -1 // Fake Return |
| 167 | +} |
| 168 | +``` |
| 169 | + |
| 170 | +#### Rust |
| 171 | + |
| 172 | +```rust |
| 173 | +/* |
| 174 | + * @LastEditTime: 2025-12-24 18:47:13 |
| 175 | + */ |
| 176 | +impl Solution { |
| 177 | + pub fn minimum_boxes(apple: Vec<i32>, mut capacity: Vec<i32>) -> i32 { |
| 178 | + let mut cnt: i32 = 0; |
| 179 | + for t in apple.iter() { |
| 180 | + cnt += t; |
| 181 | + } |
| 182 | + capacity.sort_by(|a, b| b.cmp(a)); |
| 183 | + |
| 184 | + let mut ans: i32 = 0; |
| 185 | + for t in capacity.iter() { |
| 186 | + cnt -= t; |
| 187 | + ans += 1; |
| 188 | + if cnt <= 0 { |
| 189 | + return ans |
| 190 | + } |
| 191 | + } |
| 192 | + ans |
| 193 | + } |
| 194 | +} |
| 195 | +``` |
| 196 | + |
| 197 | +## End |
| 198 | + |
| 199 | +Merry Christmas? |
| 200 | + |
| 201 | +> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/156239770)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/12/24/LeetCode%203074.%E9%87%8D%E6%96%B0%E5%88%86%E8%A3%85%E8%8B%B9%E6%9E%9C/)哦~ |
| 202 | +> |
| 203 | +> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode) |
0 commit comments