Skip to content

Instantly share code, notes, and snippets.

@cixuuz
Created September 6, 2017 15:42
Show Gist options
  • Save cixuuz/7a363d3a2eacd1f4dc9387ef8c4c4a3f to your computer and use it in GitHub Desktop.
Save cixuuz/7a363d3a2eacd1f4dc9387ef8c4c4a3f to your computer and use it in GitHub Desktop.
[517. Super Washing Machines] #leetcode
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