Leetcode 453 Minimum Moves to Equal Array Elements

kardelchen

Kardel Chen

Posted on January 14, 2022

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

💖 💪 🙅 🚩
kardelchen
Kardel Chen

Posted on January 14, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related