Skip to content

Commit 7aa1a83

Browse files
committed
minimum_moves_to_equal_array_elements_iii.rs
1 parent c36e24f commit 7aa1a83

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
impl Solution {
2+
pub fn min_moves(nums: Vec<i32>) -> i32 {
3+
let mut ans = 0;
4+
let mut ma = nums[0];
5+
for (i, &n) in nums.iter().enumerate().skip(1) {
6+
if n > ma {
7+
ans += (i as i32) * (n - ma);
8+
ma = n;
9+
} else if n < ma {
10+
ans += ma - n;
11+
}
12+
}
13+
ans
14+
}
15+
}

0 commit comments

Comments
 (0)