Skip to content

Latest commit

 

History

History
22 lines (16 loc) · 788 Bytes

453.-minimum-moves-to-equal-array-elements.md

File metadata and controls

22 lines (16 loc) · 788 Bytes

453. Minimum Moves to Equal Array Elements

  • Medium

  • Given an integer array nums of size n, 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 by 1.

Analysis

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