class Solution:
def findMinMoves(self, machines):
total = sum(machines)
n = len(machines)
if total % n != 0:
return -1
target = total // n
max_moves = 0
curr = 0
for load in machines:
diff = load - target
curr += diff
max_moves = max(max_moves, abs(curr), diff)
return max_moves
class Solution {
public:
int findMinMoves(vector<int>& machines) {
int total = 0, n = machines.size();
for (int x : machines) total += x;
if (total % n != 0) return -1;
int target = total / n;
int res = 0, curr = 0;
for (int i = 0; i < n; ++i) {
int diff = machines[i] - target;
curr += diff;
res = max(res, max(abs(curr), diff));
}
return res;
}
};
class Solution {
public int findMinMoves(int[] machines) {
int total = 0, n = machines.length;
for (int x : machines) total += x;
if (total % n != 0) return -1;
int target = total / n;
int maxMoves = 0, curr = 0;
for (int load : machines) {
int diff = load - target;
curr += diff;
maxMoves = Math.max(maxMoves, Math.max(Math.abs(curr), diff));
}
return maxMoves;
}
}
var findMinMoves = function(machines) {
let total = machines.reduce((a, b) => a + b, 0);
let n = machines.length;
if (total % n !== 0) return -1;
let target = Math.floor(total / n);
let maxMoves = 0, curr = 0;
for (let load of machines) {
let diff = load - target;
curr += diff;
maxMoves = Math.max(maxMoves, Math.abs(curr), diff);
}
return maxMoves;
};
You are given an array machines
where each element represents the number of dresses in a washing machine arranged in a row. In one move, you can choose any subset of machines and transfer one dress from each selected machine to one of its adjacent machines (left or right). The goal is to compute the minimum number of moves required to make every machine have the same number of dresses. If it is impossible, return -1
.
At first glance, one might think of simulating each possible move by shifting dresses left or right, but this quickly becomes inefficient for large arrays. Instead, we need to determine the minimum number of moves in a smarter way.
The key insight is to realize that the total number of dresses must be divisible by the number of machines for an equal distribution to be possible. If it's not, we immediately return -1
.
Next, we observe that the number of moves required at each machine depends on two things:
Let's break down the solution step by step:
machines
. If this sum is not divisible by the number of machines, return -1
as it's impossible to balance.total / n
, where n
is the number of machines.machines[i] - target
).This approach ensures we account for both local and global imbalances, and efficiently computes the minimum moves required.
Let's use the input [1, 0, 5]
:
Now, scan through each machine, tracking the cumulative sum:
The answer is 3. This matches the minimal number of moves needed to balance the machines:
[1,1,4]
)[1,2,3]
)[2,2,2]
)Brute-force Approach:
O(n)
— We iterate through the array once, performing constant-time operations per machine.O(1)
— Only a few variables are used for tracking sums and the result.The optimized approach is highly efficient and scales well even for large input sizes.
The Super Washing Machines problem is elegantly solved by recognizing that the only way to balance the machines is by tracking the local and cumulative imbalances as we scan through the array. Instead of simulating each move, we compute the necessary transfers using a single pass, resulting in an efficient and clear solution. The main insights are checking divisibility for feasibility and using prefix sums to model flow, highlighting how mathematical reasoning leads to a simple, optimal algorithm.