Gamified challenge · Priority queues
patients[i] = [arrival, severity, duration]: patient i walks in at minute arrival, with severity 1 (critical) up to 5 (minor), and needs duration minutes with the doctor. There is one doctor, who treats one patient at a time and never stops a treatment halfway.
Whenever the doctor is free, they call the most urgent patient who has already arrived: lowest severity first, then earliest arrival, then lowest index. If nobody is waiting, they wait for the next arrival. Write triage(patients) that returns the patients' indices in the order they are treated.
For [[0, 3, 4], [1, 1, 2], [2, 2, 3], [3, 1, 1]] the answer is [0, 1, 3, 2]: patient 0 is alone at minute 0, and when the doctor is free at minute 4 the critical patients 1 and 3 go before patient 2.
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google