Class Solution

java.lang.Object
g0501_0600.s0517_super_washing_machines.Solution

public class Solution extends Object
517 - Super Washing Machines.<p>Hard</p> <p>You have <code>n</code> super washing machines on a line. Initially, each washing machine has some dresses or is empty.</p> <p>For each move, you could choose any <code>m</code> (<code>1 <= m <= n</code>) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time.</p> <p>Given an integer array <code>machines</code> representing the number of dresses in each washing machine from left to right on the line, return <em>the minimum number of moves to make all the washing machines have the same number of dresses</em>. If it is not possible to do it, return <code>-1</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> machines = [1,0,5]</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> 1st move: 1 0 <&ndash; 5 => 1 1 4 2nd move: 1 <&ndash; 1 <&ndash; 4 => 2 1 3 3rd move: 2 1 <&ndash; 3 => 2 2 2</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> machines = [0,3,0]</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> 1st move: 0 <&ndash; 3 0 => 1 2 0 2nd move: 1 2 &ndash;> 0 => 1 1 1</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> machines = [0,2,0]</p> <p><strong>Output:</strong> -1</p> <p><strong>Explanation:</strong> It&rsquo;s impossible to make all three washing machines have the same number of dresses.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == machines.length</code></li> <li><code>1 <= n <= 10<sup>4</sup></code></li> <li><code>0 <= machines[i] <= 10<sup>5</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • findMinMoves

      public int findMinMoves(int[] machines)