File tree 3 files changed +151
-0
lines changed
solution/2400-2499/2448.Minimum Cost to Make Array Equal
3 files changed +151
-0
lines changed Original file line number Diff line number Diff line change @@ -248,6 +248,58 @@ public:
248
248
};
249
249
```
250
250
251
+ ### ** Rust**
252
+
253
+ ``` rust
254
+ impl Solution {
255
+ #[allow(dead_code)]
256
+ pub fn min_cost (nums : Vec <i32 >, cost : Vec <i32 >) -> i64 {
257
+ let mut zip_vec : Vec <_ > = nums . into_iter (). zip (cost . into_iter ()). collect ();
258
+
259
+ // Sort the zip vector based on nums
260
+ zip_vec . sort_by (| lhs , rhs | {
261
+ lhs . 0. cmp (& rhs . 0 )
262
+ });
263
+
264
+ let (nums , cost ): (Vec <i32 >, Vec <i32 >) = zip_vec . into_iter (). unzip ();
265
+
266
+ let mut sum : i64 = 0 ;
267
+ for & c in & cost {
268
+ sum += c as i64 ;
269
+ }
270
+ let middle_cost = (sum + 1 ) / 2 ;
271
+ let mut cur_sum : i64 = 0 ;
272
+ let mut i = 0 ;
273
+ let n = nums . len ();
274
+
275
+ while i < n {
276
+ if cost [i ] as i64 + cur_sum >= middle_cost {
277
+ break ;
278
+ }
279
+ cur_sum += cost [i ] as i64 ;
280
+ i += 1 ;
281
+ }
282
+
283
+ Self :: compute_manhattan_dis (& nums , & cost , nums [i ])
284
+ }
285
+
286
+ #[allow(dead_code)]
287
+ fn compute_manhattan_dis (v : & Vec <i32 >, c : & Vec <i32 >, e : i32 ) -> i64 {
288
+ let mut ret = 0 ;
289
+ let n = v . len ();
290
+
291
+ for i in 0 .. n {
292
+ if v [i ] == e {
293
+ continue ;
294
+ }
295
+ ret += (v [i ] - e ). abs () as i64 * c [i ] as i64 ;
296
+ }
297
+
298
+ ret
299
+ }
300
+ }
301
+ ```
302
+
251
303
### ** Go**
252
304
253
305
``` go
Original file line number Diff line number Diff line change @@ -206,6 +206,58 @@ public:
206
206
};
207
207
```
208
208
209
+ ### ** Rust**
210
+
211
+ ``` rust
212
+ impl Solution {
213
+ #[allow(dead_code)]
214
+ pub fn min_cost (nums : Vec <i32 >, cost : Vec <i32 >) -> i64 {
215
+ let mut zip_vec : Vec <_ > = nums . into_iter (). zip (cost . into_iter ()). collect ();
216
+
217
+ // Sort the zip vector based on nums
218
+ zip_vec . sort_by (| lhs , rhs | {
219
+ lhs . 0. cmp (& rhs . 0 )
220
+ });
221
+
222
+ let (nums , cost ): (Vec <i32 >, Vec <i32 >) = zip_vec . into_iter (). unzip ();
223
+
224
+ let mut sum : i64 = 0 ;
225
+ for & c in & cost {
226
+ sum += c as i64 ;
227
+ }
228
+ let middle_cost = (sum + 1 ) / 2 ;
229
+ let mut cur_sum : i64 = 0 ;
230
+ let mut i = 0 ;
231
+ let n = nums . len ();
232
+
233
+ while i < n {
234
+ if cost [i ] as i64 + cur_sum >= middle_cost {
235
+ break ;
236
+ }
237
+ cur_sum += cost [i ] as i64 ;
238
+ i += 1 ;
239
+ }
240
+
241
+ Self :: compute_manhattan_dis (& nums , & cost , nums [i ])
242
+ }
243
+
244
+ #[allow(dead_code)]
245
+ fn compute_manhattan_dis (v : & Vec <i32 >, c : & Vec <i32 >, e : i32 ) -> i64 {
246
+ let mut ret = 0 ;
247
+ let n = v . len ();
248
+
249
+ for i in 0 .. n {
250
+ if v [i ] == e {
251
+ continue ;
252
+ }
253
+ ret += (v [i ] - e ). abs () as i64 * c [i ] as i64 ;
254
+ }
255
+
256
+ ret
257
+ }
258
+ }
259
+ ```
260
+
209
261
### ** Go**
210
262
211
263
``` go
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ #[ allow( dead_code) ]
3
+ pub fn min_cost ( nums : Vec < i32 > , cost : Vec < i32 > ) -> i64 {
4
+ let mut zip_vec: Vec < _ > = nums. into_iter ( ) . zip ( cost. into_iter ( ) ) . collect ( ) ;
5
+
6
+ // Sort the zip vector based on nums
7
+ zip_vec. sort_by ( |lhs, rhs| {
8
+ lhs. 0 . cmp ( & rhs. 0 )
9
+ } ) ;
10
+
11
+ let ( nums, cost) : ( Vec < i32 > , Vec < i32 > ) = zip_vec. into_iter ( ) . unzip ( ) ;
12
+
13
+ let mut sum: i64 = 0 ;
14
+ for & c in & cost {
15
+ sum += c as i64 ;
16
+ }
17
+ let middle_cost = ( sum + 1 ) / 2 ;
18
+ let mut cur_sum: i64 = 0 ;
19
+ let mut i = 0 ;
20
+ let n = nums. len ( ) ;
21
+
22
+ while i < n {
23
+ if cost[ i] as i64 + cur_sum >= middle_cost {
24
+ break ;
25
+ }
26
+ cur_sum += cost[ i] as i64 ;
27
+ i += 1 ;
28
+ }
29
+
30
+ Self :: compute_manhattan_dis ( & nums, & cost, nums[ i] )
31
+ }
32
+
33
+ #[ allow( dead_code) ]
34
+ fn compute_manhattan_dis ( v : & Vec < i32 > , c : & Vec < i32 > , e : i32 ) -> i64 {
35
+ let mut ret = 0 ;
36
+ let n = v. len ( ) ;
37
+
38
+ for i in 0 ..n {
39
+ if v[ i] == e {
40
+ continue ;
41
+ }
42
+ ret += ( v[ i] - e) . abs ( ) as i64 * c[ i] as i64 ;
43
+ }
44
+
45
+ ret
46
+ }
47
+ }
You can’t perform that action at this time.
0 commit comments