462. Minimum Moves to Equal Array Elements II Medium

@problem@discussion
#Array#Math#Sorting



1/**
2 * [462] Minimum Moves to Equal Array Elements II
3 *
4 * Given an integer array nums of size n, return the minimum number of moves required to make all array elements equal.
5 * In one move, you can increment or decrement an element of the array by 1.
6 * Test cases are designed so that the answer will fit in a 32-bit integer.
7 *  
8 * Example 1:
9 * 
10 * Input: nums = [1,2,3]
11 * Output: 2
12 * Explanation:
13 * Only two moves are needed (remember each move increments or decrements one element):
14 * [<u>1</u>,2,3]  =>  [2,2,<u>3</u>]  =>  [2,2,2]
15 * 
16 * Example 2:
17 * 
18 * Input: nums = [1,10,2,9]
19 * Output: 16
20 * 
21 *  
22 * Constraints:
23 * 
24 * 	n == nums.length
25 * 	1 <= nums.length <= 10^5
26 * 	-10^9 <= nums[i] <= 10^9
27 * 
28 */
29pub struct Solution {}
30
31// problem: https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/
32// discuss: https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/discuss/?currentPage=1&orderBy=most_votes&query=
33
34// submission codes start here
35
36impl Solution {
37    pub fn min_moves2(nums: Vec<i32>) -> i32 {
38        0
39    }
40}
41
42// submission codes end
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn test_462() {
50    }
51}
52


Back
© 2025 bowen.ge All Rights Reserved.