Laundromat Bank
MediumOnline Assessment
Simulate a laundromat where finished washes wait for a free coin-operated dryer, and report when each customer walks out.
The laundromat has numbered washers and numbered dryers (both starting at 1). Washer i always runs a cycle of wash[i - 1] minutes. Dryer j gives dry[j - 1] minutes of heat for each coin inserted. Customers arrive with a load and a number of coins to spend on drying; customers[k] = [arrive, coins], sorted by arrive (several may arrive in the same minute, and they are handled in list order).
A customer's laundry goes through these stages:
- Arrival. The customer starts a wash in the lowest-numbered free washer. If every washer is occupied, the customer turns away at once and never returns.
- Washing. A wash started at minute
tfinishes at minutet + wash. The customer stays and, ifcoinsis 0, simply collects the clothes: they walk out at that minute and the washer is free again from that minute on. - Waiting. Otherwise the wet clothes need a dryer. A washer stays occupied until the clothes are moved to a dryer. Customers waiting for a dryer are served in the order their washes finished; if two washes finish in the same minute, the lower-numbered washer comes first.
- Drying. The first waiting customer takes the lowest-numbered free dryer and inserts all their coins: the dryer runs for
coins * dry[j - 1]minutes. If the dryer is taken at minutet, the clothes are moved at minutet(the washer is free from minutet) and the customer walks out at minutet + coins * dry[j - 1], when the dryer is free again.
Within a single minute, things happen in this order: (1) dryers whose time is up become free; (2) washes finishing now end; (3) waiting customers are assigned to free dryers, in order; (4) customers arriving now are handled, in list order. So a machine freed in this minute can be used by a customer arriving in the same minute.
Input: wash, a list of cycle lengths; dry, a list of minutes per coin; customers, a list of [arrive, coins].
Output: a list with one integer per customer: the minute they walk out, or -1 if they were turned away.
Example 1
- Input:
- [[30,45],[10],[[0,2],[0,1],[5,0],[30,1],[45,1]]]
- Output:
- [50,60,-1,70,-1]
- Explanation
- Washers take 30 and 45 minutes, one dryer gives 10 minutes per coin. Customers 0 and 1 start washes at minute 0 in washers 1 and 2; customer 2 arrives at 5 with both busy and is turned away (-1). At 30 customer 0 takes the dryer for 2 coins (20 minutes, out at 50) and washer 1 is free, so customer 3 starts a wash there at 30. At 45 customer 1's wash ends but the dryer is busy until 50, so the clothes stay in washer 2 and customer 4, arriving at 45, finds both washers occupied (-1). At 50 the dryer frees first, customer 1 takes it for 10 minutes (out at 60) and washer 2 is released. At 60 customer 3's wash ends in the same minute the dryer frees, so customer 3 dries for 10 minutes and walks out at 70.
Example 2
- Input:
- [[30,20],[7,3],[[0,3],[0,2],[1,1],[20,0]]]
- Output:
- [39,34,-1,40]
- Explanation
- Washers take 30 and 20 minutes; dryer 1 gives 7 minutes per coin, dryer 2 gives 3. Customers 0 and 1 start washes in washers 1 and 2; customer 2 (minute 1) is turned away. At 20 customer 1 (2 coins) takes the lowest free dryer, dryer 1: 14 minutes, out at 34. Washer 2 is freed in that same minute, so customer 3 (no coins) starts a wash there at 20 and walks out at 40 when it ends, never touching a dryer. At 30 customer 0 (3 coins) finds dryer 1 busy and takes dryer 2: 3 x 3 = 9 minutes, out at 39.
Example 3
- Input:
- [[5],[2,3],[[0,1],[5,1],[5,2],[7,1]]]
- Output:
- [7,12,-1,-1]
- Explanation
- One washer of 5 minutes, dryers of 2 and 3 minutes per coin. Customer 0 washes 0 to 5 and at 5 takes dryer 1 (1 coin, out at 7). The washer is released at 5, so customer 1, arriving at 5, takes it, while customer 2, also arriving at 5 and handled after customer 1, finds it occupied (-1). Customer 1's wash ends at 10 and dryer 1 (free since 7) gives 2 minutes: out at 12. Customer 3 arrives at 7 while customer 1 is washing: -1.
Constraints
- 1 ≤ wash.length ≤ 6 and 1 ≤ dry.length ≤ 5; every wash[i] is between 1 and 60 and every dry[j] between 1 and 20
- 0 ≤ customers.length ≤ 80; 0 ≤ arrive ≤ 1000, non-decreasing
- 0 ≤ coins ≤ 8