forked from tangorishi/learnJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuperWashingMachines.java
More file actions
32 lines (26 loc) · 900 Bytes
/
SuperWashingMachines.java
File metadata and controls
32 lines (26 loc) · 900 Bytes
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
27
28
29
30
31
32
public class WashingMachine {
public static int minMovesToBalance(int[] machines) {
int totalDresses = 0;
int numMachines = machines.length;
for (int i = 0; i < numMachines; i++) {
totalDresses += machines[i];
}
if (totalDresses % numMachines != 0) {
return -1;
}
int targetDresses = totalDresses / numMachines;
int maxBalance = 0;
int runningBalance = 0;
for (int i = 0; i < numMachines; i++) {
int balance = machines[i] - targetDresses;
runningBalance += balance;
maxBalance = Math.max(maxBalance, Math.abs(runningBalance));
}
return maxBalance;
}
public static void main(String[] args) {
int[] machines1 = {1, 0, 5};
int result1 = minMovesToBalance(machines1);
System.out.println(result1);
}
}