Minimum moves to equal array elements
September 1, 2022
math-and-geometryProblem URL: Minimum moves to equal array elements
Elevating n-1 elements is essensially same as decreasing 1 element. Thus we must decrease everything to the minimum value to get the result.
class Solution:
def minMoves(self, nums: List[int]) -> int:
minimum = min(nums)
count = 0
for num in nums:
count += num - minimum
return count
Time Complexity: O(n)
Space Complexity: O(1)