Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
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.
Example 1:
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:
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:
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.length1 <= n <= 1040 <= machines[i] <= 105Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[1,0,5]
[0,3,0]
[0,2,0]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #517: Super Washing Machines
class Solution {
public int findMinMoves(int[] machines) {
int n = machines.length;
int s = 0;
for (int x : machines) {
s += x;
}
if (s % n != 0) {
return -1;
}
int k = s / n;
s = 0;
int ans = 0;
for (int x : machines) {
x -= k;
s += x;
ans = Math.max(ans, Math.max(Math.abs(s), x));
}
return ans;
}
}
// Accepted solution for LeetCode #517: Super Washing Machines
func findMinMoves(machines []int) (ans int) {
n := len(machines)
s := 0
for _, x := range machines {
s += x
}
if s%n != 0 {
return -1
}
k := s / n
s = 0
for _, x := range machines {
x -= k
s += x
ans = max(ans, max(abs(s), x))
}
return
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
# Accepted solution for LeetCode #517: Super Washing Machines
class Solution:
def findMinMoves(self, machines: List[int]) -> int:
n = len(machines)
k, mod = divmod(sum(machines), n)
if mod:
return -1
ans = s = 0
for x in machines:
x -= k
s += x
ans = max(ans, abs(s), x)
return ans
// Accepted solution for LeetCode #517: Super Washing Machines
struct Solution;
impl Solution {
fn find_min_moves(machines: Vec<i32>) -> i32 {
let n = machines.len();
let sum: i32 = machines.iter().sum();
if sum % n as i32 != 0 {
return -1;
}
let avg = sum / n as i32;
let mut res = 0;
let mut count = 0;
for i in (1..n).rev() {
let diff = machines[i] - avg;
res = res.max(diff);
count += diff;
res = res.max(count.abs());
}
res
}
}
#[test]
fn test() {
let machines = vec![1, 0, 5];
let res = 3;
assert_eq!(Solution::find_min_moves(machines), res);
let machines = vec![0, 3, 0];
let res = 2;
assert_eq!(Solution::find_min_moves(machines), res);
let machines = vec![0, 2, 0];
let res = -1;
assert_eq!(Solution::find_min_moves(machines), res);
}
// Accepted solution for LeetCode #517: Super Washing Machines
function findMinMoves(machines: number[]): number {
const n = machines.length;
let s = machines.reduce((a, b) => a + b);
if (s % n !== 0) {
return -1;
}
const k = Math.floor(s / n);
s = 0;
let ans = 0;
for (let x of machines) {
x -= k;
s += x;
ans = Math.max(ans, Math.abs(s), x);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.