Super Washing Machines

You have n super washing machines on a line. Initially, each washing machine has some dresses or is empty.

For each move, you could choose any m (1 <= m <= n) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time.

Given an integer array machines representing the number of dresses in each washing machine from left to right on the line, return the minimum number of moves to make all the washing machines have the same number of dresses. If it is not possible to do it, return -1.

Example 1
Inputmachines = [1,0,5]
Output3
In three moves, dresses can be passed left from the rightmost machine until all machines have 2 dresses.
Example 2
Inputmachines = [0,3,0]
Output2
In two moves, the middle machine can pass one dress left and then one dress right so every machine has 1 dress.

Constraints

  • n == machines.length
  • 1 <= n <= 10^4
  • 0 <= machines[i] <= 10^5

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate