website/content/ChapterFour/0400~0499/0453.Minimum-Moves-to-Equal-Array-Elements.md
Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1.
Example:
Input:
[1,2,3]
Output:
3
Explanation:
Only three moves are needed (remember each move increments two elements):
[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
给定一个长度为 n 的非空整数数组,找到让数组所有元素相等的最小移动次数。每次移动将会使 n - 1 个元素增加 1。
package leetcode
import "math"
func minMoves(nums []int) int {
sum, min, l := 0, math.MaxInt32, len(nums)
for _, v := range nums {
sum += v
if min > v {
min = v
}
}
return sum - min*l
}