package com.fishercoder.solutions;

/**
 * 517. 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 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.

 Example1

 Input: [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
 Example2

 Input: [0,3,0]

 Output: 2

 Explanation:
 1st move:    0 <-- 3     0    =>    1     2     0
 2nd move:    1     2 --> 0    =>    1     1     1
 Example3

 Input: [0,2,0]

 Output: -1

 Explanation:
 It's impossible to make all the three washing machines have the same number of dresses.
 Note:
 The range of n is [1, 10000].
 The range of dresses number in a super washing machine is [0, 1e5].

 */
public class _517 {
    /**Reference: https://discuss.leetcode.com/topic/79938/super-short-easy-java-o-n-solution*/
    public int findMinMoves(int[] machines) {
        int total = 0;
        for (int i : machines) {
            total += i;
        }
        if (total % machines.length != 0) {
            return -1;
        }
        int avg = total / machines.length;
        int cnt = 0;
        int max = 0;
        for (int load : machines) {
            cnt += load - avg; //load-avg is "gain/lose"
            max = Math.max(Math.max(max, Math.abs(cnt)), load - avg);
        }
        return max;
    }

    public static class Solution2 {
        /**TODO: finish it.*/
        public int findMinMoves(int[] machines) {
            int totalDresses = 0;
            for (int i = 0; i < machines.length; i++) {
                totalDresses += machines[i];
            }
            if (totalDresses / machines.length == 0 || totalDresses % machines.length != 0) {
                return -1;
            }
            return -1;
        }
    }
}

@@ -1262,19 +1262,71 @@
 7 %7B%0A    
-/**
+public static class Solution1 %7B%0A        /**%0A         * 
 Referenc
@@ -1403,19 +1403,33 @@
 solution
-*/%0A
+%0A         */%0A    
     publ
@@ -1466,32 +1466,36 @@
 ines) %7B%0A        
+  
+  
 int total = 0;%0A 
@@ -1489,24 +1489,28 @@
  total = 0;%0A
+    
         for 
@@ -1538,24 +1538,28 @@
             
+   
+ 
 total += i;%0A
@@ -1566,26 +1566,33 @@
         
-%7D%0A
+    %7D%0A   
         
 if (tota
@@ -1575,32 +1575,33 @@
    %7D%0A           
+ 
 if (total %25 mach
@@ -1624,32 +1624,36 @@
 ) %7B%0A            
+    
 return -1;%0A     
@@ -1639,32 +1639,36 @@
      return -1;%0A
+    
         %7D%0A      
@@ -1665,24 +1665,28 @@
   %7D%0A        
+    
 int avg = to
@@ -1708,16 +1708,20 @@
 length;%0A
+    
         
@@ -1737,24 +1737,28 @@
  0;%0A        
+    
 int max = 0;
@@ -1754,24 +1754,28 @@
 nt max = 0;%0A
+    
         for 
@@ -1810,16 +1810,20 @@
         
+ 
+   
 cnt += l
@@ -1859,16 +1859,20 @@
 n/lose%22%0A
+    
         
@@ -1945,458 +1945,36 @@
     
-%7D%0A        return max;%0A
     %7D%0A
-%0A
     
-public static class Solution2 %7B%0A        /**TODO: finish it.*/%0A        public int findMinMoves(int%5B%5D machines) %7B%0A            int totalDresses = 0;%0A            for (int i = 0; i %3C machines.length; i++) %7B%0A                totalDresses += machines%5Bi%5D;%0A            %7D%0A            if (totalDresses / machines.length == 0 %7C%7C totalDresses %25 machines.length != 0) %7B%0A                return -1;%0A            %7D%0A            return -1
+        return max
 ;%0A  

refactor 517