Skip to content

Commit ebab74a

Browse files
committed
feat: add solutions to lc problem: No.3424
No.3424.Minimum Cost to Make Arrays Identical
1 parent 2976139 commit ebab74a

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

solution/3400-3499/3424.Minimum Cost to Make Arrays Identical/README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ tags:
8383

8484
<!-- solution:start -->
8585

86-
### 方法一
86+
### 方法一:贪心 + 排序
87+
88+
如果不允许对数组进行分割,那么我们可以直接计算两个数组的绝对差值之和,作为总代价 $c_1$。如果允许对数组进行分割,那么我们可以将数组 $\textit{arr}$ 分割成 $n$ 个长度为 1 的子数组,然后以任意顺序重新排列,然后与数组 $\textit{brr}$ 进行比较,计算绝对差值之和,作为总代价 $c_2$,要使得 $c_2$ 最小,我们可以将两个数组都排序,然后计算绝对差值之和。最终的结果为 $\min(c_1, c_2 + k)$。
89+
90+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 $\textit{arr}$ 的长度。
8791

8892
<!-- tabs:start -->
8993

@@ -187,6 +191,29 @@ function minCost(arr: number[], brr: number[], k: number): number {
187191
}
188192
```
189193

194+
#### Rust
195+
196+
```rust
197+
impl Solution {
198+
pub fn min_cost(mut arr: Vec<i32>, mut brr: Vec<i32>, k: i64) -> i64 {
199+
let c1: i64 = arr.iter()
200+
.zip(&brr)
201+
.map(|(a, b)| (*a - *b).abs() as i64)
202+
.sum();
203+
204+
arr.sort_unstable();
205+
brr.sort_unstable();
206+
207+
let c2: i64 = k + arr.iter()
208+
.zip(&brr)
209+
.map(|(a, b)| (*a - *b).abs() as i64)
210+
.sum::<i64>();
211+
212+
c1.min(c2)
213+
}
214+
}
215+
```
216+
190217
<!-- tabs:end -->
191218

192219
<!-- solution:end -->

solution/3400-3499/3424.Minimum Cost to Make Arrays Identical/README_EN.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ tags:
7979

8080
<!-- solution:start -->
8181

82-
### Solution 1
82+
### Solution 1: Greedy + Sorting
83+
84+
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)$.
85+
86+
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}$.
8387

8488
<!-- tabs:start -->
8589

@@ -183,6 +187,29 @@ function minCost(arr: number[], brr: number[], k: number): number {
183187
}
184188
```
185189

190+
#### Rust
191+
192+
```rust
193+
impl Solution {
194+
pub fn min_cost(mut arr: Vec<i32>, mut brr: Vec<i32>, k: i64) -> i64 {
195+
let c1: i64 = arr.iter()
196+
.zip(&brr)
197+
.map(|(a, b)| (*a - *b).abs() as i64)
198+
.sum();
199+
200+
arr.sort_unstable();
201+
brr.sort_unstable();
202+
203+
let c2: i64 = k + arr.iter()
204+
.zip(&brr)
205+
.map(|(a, b)| (*a - *b).abs() as i64)
206+
.sum::<i64>();
207+
208+
c1.min(c2)
209+
}
210+
}
211+
```
212+
186213
<!-- tabs:end -->
187214

188215
<!-- solution:end -->
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
impl Solution {
2+
pub fn min_cost(mut arr: Vec<i32>, mut brr: Vec<i32>, k: i64) -> i64 {
3+
let c1: i64 = arr
4+
.iter()
5+
.zip(&brr)
6+
.map(|(a, b)| (*a - *b).abs() as i64)
7+
.sum();
8+
9+
arr.sort_unstable();
10+
brr.sort_unstable();
11+
12+
let c2: i64 = k + arr
13+
.iter()
14+
.zip(&brr)
15+
.map(|(a, b)| (*a - *b).abs() as i64)
16+
.sum::<i64>();
17+
18+
c1.min(c2)
19+
}
20+
}

0 commit comments

Comments
 (0)