Created
September 6, 2017 15:42
-
-
Save cixuuz/7a363d3a2eacd1f4dc9387ef8c4c4a3f to your computer and use it in GitHub Desktop.
[517. Super Washing Machines] #leetcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class 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, cnt = 0, 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment