517. Super Washing Machines
HardSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the minimum number of moves to equalize the number of dresses in a line of washing machines. A "move" consists of multiple simultaneous transfers, where selected machines can pass one dress to an adjacent neighbor. The total number of dresses remains constant.
Solution
Super Washing Machines in Python
class Solution:
def findMinMoves(self, machines: List[int]) -> int:
n = len(machines)
total_dresses = sum(machines)
# If the total number of dresses is not divisible by n, it's impossible.
if total_dresses % n != 0:
return -1
avg = total_dresses // n
max_moves = 0
balance = 0 # Represents the net flow of dresses
for d in machines:
# The number of dresses this machine needs to give (if > 0) or take (if < 0).
diff = d - avg
# The balance is the cumulative sum of differences. It represents the net number
# of dresses that need to move across the right boundary of the current machine.
balance += diff
# The number of moves is limited by two bottlenecks:
# 1. A machine with a large surplus. It needs `diff` moves to offload them.
# (We only care about surplus `diff > 0`, but max handles negative `diff` correctly).
# 2. The total flow of dresses across a boundary. abs(balance) dresses
# must pass the current boundary.
# We take the maximum of the current max_moves, the machine's own surplus,
# and the flow required at its boundary.
max_moves = max(max_moves, diff, abs(balance))
return max_moves
Super Washing Machines in Java
class Solution {
public int findMinMoves(int[] machines) {
int n = machines.length;
long total_dresses = 0;
for (int d : machines) {
total_dresses += d;
}
// If the total number of dresses is not divisible by n, it's impossible.
if (total_dresses % n != 0) {
return -1;
}
int avg = (int) (total_dresses / n);
int max_moves = 0;
int balance = 0; // Represents the net flow of dresses
for (int d : machines) {
// The number of dresses this machine needs to give (if > 0) or take (if < 0).
int diff = d - avg;
// The balance is the cumulative sum of differences. It represents the net number
// of dresses that need to move across the right boundary of the current machine.
balance += diff;
// The number of moves is limited by two bottlenecks:
// 1. A machine with a large surplus. It needs `diff` moves to offload them.
// (We only care about surplus `diff > 0`, but Math.max handles negative `diff` correctly).
// 2. The total flow of dresses across a boundary. `Math.abs(balance)` dresses
// must pass the current boundary.
// We take the maximum of the current max_moves, the machine's own surplus,
// and the flow required at its boundary.
max_moves = Math.max(max_moves, Math.max(diff, Math.abs(balance)));
}
return max_moves;
}
}
Super Washing Machines in C++
class Solution {
public:
int findMinMoves(vector<int>& machines) {
int n = machines.size();
long long total_dresses = 0;
for (int d : machines) {
total_dresses += d;
}
// If the total number of dresses is not divisible by n, it's impossible.
if (total_dresses % n != 0) {
return -1;
}
int avg = total_dresses / n;
int max_moves = 0;
int balance = 0; // Represents the net flow of dresses
for (int d : machines) {
// The number of dresses this machine needs to give (if > 0) or take (if < 0).
int diff = d - avg;
// The balance is the cumulative sum of differences. It represents the net number
// of dresses that need to move across the right boundary of the current machine.
balance += diff;
// The number of moves is limited by two bottlenecks:
// 1. A machine with a large surplus. It needs `diff` moves to offload them.
// (We only care about surplus `diff > 0`, but `max` handles negative `diff` correctly).
// 2. The total flow of dresses across a boundary. `abs(balance)` dresses
// must pass the current boundary.
// We take the maximum of the current max_moves, the machine's own surplus,
// and the flow required at its boundary.
max_moves = max(max_moves, max(diff, abs(balance)));
}
return max_moves;
}
};
Super Washing Machines in TypeScript
function findMinMoves(machines: number[]): number {
const n = machines.length;
let total_dresses = 0;
for (const d of machines) {
total_dresses += d;
}
// If the total number of dresses is not divisible by n, it's impossible.
if (total_dresses % n !== 0) {
return -1;
}
const avg = total_dresses / n;
let max_moves = 0;
let balance = 0; // Represents the net flow of dresses
for (const d of machines) {
// The number of dresses this machine needs to give (if > 0) or take (if < 0).
const diff = d - avg;
// The balance is the cumulative sum of differences. It represents the net number
// of dresses that need to move across the right boundary of the current machine.
balance += diff;
// The number of moves is limited by two bottlenecks:
// 1. A machine with a large surplus. It needs `diff` moves to offload them.
// (We only care about surplus `diff > 0`, but Math.max handles negative `diff` correctly).
// 2. The total flow of dresses across a boundary. Math.abs(balance) dresses
// must pass the current boundary.
// We take the maximum of the current max_moves, the machine's own surplus,
// and the flow required at its boundary.
max_moves = Math.max(max_moves, diff, Math.abs(balance));
}
return max_moves;
};
More Hard LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.