-
Medium
-
Given an integer array
nums
of sizen
, return the minimum number of moves required to make all array elements equal.In one move, you can increment
n - 1
elements of the array by1
.
This problem can be more easily understood if stated the other way around. When we increment n-1 elements by 1, it is the same as decreasing only one element by 1. Therefore we would be decreasing every other element except the minimum one to be equal to the minimum value.
class Solution:
def minMoves(self, nums: List[int]) -> int:
mi = min(nums)
ans = 0
for x in nums:
ans += x - mi
return ans