1733. Aging Triage Board

Medium3.5 points estimatedDesignHeapHash Table

Design a TriageBoard for an emergency room. arrive(id, severity, time) adds a waiting patient (returns nothing). At a later moment t a waiting patient's urgency is severity + (t - arrival time), so everyone ages at the same rate. treat(time) removes and returns the id of the most urgent waiting patient (ties: earlier arrival, then smaller id), or -1 if nobody waits. leave(id) removes that waiting patient and returns true, or returns false if the id is not waiting. urgency(id, time) returns that waiting patient's urgency at time as a long, or -1 if not waiting. waiting() returns the count of waiting patients. An id may arrive again after being treated or leaving. Aim for O(log n) per update.

Example 1

Input:
operations = ["TriageBoard","arrive","arrive","treat","urgency","waiting","treat"]arguments = [[],[1,5,0],[2,3,1],[2],[2,3],[],[4]]
Output:
[null,null,null,1,5,1,2]
Explanation:

Patient 1 has key 5 - 0 = 5 and patient 2 has 3 - 1 = 2, so patient 1 is treated first; patient 2 then has urgency 3 + (3 - 1) = 5 at time 3 and is treated next.

Example 2

Input:
operations = ["TriageBoard","arrive","arrive","leave","leave","treat","treat"]arguments = [[],[7,4,2],[3,6,4],[7],[7],[5],[6]]
Output:
[null,null,null,true,false,3,-1]
Explanation:

Leaving removes patient 7 once (true), then false; patient 3 is treated and the next treat finds nobody and returns -1.

Constraints

At most 104 calls.

0 ≤ id ≤ 105, 0 ≤ severity ≤ 109, 0 ≤ time ≤ 109, and time never decreases across calls.

arrive is never called with an id that is currently waiting.

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Input
Each case is an operation log. operations names the class first and then each method call; arguments holds the arguments for each, in the same order. Your answer is one list with a result per operation - null for the constructor and for methods that return nothing.

Expected complexity

Time
O(log n) per operation
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…

operations names the class, then each method to call; arguments holds one list of arguments per operation, in the same order.