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.length
1 <= n <= 104
0 <= machines[i] <= 105
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for this washing machine problem involves exploring all possible ways to redistribute dresses between the machines. We simulate moving dresses in different amounts between machines until all machines have the same number of dresses. We then find the minimum number of moves needed across all the possibilities that achieve balance.
Here's how the algorithm would work step-by-step:
def super_washing_machines_brute_force(machines):
total_dresses = sum(machines)
number_of_machines = len(machines)
# If the dresses can't be evenly distributed, it's impossible
if total_dresses % number_of_machines != 0:
return -1
average_dresses = total_dresses // number_of_machines
min_moves = float('inf')
def solve(current_machines, moves):
nonlocal min_moves
all_equal = all(machine == average_dresses for machine in current_machines)
if all_equal:
min_moves = min(min_moves, moves)
return
# Limit the search depth to avoid infinite loops
if moves > 10 * number_of_machines:
return
for i in range(number_of_machines):
if current_machines[i] != average_dresses:
for j in range(number_of_machines):
if i != j:
# Try moving a dress from i to j
temp_machines = current_machines[:]
if current_machines[i] > 0:
temp_machines[i] -= 1
temp_machines[j] += 1
# Recursively solve with the new state
solve(temp_machines, moves + 1)
# Start the search from the initial state with 0 moves
solve(machines, 0)
# If no solution was found, return -1
if min_moves == float('inf'):
return -1
else:
return min_moves
The problem can be viewed as balancing out the load between washing machines. We aim to determine the minimum number of moves required to achieve an equal distribution of dresses among all machines by transferring dresses between adjacent machines.
Here's how the algorithm would work step-by-step:
def super_washing_machines(machines):
number_of_machines = len(machines)
total_dresses = sum(machines)
average_dresses = total_dresses / number_of_machines
# Impossible to balance if total dresses can't be evenly divided
if total_dresses % number_of_machines != 0:
return -1
max_moves = 0
transfer_amount = 0
for dresses_at_machine in machines:
# Calculate the difference between current and target
difference = dresses_at_machine - average_dresses
# Update the cumulative transfer amount
transfer_amount += difference
# The number of moves depends on both transfer and difference
moves_needed = max(abs(transfer_amount), difference)
# Track the global maximum moves needed
max_moves = max(max_moves, abs(moves_needed))
return int(max_moves)
Case | How to Handle |
---|---|
Empty input array | Return 0 since no machines and thus no moves are needed. |
Array with one washing machine | Return 0 because a single machine is already balanced. |
Array with all identical values | Return 0, as the machines are already balanced and no moves are needed. |
Total number of dresses not evenly divisible by the number of washing machines | Return -1 because it's impossible to balance the dresses among the machines. |
Integer overflow when calculating the total number of dresses | Use a data type that can hold the total number of dresses (e.g., long) or check for potential overflow before summing. |
Large array size impacting memory or performance | The algorithm should be O(n), so it should scale linearly, but memory consumption should be monitored for very large n. |
Highly skewed distribution of dresses (one machine with a huge number) | The algorithm should correctly handle skewed distributions as it focuses on the imbalances between machines and the average. |
The algorithm gets stuck in an infinite loop when balancing | The algorithm calculates the max load required from each machine to other machines to achieve average dresses in the end so that the algorithm always converges. |