forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_517.java
26 lines (25 loc) · 787 Bytes
/
_517.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.fishercoder.solutions;
public class _517 {
public static class Solution1 {
/**
* 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;
}
}
}