DEV Community

Kardel Chen
Kardel Chen

Posted on

Leetcode 453 Minimum Moves to Equal Array Elements

class Solution:
    def minMoves(self, nums: List[int]) -> int:
        return sum(nums) - len(nums) * min(nums)
Enter fullscreen mode Exit fullscreen mode

To understand this, we just think move is 1. add 1 to every element in the array, 2. minus 1 to a specific element
For example, To remove difference of [99, 98, 97, 96],
we only need to remove difference of [3,2,1,0]
and every move operation is "minus 1 to a specific element"
So to remove the difference, we need 3+2+1 operations

Top comments (0)