Leetcode 453 Minimum Moves to Equal Array Elements
Kardel Chen
Posted on January 14, 2022
class Solution:
def minMoves(self, nums: List[int]) -> int:
return sum(nums) - len(nums) * min(nums)
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
💖 💪 🙅 🚩
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
githubcopilot AI Innovations at Microsoft Ignite 2024 What You Need to Know (Part 2)
November 29, 2024