Skip to content

Commit dedb92e

Browse files
committed
update: 添加问题“3074.重新分装苹果”的代码和题解 (#1278)
3074: AC.cpp (#1277) cpp - AC,100.00%,40.99% py - AC,100.00%,95.91% go - AC,100.00%,29.55% java - AC,39.52%,43.15% rust - AC,100.00%,11.11% Signed-off-by: LetMeFly666 <[email protected]>
1 parent 2577f49 commit dedb92e

15 files changed

+523
-56
lines changed

.commitmsg

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
2054: AC.cpp (#1275) - AC,85.19%,53.70%
1+
3074: AC.cpp (#1277)
2+
cpp - AC,100.00%,40.99%
3+
py - AC,100.00%,95.91%
4+
go - AC,100.00%,29.55%
5+
java - AC,39.52%,43.15%
6+
rust - AC,100.00%,11.11%
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-12-24 13:30:54
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-12-24 13:32:26
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int minimumBoxes(vector<int>& apple, vector<int>& capacity) {
14+
sort(capacity.begin(), capacity.end(), greater<>());
15+
int cnt = 0;
16+
for (int t : apple) {
17+
cnt += t;
18+
}
19+
int ans = 0;
20+
for (int t : capacity) {
21+
cnt -= t;
22+
ans++;
23+
if (cnt <= 0) {
24+
return ans;
25+
}
26+
}
27+
return -1; // Fake Return
28+
}
29+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-12-24 13:30:54
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-12-24 13:45:02
6+
*/
7+
package main
8+
9+
import "sort"
10+
11+
func minimumBoxes(apple []int, capacity []int) int {
12+
cnt := 0
13+
for _, t := range apple {
14+
cnt += t
15+
}
16+
sort.Sort(sort.Reverse(sort.IntSlice(capacity)))
17+
for i, t := range capacity {
18+
cnt -= t
19+
if cnt <= 0 {
20+
return i + 1
21+
}
22+
}
23+
return -1 // Fake Return
24+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-12-24 13:30:54
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-12-24 18:35:17
6+
*/
7+
import java.util.Arrays;
8+
9+
class Solution {
10+
public int minimumBoxes(int[] apple, int[] capacity) {
11+
int cnt = 0;
12+
for (int t : apple) {
13+
cnt += t;
14+
}
15+
Arrays.sort(capacity);
16+
int ans = 0;
17+
for (int i = capacity.length - 1; i >= 0; i--) {
18+
cnt -= capacity[i];
19+
ans++;
20+
if (cnt <= 0) {
21+
return ans;
22+
}
23+
}
24+
return -1; // FAKE RETURN
25+
}
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-12-24 13:30:54
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-12-24 13:40:50
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def minimumBoxes(self, apple: List[int], capacity: List[int]) -> int:
11+
cnt = sum(apple)
12+
ans = 0
13+
for t in sorted(capacity, reverse=True):
14+
cnt -= t
15+
ans += 1
16+
if cnt <= 0:
17+
return ans
18+
19+
20+
# if __name__ == "__main__":
21+
# sol = Solution()
22+
# print(sol.minimumBoxes([1,3,2], [4,3,1,5,2]))
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-12-24 13:30:54
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-12-24 18:47:13
6+
*/
7+
impl Solution {
8+
pub fn minimum_boxes(apple: Vec<i32>, mut capacity: Vec<i32>) -> i32 {
9+
let mut cnt: i32 = 0;
10+
for t in apple.iter() {
11+
cnt += t;
12+
}
13+
capacity.sort_by(|a, b| b.cmp(a));
14+
15+
let mut ans: i32 = 0;
16+
for t in capacity.iter() {
17+
cnt -= t;
18+
ans += 1;
19+
if cnt <= 0 {
20+
return ans
21+
}
22+
}
23+
ans
24+
}
25+
}

Codes/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
* @LastEditTime: 2025-11-01 22:18:35
66
*/
77
pub struct Solution;
8-
include!("0960-delete-columns-to-make-sorted-iii.rs"); // 这个fileName是会被脚本替换掉的
8+
include!("3074-apple-redistribution-into-boxes.rs"); // 这个fileName是会被脚本替换掉的

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: LetMeFly
33
* @Date: 2022-05-19 18:48:53
44
* @LastEditors: LetMeFly.xyz
5-
* @LastEditTime: 2025-10-27 10:03:05
5+
* @LastEditTime: 2025-12-24 10:04:41
66
-->
77
# LetLeet Blog
88

@@ -69,6 +69,7 @@
6969
|日记 - Windows远程桌面(RDP, Remote Desktop Protocol)跳坑指北|<a href="https://blog.letmefly.xyz/2024/09/13/Other-DiaryWindowsRDP_RemoteDesktopProtocol_Connection">本平台博客</a>|无|
7070
|分布式操作系统笔记 - 极简极入门级|<a href="https://blog.letmefly.xyz/2023/12/16/Other-Math-GraphTheory-Notes/">本平台博客</a>|无|
7171
|Docker学习笔记 - 极简极入门级|<a href="https://blog.letmefly.xyz/2023/10/07/Other-Docker-Note/">本平台博客</a>|<a href="https://letmefly.blog.csdn.net/article/details/133633479">CSDN博客</a>|
72+
|“豆包聊天搜索” —— 直接在Chrome等浏览器地址栏开启对话|<a href="https://blog.letmefly.xyz/2025/12/24/Other-DoubaoChatSearch-DirectlyStartChatInChromeETC/">本平台博客</a>|<a href="https://letmefly.blog.csdn.net/article/details/156224252">CSDN博客</a>|
7273
|英语学习笔记(自用) - 英语易忘单词等|<a href="https://blog.letmefly.xyz/2024/02/19/Other-English-LearningNotes-SomeWords/">本平台博客</a>|无|
7374
|离别:哪些是在家而不是在学校很方便的事情|<a href="https://blog.letmefly.xyz/2023/02/17/Other-Farewell-WhatIsEasilyDoneAtHomeButNotSchool/">本平台博客</a>|无|
7475
|FFmpeg - 如何在Linux上安装支持CUDA的FFmpeg|<a href="https://blog.letmefly.xyz/2023/04/07/Other-FFmpeg-howToInstallCudableFFmpegOnLinux/">本平台博客</a>|<a href="https://letmefly.blog.csdn.net/article/details/137449955">CSDN博客</a>|
@@ -958,6 +959,7 @@
958959
|3066.超过阈值的最少操作数II|中等|<a href="https://leetcode.cn/problems/minimum-operations-to-exceed-threshold-value-ii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/01/15/LeetCode%203066.%E8%B6%85%E8%BF%87%E9%98%88%E5%80%BC%E7%9A%84%E6%9C%80%E5%B0%91%E6%93%8D%E4%BD%9C%E6%95%B0II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/145160799" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-operations-to-exceed-threshold-value-ii/solutions/3048076/letmefly-mo-ni-yuan-di-jian-dui-o1kong-j-k4rj/" target="_blank">LeetCode题解</a>|
959960
|3067.在带权树网络中统计可连接服务器对数目|中等|<a href="https://leetcode.cn/problems/count-pairs-of-connectable-servers-in-a-weighted-tree-network/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/06/04/LeetCode%203067.%E5%9C%A8%E5%B8%A6%E6%9D%83%E6%A0%91%E7%BD%91%E7%BB%9C%E4%B8%AD%E7%BB%9F%E8%AE%A1%E5%8F%AF%E8%BF%9E%E6%8E%A5%E6%9C%8D%E5%8A%A1%E5%99%A8%E5%AF%B9%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139456087" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-pairs-of-connectable-servers-in-a-weighted-tree-network/solutions/2801125/letmefly-3067zai-dai-quan-shu-wang-luo-z-zf2j/" target="_blank">LeetCode题解</a>|
960961
|3068.最大节点价值之和|困难|<a href="https://leetcode.cn/problems/find-the-maximum-sum-of-node-values/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/05/27/LeetCode%203068.%E6%9C%80%E5%A4%A7%E8%8A%82%E7%82%B9%E4%BB%B7%E5%80%BC%E4%B9%8B%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/148267428" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-maximum-sum-of-node-values/solutions/3687719/letmefly-3068zui-da-jie-dian-jie-zhi-zhi-ixt6/" target="_blank">LeetCode题解</a>|
962+
|3074.重新分装苹果|简单|<a href="https://leetcode.cn/problems/apple-redistribution-into-boxes/" target="_blank">题目地址</a>|<a href="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/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/156239770" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/apple-redistribution-into-boxes/solutions/3864892/letmefly-3074zhong-xin-fen-zhuang-ping-g-99qr/" target="_blank">LeetCode题解</a>|
961963
|3083.字符串及其反转中是否存在同一子字符串|简单|<a href="https://leetcode.cn/problems/existence-of-a-substring-in-a-string-and-its-reverse/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/12/26/LeetCode%203083.%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%8F%8A%E5%85%B6%E5%8F%8D%E8%BD%AC%E4%B8%AD%E6%98%AF%E5%90%A6%E5%AD%98%E5%9C%A8%E5%90%8C%E4%B8%80%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/144746598" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/existence-of-a-substring-in-a-string-and-its-reverse/solutions/3031052/letmefly-3083zi-fu-chuan-ji-qi-fan-zhuan-6mhd/" target="_blank">LeetCode题解</a>|
962964
|3095.或值至少K的最短子数组I|简单|<a href="https://leetcode.cn/problems/shortest-subarray-with-or-at-least-k-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/01/16/LeetCode%203095.%E6%88%96%E5%80%BC%E8%87%B3%E5%B0%91K%E7%9A%84%E6%9C%80%E7%9F%AD%E5%AD%90%E6%95%B0%E7%BB%84I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/145180094" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/shortest-subarray-with-or-at-least-k-i/solutions/3048904/letmefly-3095huo-zhi-zhi-shao-k-de-zui-d-ieip/" target="_blank">LeetCode题解</a>|
963965
|3096.得到更多分数的最少关卡数目|中等|<a href="https://leetcode.cn/problems/minimum-levels-to-gain-more-points/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/07/19/LeetCode%203096.%E5%BE%97%E5%88%B0%E6%9B%B4%E5%A4%9A%E5%88%86%E6%95%B0%E7%9A%84%E6%9C%80%E5%B0%91%E5%85%B3%E5%8D%A1%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/140553266" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-levels-to-gain-more-points/solutions/2849745/letmefly-3096de-dao-geng-duo-fen-shu-de-u53ww/" target="_blank">LeetCode题解</a>|
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
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>&nbsp;</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>&nbsp;</p>
40+
41+
<p><strong>提示:</strong></p>
42+
43+
<ul>
44+
<li><code>1 &lt;= n == apple.length &lt;= 50</code></li>
45+
<li><code>1 &lt;= m == capacity.length &lt;= 50</code></li>
46+
<li><code>1 &lt;= apple[i], capacity[i] &lt;= 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

Comments
 (0)