#517
Super Washing Machines
master · 1615 · lc hard +32 · verified · 43.9% accepted · 823 likes · top 26%
Description
There are n washing machines in a row. In a single move you may simultaneously pick any subset of machines and have each selected machine transfer exactly one dress to a neighbor. Given an integer array machines indicating the current dress count in each machine (left to right), return the minimum number of moves to equalize all machines. Return -1 if equal distribution is impossible.
Example 1:
Input: machines = [1,0,5]
Output: 3
Explanation:
1st move: 1 0 <-- 5 => 1 1 4
2nd move: 1 <-- 1 <-- 4 => 2 1 3
3rd move: 2 1 <-- 3 => 2 2 2
Example 2:
Input: machines = [0,3,0]
Output: 2
Explanation:
1st move: 0 <-- 3 0 => 1 2 0
2nd move: 1 2 --> 0 => 1 1 1
Example 3:
Input: machines = [0,2,0]
Output: -1
Explanation:
It's impossible to make all three washing machines have the same number of dresses.
Code
1
2
3