Skip to content

Latest commit

 

History

History
30 lines (21 loc) · 514 Bytes

462.md

File metadata and controls

30 lines (21 loc) · 514 Bytes

Minimum Moves to Equal Array Elements II

Description

link


Solution

  • Math Method

Code

class Solution:
    def minMoves2(self, nums: List[int]) -> int:
        nums_sorted = sorted(nums)
        mid = len(nums) // 2
        midnum = nums_sorted[mid]
        res = 0
        for num in nums_sorted:
            res += abs(num - midnum)
        res = int(res)
        return res