183k views
4 votes
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 representing the number of dresses in each washing machine from left to right on the line, you should find 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.

1 Answer

5 votes

Final answer:

To make all the washing machines have the same number of dresses, find the machine with the most dresses and distribute the dresses evenly. The number of moves needed will be equal to the maximum difference between the number of dresses in each machine.

Step-by-step explanation:

To make all the washing machines have the same number of dresses, we need to find the machine with the most dresses and distribute the dresses evenly among all the machines. The number of moves needed will be equal to the maximum difference between the number of dresses in each machine. Let's say the array representing the number of dresses is [1, 0, 5, 2]. The maximum difference is 5 - 0 = 5, so we need at least 5 moves. We can start by passing one dress from the machine with 5 dresses to the adjacent machine with 0 dresses, resulting in the array [1, 1, 4, 2]. Then we can pass one dress from the machine with 4 dresses to the adjacent machine with 1 dress, resulting in the array [2, 2, 3, 2]. We can continue this process until all the machines have the same number of dresses.

User Woodsman
by
8.3k points