Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ tags:

为了使得需要的箱子数量尽可能少,我们应该优先使用容量大的箱子。因此,我们可以对箱子按照容量从大到小排序,然后依次使用箱子,直到所有的苹果都被分装完,返回此时使用的箱子数量。

时间复杂度 $O(m \times \log m + n)$,空间复杂度 $O(\log m)$。其中 $m$ 和 $n$ 分别是数组 `capacity``apple` 的长度。
时间复杂度 $O(m \times \log m + n)$,空间复杂度 $O(\log m)$。其中 $m$ 和 $n$ 分别是数组 $\textit{capacity}$$\textit{apple}$ 的长度。

<!-- tabs:start -->

Expand Down Expand Up @@ -156,6 +156,28 @@ function minimumBoxes(apple: number[], capacity: number[]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn minimum_boxes(apple: Vec<i32>, mut capacity: Vec<i32>) -> i32 {
capacity.sort();

let mut s: i32 = apple.iter().sum();

let n = capacity.len();
let mut i = 1;
loop {
s -= capacity[n - i];
if s <= 0 {
return i as i32;
}
i += 1;
}
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ It is possible to distribute the apples as the total capacity is greater than or

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.

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.
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 $\textit{capacity}$ and $\textit{apple}$ respectively.

<!-- tabs:start -->

Expand Down Expand Up @@ -154,6 +154,28 @@ function minimumBoxes(apple: number[], capacity: number[]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn minimum_boxes(apple: Vec<i32>, mut capacity: Vec<i32>) -> i32 {
capacity.sort();

let mut s: i32 = apple.iter().sum();

let n = capacity.len();
let mut i = 1;
loop {
s -= capacity[n - i];
if s <= 0 {
return i as i32;
}
i += 1;
}
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
impl Solution {
pub fn minimum_boxes(apple: Vec<i32>, mut capacity: Vec<i32>) -> i32 {
capacity.sort();

let mut s: i32 = apple.iter().sum();

let n = capacity.len();
let mut i = 1;
loop {
s -= capacity[n - i];
if s <= 0 {
return i as i32;
}
i += 1;
}
}
}