Problem#
You have n
super washing machines on a line. Initially, each washing machine has some dresses or is empty.
For each move, you could choose any m
(1 <= m <= n
) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time.
Given an integer array machines
representing the number of dresses in each washing machine from left to right on the line, return the minimum number of moves to make all the washing machines have the same number of dresses. If it is not possible to do it, return -1
.
Examples#
Example 1#
1
2
3
4
5
6
7
8
9
10
|
Input: machines = [1,0,5]
Output: 3
Explanation:
1st move: 1 0 <-- 5 => 1 1 4
2nd move: 1 <-- 1 <-- 4 => 2 1 3
3rd move: 2 1 <-- 3 => 2 2 2
|
Example 2#
1
2
3
4
5
6
7
8
9
|
Input: machines = [0,3,0]
Output: 2
Explanation:
1st move: 0 <-- 3 0 => 1 2 0
2nd move: 1 2 --> 0 => 1 1 1
|
Example 3#
1
2
3
4
5
6
7
8
|
Input: machines = [0,2,0]
Output: -1
Explanation:
It's impossible to make all three washing machines have the same number of dresses.
|
Constraints#
n == machines.length
1 <= n <= 10^4
0 <= machines[i] <= 10^5
Solution#
Approach#
If the total number of dresses is not divisible by the number of machines, it’s impossible. Otherwise, for each machine, compute the running sum of the difference from the target. The answer is the maximum of the absolute value of the running sum and the local difference at each machine.
Code#
C++#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
class Solution {
public:
int findMinMoves(vector<int>& machines) {
int n = machines.size(), total = 0;
for (int x : machines) total += x;
if (total % n != 0) return -1;
int avg = total / n, res = 0, sum = 0;
for (int x : machines) {
int diff = x - avg;
sum += diff;
res = max(res, max(abs(sum), diff));
}
return res;
}
};
|
Java#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
class Solution {
public int findMinMoves(int[] machines) {
int n = machines.length, total = 0;
for (int x : machines) total += x;
if (total % n != 0) return -1;
int avg = total / n, res = 0, sum = 0;
for (int x : machines) {
int diff = x - avg;
sum += diff;
res = Math.max(res, Math.max(Math.abs(sum), diff));
}
return res;
}
}
|
Kotlin#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
class Solution {
fun findMinMoves(machines: IntArray): Int {
val n = machines.size
val total = machines.sum()
if (total % n != 0) return -1
val avg = total / n
var res = 0
var sum = 0
for (x in machines) {
val diff = x - avg
sum += diff
res = maxOf(res, kotlin.math.abs(sum), diff)
}
return res
}
}
|
Python#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
class Solution:
def findMinMoves(self, machines: list[int]) -> int:
n = len(machines)
total = sum(machines)
if total % n != 0:
return -1
avg = total // n
res = 0
s = 0
for x in machines:
diff = x - avg
s += diff
res = max(res, abs(s), diff)
return res
|
Rust#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
impl Solution {
pub fn find_min_moves(machines: Vec<i32>) -> i32 {
let n = machines.len() as i32;
let total: i32 = machines.iter().sum();
if total % n != 0 { return -1; }
let avg = total / n;
let mut res = 0;
let mut sum = 0;
for &x in &machines {
let diff = x - avg;
sum += diff;
res = res.max(sum.abs()).max(diff);
}
res
}
}
|
TypeScript#
1
2
3
4
5
6
7
8
9
10
11
12
13
|
function findMinMoves(machines: number[]): number {
const n = machines.length;
const total = machines.reduce((a, b) => a + b, 0);
if (total % n !== 0) return -1;
const avg = Math.floor(total / n);
let res = 0, sum = 0;
for (const x of machines) {
const diff = x - avg;
sum += diff;
res = Math.max(res, Math.abs(sum), diff);
}
return res;
}
|
Explanation#
We check if balancing is possible. Then, for each machine, we track the running sum of differences and the local difference. The answer is the maximum of these values.
Complexity#
- ⏰ Time complexity:
O(n)
- 🧺 Space complexity:
O(1)