---
# System prepended metadata

title: \#453 Minimum Moves to Equal Array Elements
tags: [leetcode, math problem, leetcode-easy]

---

# \#453 Minimum Moves to Equal Array Elements
## *給定一數字陣列, 每次可對陣列中的n-1個元素+1(稱為一步), 回傳讓所有元素相等所需的最小步數*
## Log
 - build 20210824 by syhuang

## 參考解
 - 概念:逆向思考, n-1個元素都+1, 等價於最大的元素-1
 - 所有元素最終都等值, 即所有元素都被扣除到最小值的那個元素為止
 - 每次步數為*最大值的元素-1*, 因此總步數就是\[所有元素的總和\]-\[最小元素值\]*\[元素陣列長度\]
 - leetcode submit runtime 67%, memory 80%
```javascript=
var minMoves = function(nums) {
    let sum = 0;
    let min = nums[0];
    let result = 0;
    
    //先找出陣列中最小值&計算sum
    for(let n of nums){
        if(n<min) min = n;
        sum+=n;
    }
    
    //sum扣除最小值*陣列長度, 即為最少的移動步數
    result = sum - min*nums.length;
    
    return result;
};
```
## 備註
## 參考
- [參考解](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/discuss/93817/It-is-a-math-question)
###### tags: `leetcode`, `leetcode-easy`, `math problem`