Bed Board Turnover
MediumOnline Assessment
Track a hospital bed board where beds must be cleaned after every patient and newly arriving patients sometimes have to wait.
beds[i] = [ward, minutes] gives bed i's ward and how long cleaning it takes. All beds start clean. A bed is clean, occupied, dirty (waiting for a cleaner) or cleaning. There are crews cleaning crews; each cleans one bed at a time without interruption. events is a list of [minute, kind, patient, ward] entries in order (the ward is only present for admit and transfer; minutes never decrease):
"admit": ignored if the patient is already in the hospital (in a bed, or waiting for one). Otherwise the patient takes the lowest-numbered clean bed in that ward. If there is none, the patient joins the waiting list."transfer": ignored unless the patient is in a bed, is not already waiting for a transfer, and the ward differs from the patient's current one. Otherwise the patient takes the lowest-numbered clean bed in the new ward and the old bed becomes dirty at once. If there is no clean bed, the patient joins the waiting list and keeps the old bed until a bed is found."discharge": ignored if the patient is not in the hospital. Otherwise the patient leaves the waiting list (if on it) and their bed (if they have one) becomes dirty.
Whenever a crew is free it takes the dirty bed that has been dirty longest (ties: lower bed number) and starts cleaning it. When cleaning ends, the bed is clean and goes straight to the first patient on the waiting list wanting its ward, if any; a transferring patient's old bed becomes dirty at that moment.
Within one minute: first every cleaning that ends now finishes (lower bed number first), then the events of that minute are applied in the given order, then free crews start on dirty beds. Process everything up to and including minute end.
Input: beds, crews, events, end (an integer).
Output: [states, waiting]: states has one string per bed, the patient's name if occupied or "clean", "dirty", "cleaning"; waiting lists the waiting patients in the order they joined the list.
Example 1
- Input:
- [[["ICU",30],["ICU",30],["MED",20],["MED",20],["MED",20]],1,[[0,"admit","A","MED"],[0,"admit","B","MED"],[5,"admit","C","MED"],[10,"admit","D","MED"],[12,"discharge","B"],[20,"discharge","A"]],70]
- Output:
- [["clean","clean","clean","D","C"],[]]
- Explanation
- Beds 2 to 4 are the MED ward (20 minutes to clean), with one crew. A, B and C fill beds 2, 3, 4 at minutes 0 to 5, and D finds no bed at minute 10 and waits. B leaves at 12, so bed 3 is cleaned from 12 to 32 and then goes straight to D. A leaves at 20; bed 2 only starts cleaning at 32 (the crew was busy) and is clean at 52. By minute 70 the beds read clean, clean, clean, D, C.
Example 2
- Input:
- [[["ICU",30],["ICU",30],["MED",20],["MED",20],["MED",20]],1,[[0,"admit","A","ICU"],[0,"admit","B","ICU"],[1,"admit","C","MED"],[2,"admit","D","ICU"],[3,"transfer","C","ICU"],[10,"discharge","B"],[40,"transfer","B","MED"]],41]
- Output:
- [["A","D","C","clean","clean"],["C"]]
- Explanation
- A and B take both ICU beds (0 and 1) and C takes MED bed 2. D asks for ICU at minute 2 and waits. C asks to transfer to ICU at 3, finds none, and waits while still holding bed 2. B leaves at 10, so bed 1 is cleaned from 10 to 40 (30 minutes) and goes to D, the first on the list. B's transfer request at minute 40 is ignored, because B has already left. At minute 41 the beds read A, D, C, clean, clean, with C still waiting.
Example 3
- Input:
- [[["ICU",30],["ICU",30],["MED",20],["MED",20],["MED",20]],1,[[0,"admit","A","MED"],[0,"admit","B","MED"],[0,"admit","C","MED"],[0,"admit","E","ICU"],[5,"discharge","C"],[5,"discharge","A"],[6,"transfer","B","ICU"],[10,"admit","D","MED"]],60]
- Output:
- [["E","B","D","cleaning","clean"],[]]
- Explanation
- A, B and C fill beds 2, 3, 4 and E takes ICU bed 0. At minute 5 C and A leave: both beds are dirty since minute 5, and the lower bed number goes first, so bed 2 is cleaned from 5 to 25 and bed 4 from 25 to 45. At 6, B transfers to ICU bed 1 (clean), so bed 3 turns dirty at 6 and waits behind them. D, who arrives at 10 and waits, gets bed 2 at 25. Bed 3 only starts cleaning at 45, so at minute 60 it is still cleaning. States: E, B, D, cleaning, clean; nobody is waiting.
Constraints
- 1 ≤ beds.length ≤ 12; 1 ≤ beds[i][1] ≤ 60; 1 ≤ crews ≤ 4
- 0 ≤ events.length ≤ 80; 0 ≤ minute ≤ end ≤ 600; wards and patients are strings, patient names start with a capital letter
- a ward named in an event may have no beds at all