g0501_0600.s0517_super_washing_machines.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0501_0600.s0517_super_washing_machines;
// #Hard #Array #Greedy
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;
int cnt = 0;
int max = 0;
for (int load : machines) {
cnt += load - avg;
max = Math.max(Math.max(max, Math.abs(cnt)), load - avg);
}
return max;
}
}