Skip to the code
cf.
+10 XP

#397 Drain the Notification Queue

easy

A notification center receives a stream of commands. "push P ID" adds a notification with priority P (a whole number; smaller is more urgent) and id ID. "pop" shows the most urgent waiting notification and removes it. Among equal priorities, the one pushed earlier is shown first.

Write drain(commands) that runs the commands in order and returns the ids shown by each "pop", in order. A "pop" when nothing is waiting shows "none".

Example 1

Input: commands = ["push 2 email","push 1 alert","pop","push 1 chat","pop","pop"]
Output: ["alert","chat","email"]

alert (1) beats email (2); then chat (1) beats email.

Example 2

Input: commands = ["pop","push 5 a","push 5 b","pop"]
Output: ["none","a"]

Nothing is waiting at first. Then a and b tie, and a was pushed first.

Constraints

  • 1 <= commands.length <= 10^4
  • -10^6 <= P <= 10^6
  • Ids have no spaces.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).