Java | Leetcode Java题解之第517题超级洗衣机
题目:
题解:
class Solution {
public int findMinMoves(int[] machines) {
int tot = Arrays.stream(machines).sum();
int n = machines.length;
if (tot % n != 0) {
return -1;
}
int avg = tot / n;
int ans = 0, sum = 0;
for (int num : machines) {
num -= avg;
sum += num;
ans = Math.max(ans, Math.max(Math.abs(sum), num));
}
return ans;
}
}