3736. Minimum Moves to Equal Array Elements III

1 min read

LeetCode Daily Problem: Minimum Moves to Equal Array Elements III

On this page

Original problem on LeetCode

You are given an integer array nums.

In one move, you may increase the value of any single element nums[i] by 1.

Return the minimum total number of moves required so that all elements in nums become equal.

Constraints:

  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 100

Approach

All elements must reach the maximum, so the total number of moves is the sum of the gaps between each element and the maximum.

python
class Solution:
    def minMoves(self, nums: List[int]) -> int:
        max_num = max(nums)
        return sum((max_num-num for num in nums))

Complexity

  • Time: O(n) — one pass.
  • Space: O(1).
TagsSimulation

Back to all posts