Skip to content

feat: add solutions to lc problem: No.3424 #4488

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2025
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 @@ -83,7 +83,11 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:贪心 + 排序

如果不允许对数组进行分割,那么我们可以直接计算两个数组的绝对差值之和,作为总代价 $c_1$。如果允许对数组进行分割,那么我们可以将数组 $\textit{arr}$ 分割成 $n$ 个长度为 1 的子数组,然后以任意顺序重新排列,然后与数组 $\textit{brr}$ 进行比较,计算绝对差值之和,作为总代价 $c_2$,要使得 $c_2$ 最小,我们可以将两个数组都排序,然后计算绝对差值之和。最终的结果为 $\min(c_1, c_2 + k)$。

时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 $\textit{arr}$ 的长度。

<!-- tabs:start -->

Expand Down Expand Up @@ -187,6 +191,29 @@ function minCost(arr: number[], brr: number[], k: number): number {
}
```

#### Rust

```rust
impl Solution {
pub fn min_cost(mut arr: Vec<i32>, mut brr: Vec<i32>, k: i64) -> i64 {
let c1: i64 = arr.iter()
.zip(&brr)
.map(|(a, b)| (*a - *b).abs() as i64)
.sum();

arr.sort_unstable();
brr.sort_unstable();

let c2: i64 = k + arr.iter()
.zip(&brr)
.map(|(a, b)| (*a - *b).abs() as i64)
.sum::<i64>();

c1.min(c2)
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Greedy + Sorting

If splitting the array is not allowed, we can directly calculate the sum of absolute differences between the two arrays as the total cost $c_1$. If splitting is allowed, we can divide the array $\textit{arr}$ into $n$ subarrays of length 1, then rearrange them in any order, and compare with array $\textit{brr}$, calculating the sum of absolute differences as the total cost $c_2$. To minimize $c_2$, we can sort both arrays and then calculate the sum of absolute differences. The final result is $\min(c_1, c_2 + k)$.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$, where $n$ is the length of the array $\textit{arr}$.

<!-- tabs:start -->

Expand Down Expand Up @@ -183,6 +187,29 @@ function minCost(arr: number[], brr: number[], k: number): number {
}
```

#### Rust

```rust
impl Solution {
pub fn min_cost(mut arr: Vec<i32>, mut brr: Vec<i32>, k: i64) -> i64 {
let c1: i64 = arr.iter()
.zip(&brr)
.map(|(a, b)| (*a - *b).abs() as i64)
.sum();

arr.sort_unstable();
brr.sort_unstable();

let c2: i64 = k + arr.iter()
.zip(&brr)
.map(|(a, b)| (*a - *b).abs() as i64)
.sum::<i64>();

c1.min(c2)
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
impl Solution {
pub fn min_cost(mut arr: Vec<i32>, mut brr: Vec<i32>, k: i64) -> i64 {
let c1: i64 = arr
.iter()
.zip(&brr)
.map(|(a, b)| (*a - *b).abs() as i64)
.sum();

arr.sort_unstable();
brr.sort_unstable();

let c2: i64 = k + arr
.iter()
.zip(&brr)
.map(|(a, b)| (*a - *b).abs() as i64)
.sum::<i64>();

c1.min(c2)
}
}