From cb6c43d9db658ed1b71f077c4479a93273c5ee40 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 24 Dec 2025 07:18:42 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3074 --- .../README.md | 24 ++++++++++++++++++- .../README_EN.md | 24 ++++++++++++++++++- .../Solution.rs | 17 +++++++++++++ 3 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 solution/3000-3099/3074.Apple Redistribution into Boxes/Solution.rs diff --git a/solution/3000-3099/3074.Apple Redistribution into Boxes/README.md b/solution/3000-3099/3074.Apple Redistribution into Boxes/README.md index 7590c1bbbe302..6603ae2bb6d31 100644 --- a/solution/3000-3099/3074.Apple Redistribution into Boxes/README.md +++ b/solution/3000-3099/3074.Apple Redistribution into Boxes/README.md @@ -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}$ 的长度。 @@ -156,6 +156,28 @@ function minimumBoxes(apple: number[], capacity: number[]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn minimum_boxes(apple: Vec, mut capacity: Vec) -> 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; + } + } +} +``` + diff --git a/solution/3000-3099/3074.Apple Redistribution into Boxes/README_EN.md b/solution/3000-3099/3074.Apple Redistribution into Boxes/README_EN.md index befb471b5cafc..52de3d0b502a8 100644 --- a/solution/3000-3099/3074.Apple Redistribution into Boxes/README_EN.md +++ b/solution/3000-3099/3074.Apple Redistribution into Boxes/README_EN.md @@ -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. @@ -154,6 +154,28 @@ function minimumBoxes(apple: number[], capacity: number[]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn minimum_boxes(apple: Vec, mut capacity: Vec) -> 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; + } + } +} +``` + diff --git a/solution/3000-3099/3074.Apple Redistribution into Boxes/Solution.rs b/solution/3000-3099/3074.Apple Redistribution into Boxes/Solution.rs new file mode 100644 index 0000000000000..95f2c09ff3b72 --- /dev/null +++ b/solution/3000-3099/3074.Apple Redistribution into Boxes/Solution.rs @@ -0,0 +1,17 @@ +impl Solution { + pub fn minimum_boxes(apple: Vec, mut capacity: Vec) -> 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; + } + } +}