Laundromat Machine Count
Your laundromat works by reservation. Customers book a washing machine for a window measured in minutes after opening. A booking of [10, 40] means the load starts at minute 10 and the machine is free again at minute 40, so it can take a load that starts at 40.
Machines are expensive, so given a busy Saturday's reservations, you want to know how few machines could have served everyone on time.
Your task
Write machinesNeeded(loads). Each load is [start, end] with start < end. Return the minimum number of machines so every load runs on some machine and no machine runs two loads at once.
A load ending at time t and one starting at t can share a machine. An empty list needs 0 machines.
machinesNeeded(loads: int[][]) → int
Examples
loads = [[0, 45], [10, 40], [40, 70], [50, 95]]
2
From minute 10 to 40 two loads run at once, so you need at least 2 machines, and 2 is enough. Machine A runs [0, 45] and then [50, 95], and machine B runs [10, 40] and then [40, 70].
loads = [[30, 60], [60, 90], [90, 120]]
1
Each load starts exactly when the previous one ends, so one machine runs all three back to back.
Constraints
- 0 ≤ loads.length ≤ 10,000
- 0 ≤ start < end ≤ 1,000,000,000
Start with brute force
For each load's start time t, scan the whole list to count loads running then (start ≤ t < end). The peak is the answer, because the busiest moment is always some load's start.
That is O(n²), which is about 100 million comparisons for 10,000 loads, and it recounts the same loads again and again. Sorting lets you keep a running count instead.
Hints
0 of 4 shown- Hint 1
In the first example, pick any minute and count the loads running then. What is the largest count, and how does it relate to the answer?
- Hint 2
You can never use fewer machines than the most loads running at once, and you never need more. So: what is the peak number of loads in progress?
- Hint 3
The count only changes when a load starts (+1) or ends (-1), so visit those moments in time order and track the count. What should happen when an end and a start share a minute?
- Hint 4
Sort the start times, and separately the end times. Walk the starts in order; before counting a start, release every load whose end ≤ that start (one pointer into the sorted ends). Track the running count and its maximum. Ends at the same minute go first, because touching loads can share a machine.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
The answer is the most loads running at the same moment. Fewer machines fail at that moment, and that many always suffice, since each new load can take any free machine. To find the peak, sweep through time: a start adds a running load, an end removes one.
Trace the first example. Starts: [0, 10, 40, 50]. Ends: [40, 45, 70, 95].
- At start 0 no load has ended yet, so the running count is 1 and the peak is 1.
- At start 10 no load has ended yet, so the running count is 2 and the peak is 2.
- At start 40 you first release the load that ends at 40, so the running count drops to 1. The new start raises it to 2, and the peak stays 2.
- At start 50 you first release the load that ends at 45, so the running count drops to 1. The new start raises it to 2, and the answer is 2.
Releasing ends with ≤ before counting the start lets touching loads share a machine; with < you would report 3. The inner loop never runs past the end of ends, because the current load's own end is still waiting and is greater than the current start.
function machinesNeeded(loads) {
const starts = loads.map(l => l[0]).sort((a, b) => a - b);
const ends = loads.map(l => l[1]).sort((a, b) => a - b);
let running = 0, peak = 0, e = 0;
for (const start of starts) {
// Free every machine whose load is done by now.
while (ends[e] <= start) {
running--;
e++;
}
running++;
peak = Math.max(peak, running);
}
return peak;
}def machines_needed(loads):
starts = sorted(s for s, _ in loads)
ends = sorted(e for _, e in loads)
running = peak = e = 0
for start in starts:
# Free every machine whose load is done by now.
while ends[e] <= start:
running -= 1
e += 1
running += 1
peak = max(peak, running)
return peakRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.