Skip to content

Commit 4de061f

Browse files
authored
feat: add weekly contest 388 (doocs#2428)
1 parent 9e59b27 commit 4de061f

File tree

23 files changed

+978
-3
lines changed

23 files changed

+978
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# [3074. 重新分装苹果](https://leetcode.cn/problems/apple-redistribution-into-boxes)
2+
3+
[English Version](/solution/3000-3099/3074.Apple%20Redistribution%20into%20Boxes/README_EN.md)
4+
5+
<!-- tags: -->
6+
7+
## 题目描述
8+
9+
<!-- 这里写题目描述 -->
10+
11+
<p>给你一个长度为 <code>n</code> 的数组 <code>apple</code> 和另一个长度为 <code>m</code> 的数组 <code>capacity</code> 。</p>
12+
13+
<p>一共有 <code>n</code> 个包裹,其中第 <code>i</code> 个包裹中装着 <code>apple[i]</code> 个苹果。同时,还有 <code>m</code> 个箱子,第 <code>i</code> 个箱子的容量为 <code>capacity[i]</code> 个苹果。</p>
14+
15+
<p>请你选择一些箱子来将这 <code>n</code> 个包裹中的苹果重新分装到箱子中,返回你需要选择的箱子的<strong> 最小</strong> 数量。</p>
16+
17+
<p><strong>注意</strong>,同一个包裹中的苹果可以分装到不同的箱子中。</p>
18+
19+
<p>&nbsp;</p>
20+
21+
<p><strong class="example">示例 1:</strong></p>
22+
23+
<pre>
24+
<strong>输入:</strong>apple = [1,3,2], capacity = [4,3,1,5,2]
25+
<strong>输出:</strong>2
26+
<strong>解释:</strong>使用容量为 4 和 5 的箱子。
27+
总容量大于或等于苹果的总数,所以可以完成重新分装。
28+
</pre>
29+
30+
<p><strong class="example">示例 2:</strong></p>
31+
32+
<pre>
33+
<strong>输入:</strong>apple = [5,5,5], capacity = [2,4,2,7]
34+
<strong>输出:</strong>4
35+
<strong>解释:</strong>需要使用所有箱子。
36+
</pre>
37+
38+
<p>&nbsp;</p>
39+
40+
<p><strong>提示:</strong></p>
41+
42+
<ul>
43+
<li><code>1 &lt;= n == apple.length &lt;= 50</code></li>
44+
<li><code>1 &lt;= m == capacity.length &lt;= 50</code></li>
45+
<li><code>1 &lt;= apple[i], capacity[i] &lt;= 50</code></li>
46+
<li>输入数据保证可以将包裹中的苹果重新分装到箱子中。</li>
47+
</ul>
48+
49+
## 解法
50+
51+
### 方法一:贪心 + 排序
52+
53+
为了使得需要的箱子数量尽可能少,我们应该优先使用容量大的箱子。因此,我们可以对箱子按照容量从大到小排序,然后依次使用箱子,直到所有的苹果都被分装完,返回此时使用的箱子数量。
54+
55+
时间复杂度 $O(m \times \log m + n)$,空间复杂度 $O(\log m)$。其中 $m$ 和 $n$ 分别是数组 `capacity``apple` 的长度。
56+
57+
<!-- tabs:start -->
58+
59+
```python
60+
class Solution:
61+
def minimumBoxes(self, apple: List[int], capacity: List[int]) -> int:
62+
capacity.sort(reverse=True)
63+
s = sum(apple)
64+
for i, c in enumerate(capacity, 1):
65+
s -= c
66+
if s <= 0:
67+
return i
68+
```
69+
70+
```java
71+
class Solution {
72+
public int minimumBoxes(int[] apple, int[] capacity) {
73+
Arrays.sort(capacity);
74+
int s = 0;
75+
for (int x : apple) {
76+
s += x;
77+
}
78+
for (int i = 1, n = capacity.length;; ++i) {
79+
s -= capacity[n - i];
80+
if (s <= 0) {
81+
return i;
82+
}
83+
}
84+
}
85+
}
86+
```
87+
88+
```cpp
89+
class Solution {
90+
public:
91+
int minimumBoxes(vector<int>& apple, vector<int>& capacity) {
92+
sort(capacity.rbegin(), capacity.rend());
93+
int s = accumulate(apple.begin(), apple.end(), 0);
94+
for (int i = 1;; ++i) {
95+
s -= capacity[i - 1];
96+
if (s <= 0) {
97+
return i;
98+
}
99+
}
100+
}
101+
};
102+
```
103+
104+
```go
105+
func minimumBoxes(apple []int, capacity []int) int {
106+
sort.Ints(capacity)
107+
s := 0
108+
for _, x := range apple {
109+
s += x
110+
}
111+
for i := 1; ; i++ {
112+
s -= capacity[len(capacity)-i]
113+
if s <= 0 {
114+
return i
115+
}
116+
}
117+
}
118+
```
119+
120+
```ts
121+
function minimumBoxes(apple: number[], capacity: number[]): number {
122+
capacity.sort((a, b) => b - a);
123+
let s = apple.reduce((acc, cur) => acc + cur, 0);
124+
for (let i = 1; ; ++i) {
125+
s -= capacity[i - 1];
126+
if (s <= 0) {
127+
return i;
128+
}
129+
}
130+
}
131+
```
132+
133+
<!-- tabs:end -->
134+
135+
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# [3074. Apple Redistribution into Boxes](https://leetcode.com/problems/apple-redistribution-into-boxes)
2+
3+
[中文文档](/solution/3000-3099/3074.Apple%20Redistribution%20into%20Boxes/README.md)
4+
5+
<!-- tags: -->
6+
7+
## Description
8+
9+
<p>You are given an array <code>apple</code> of size <code>n</code> and an array <code>capacity</code> of size <code>m</code>.</p>
10+
11+
<p>There are <code>n</code> packs where the <code>i<sup>th</sup></code> pack contains <code>apple[i]</code> apples. There are <code>m</code> boxes as well, and the <code>i<sup>th</sup></code> box has a capacity of <code>capacity[i]</code> apples.</p>
12+
13+
<p>Return <em>the <strong>minimum</strong> number of boxes you need to select to redistribute these </em><code>n</code><em> packs of apples into boxes</em>.</p>
14+
15+
<p><strong>Note</strong> that, apples from the same pack can be distributed into different boxes.</p>
16+
17+
<p>&nbsp;</p>
18+
<p><strong class="example">Example 1:</strong></p>
19+
20+
<pre>
21+
<strong>Input:</strong> apple = [1,3,2], capacity = [4,3,1,5,2]
22+
<strong>Output:</strong> 2
23+
<strong>Explanation:</strong> We will use boxes with capacities 4 and 5.
24+
It is possible to distribute the apples as the total capacity is greater than or equal to the total number of apples.
25+
</pre>
26+
27+
<p><strong class="example">Example 2:</strong></p>
28+
29+
<pre>
30+
<strong>Input:</strong> apple = [5,5,5], capacity = [2,4,2,7]
31+
<strong>Output:</strong> 4
32+
<strong>Explanation:</strong> We will need to use all the boxes.
33+
</pre>
34+
35+
<p>&nbsp;</p>
36+
<p><strong>Constraints:</strong></p>
37+
38+
<ul>
39+
<li><code>1 &lt;= n == apple.length &lt;= 50</code></li>
40+
<li><code>1 &lt;= m == capacity.length &lt;= 50</code></li>
41+
<li><code>1 &lt;= apple[i], capacity[i] &lt;= 50</code></li>
42+
<li>The input is generated such that it&#39;s possible to redistribute packs of apples into boxes.</li>
43+
</ul>
44+
45+
## Solutions
46+
47+
### Solution 1: Greedy + Sorting
48+
49+
To minimize the number of boxes needed, we should prioritize using boxes with larger capacities. Therefore, we can sort the boxes in descending order of capacity, and then use the boxes one by one until all the apples are packed. We return the number of boxes used at this point.
50+
51+
The time complexity is $O(m \times \log m + n)$ and the space complexity is $O(\log m)$, where $m$ and $n$ are the lengths of the arrays `capacity` and `apple` respectively.
52+
53+
<!-- tabs:start -->
54+
55+
```python
56+
class Solution:
57+
def minimumBoxes(self, apple: List[int], capacity: List[int]) -> int:
58+
capacity.sort(reverse=True)
59+
s = sum(apple)
60+
for i, c in enumerate(capacity, 1):
61+
s -= c
62+
if s <= 0:
63+
return i
64+
```
65+
66+
```java
67+
class Solution {
68+
public int minimumBoxes(int[] apple, int[] capacity) {
69+
Arrays.sort(capacity);
70+
int s = 0;
71+
for (int x : apple) {
72+
s += x;
73+
}
74+
for (int i = 1, n = capacity.length;; ++i) {
75+
s -= capacity[n - i];
76+
if (s <= 0) {
77+
return i;
78+
}
79+
}
80+
}
81+
}
82+
```
83+
84+
```cpp
85+
class Solution {
86+
public:
87+
int minimumBoxes(vector<int>& apple, vector<int>& capacity) {
88+
sort(capacity.rbegin(), capacity.rend());
89+
int s = accumulate(apple.begin(), apple.end(), 0);
90+
for (int i = 1;; ++i) {
91+
s -= capacity[i - 1];
92+
if (s <= 0) {
93+
return i;
94+
}
95+
}
96+
}
97+
};
98+
```
99+
100+
```go
101+
func minimumBoxes(apple []int, capacity []int) int {
102+
sort.Ints(capacity)
103+
s := 0
104+
for _, x := range apple {
105+
s += x
106+
}
107+
for i := 1; ; i++ {
108+
s -= capacity[len(capacity)-i]
109+
if s <= 0 {
110+
return i
111+
}
112+
}
113+
}
114+
```
115+
116+
```ts
117+
function minimumBoxes(apple: number[], capacity: number[]): number {
118+
capacity.sort((a, b) => b - a);
119+
let s = apple.reduce((acc, cur) => acc + cur, 0);
120+
for (let i = 1; ; ++i) {
121+
s -= capacity[i - 1];
122+
if (s <= 0) {
123+
return i;
124+
}
125+
}
126+
}
127+
```
128+
129+
<!-- tabs:end -->
130+
131+
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int minimumBoxes(vector<int>& apple, vector<int>& capacity) {
4+
sort(capacity.rbegin(), capacity.rend());
5+
int s = accumulate(apple.begin(), apple.end(), 0);
6+
for (int i = 1;; ++i) {
7+
s -= capacity[i - 1];
8+
if (s <= 0) {
9+
return i;
10+
}
11+
}
12+
}
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func minimumBoxes(apple []int, capacity []int) int {
2+
sort.Ints(capacity)
3+
s := 0
4+
for _, x := range apple {
5+
s += x
6+
}
7+
for i := 1; ; i++ {
8+
s -= capacity[len(capacity)-i]
9+
if s <= 0 {
10+
return i
11+
}
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int minimumBoxes(int[] apple, int[] capacity) {
3+
Arrays.sort(capacity);
4+
int s = 0;
5+
for (int x : apple) {
6+
s += x;
7+
}
8+
for (int i = 1, n = capacity.length;; ++i) {
9+
s -= capacity[n - i];
10+
if (s <= 0) {
11+
return i;
12+
}
13+
}
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def minimumBoxes(self, apple: List[int], capacity: List[int]) -> int:
3+
capacity.sort(reverse=True)
4+
s = sum(apple)
5+
for i, c in enumerate(capacity, 1):
6+
s -= c
7+
if s <= 0:
8+
return i
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function minimumBoxes(apple: number[], capacity: number[]): number {
2+
capacity.sort((a, b) => b - a);
3+
let s = apple.reduce((acc, cur) => acc + cur, 0);
4+
for (let i = 1; ; ++i) {
5+
s -= capacity[i - 1];
6+
if (s <= 0) {
7+
return i;
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)