Skip to main content

462 - Minimum Moves to Equal Array Elements II

Details

KeyValue
Linkhttps://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/
LanguagePython 3
Runtime95 ms, faster than 70.68% of Python3 online submissions for Minimum Moves to Equal Array Elements II
Memory Usage15.3 MB, less than 95.33% of Python3 online submissions for Minimum Moves to Equal Array Elements II
DatastructuresList[int]
AlgorithmsMath/Median
ComplexityTime: O(NlogN) Memory: O(1)

Procedure

  1. ...

Code

class Solution:
def minMoves2(self, nums: List[int]) -> int:
nums.sort()
result, median = 0, nums[ len(nums) // 2 ]
for num in nums: result += abs(median - num)
return result