---
# System prepended metadata

title: 0453. Minimum Moves to Equal Array Elements
tags: [Leetcode, Medium, Math]

---

# 0453. Minimum Moves to Equal Array Elements
###### tags: `Leetcode` `Medium` `Math`
Link: https://leetcode.com/problems/minimum-moves-to-equal-array-elements/description/
## 思路
每次move n-1个element都增加1
相当于每次move 一个element减少1
所以我们希望所有的element都减少到和最小的element一样大
因此答案就是```nums[0]-min+nums[1]-min+...+nums[n-1]-min = sum-n*min```
## Code
```java=
class Solution {
    public int minMoves(int[] nums) {
        int n = nums.length;
        int sum = 0;
        int min = Integer.MAX_VALUE;
        for(int i=0; i<n; i++){
            sum += nums[i];
            min = Math.min(min, nums[i]);
        }
        return sum-n*min;
    }
}
```
```python=
class Solution:
    def minMoves(self, nums: List[int]) -> int:
        s = sum(nums)
        m = min(nums)
        n = len(nums)
        return s-m*n
```