diff --git a/solution/3400-3499/3424.Minimum Cost to Make Arrays Identical/README.md b/solution/3400-3499/3424.Minimum Cost to Make Arrays Identical/README.md index 44353cff4388e..7092cd69a46fb 100644 --- a/solution/3400-3499/3424.Minimum Cost to Make Arrays Identical/README.md +++ b/solution/3400-3499/3424.Minimum Cost to Make Arrays Identical/README.md @@ -83,7 +83,11 @@ tags: -### 方法一 +### 方法一:贪心 + 排序 + +如果不允许对数组进行分割,那么我们可以直接计算两个数组的绝对差值之和,作为总代价 $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}$ 的长度。 @@ -187,6 +191,29 @@ function minCost(arr: number[], brr: number[], k: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn min_cost(mut arr: Vec, mut brr: Vec, 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::(); + + c1.min(c2) + } +} +``` + diff --git a/solution/3400-3499/3424.Minimum Cost to Make Arrays Identical/README_EN.md b/solution/3400-3499/3424.Minimum Cost to Make Arrays Identical/README_EN.md index cca1584a56398..7f32f064e77eb 100644 --- a/solution/3400-3499/3424.Minimum Cost to Make Arrays Identical/README_EN.md +++ b/solution/3400-3499/3424.Minimum Cost to Make Arrays Identical/README_EN.md @@ -79,7 +79,11 @@ tags: -### 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}$. @@ -183,6 +187,29 @@ function minCost(arr: number[], brr: number[], k: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn min_cost(mut arr: Vec, mut brr: Vec, 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::(); + + c1.min(c2) + } +} +``` + diff --git a/solution/3400-3499/3424.Minimum Cost to Make Arrays Identical/Solution.rs b/solution/3400-3499/3424.Minimum Cost to Make Arrays Identical/Solution.rs new file mode 100644 index 0000000000000..a6c95d5303b07 --- /dev/null +++ b/solution/3400-3499/3424.Minimum Cost to Make Arrays Identical/Solution.rs @@ -0,0 +1,20 @@ +impl Solution { + pub fn min_cost(mut arr: Vec, mut brr: Vec, 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::(); + + c1.min(c2) + } +}