Skip to content

Commit 583235d

Browse files
committed
feat: add more
1 parent 92550d7 commit 583235d

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,43 @@
11
// https://leetcode.com/problems/minimum-moves-to-equal-array-elements
2-
//
2+
//
33
// Given an integer array `nums` of size `n`, return _the minimum number of moves required to make all array elements equal_.
4-
//
4+
//
55
// In one move, you can increment `n - 1` elements of the array by `1`.
6-
//
6+
//
77
// **Example 1:**
8-
//
8+
//
99
// ```
1010
// **Input:** nums = [1,2,3]
1111
// **Output:** 3
1212
// **Explanation:** Only three moves are needed (remember each move increments two elements):
1313
// [1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
1414
// ```
15-
//
15+
//
1616
// **Example 2:**
17-
//
17+
//
1818
// ```
1919
// **Input:** nums = [1,1,1]
2020
// **Output:** 0
2121
// ```
22-
//
22+
//
2323
// **Constraints:**
24-
//
24+
//
2525
// * `n == nums.length`
2626
// * `1 <= nums.length <= 10<sup>5</sup>`
2727
// * `-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup>`
2828
// * The answer is guaranteed to fit in a **32-bit** integer.
2929

3030
pub fn min_moves(nums: Vec<i32>) -> i32 {
31-
31+
let mut min = nums[0];
32+
let mut sum = nums[0];
33+
for i in 1..nums.len() {
34+
min = min.min(nums[i]);
35+
sum += nums[i];
3236
}
37+
return sum - min * nums.len() as i32;
38+
}
3339

3440
#[test]
3541
pub fn t1() {
42+
assert_eq!(min_moves(vec![1, 2, 3]), 3);
3643
}

Array/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ mod _0347_top_k_frequent_elements;
1212
mod _0349_intersection_of_two_arrays;
1313
mod _0350_intersection_of_two_arrays_ii;
1414
mod _0448_find_all_numbers_disappeared_in_an_array;
15+
mod _0453_minimum_moves_to_equal_array_elements;
1516
mod _0463_island_perimeter;
1617
mod _0485_max_consecutive_ones;
1718
mod _0495_teemo_attacking;

0 commit comments

Comments
 (0)