[
math
array
]
Leetcode 0453 Minimum Moves to Equal Array Elements
Problem statement
https://leetcode.com/problems/minimum-moves-to-equal-array-elements/
Solution
Increasing n-1
numbers by 1
is equivalent decreasing one element by 1
. So, we need to find minimum element and decrease all others so they become equal to minimum.
Complexity
We can write is as oneliner with O(n)
time complexity and O(1)
space complexity.
Code
class Solution:
def minMoves(self, nums):
return sum(nums) - len(nums)*min(nums)