Serve Support Tickets Most-Urgent-First With Arrival Tie-Breaks

Implement serveTickets

A help desk receives a stream of events: a new ticket arrives (add, with an urgency score) or an agent asks for the next ticket to work on (serve). Tickets are numbered 1, 2, 3, … in order of arrival. Each serve must hand back the number of the most urgent waiting ticket — the highest score wins, and if several tickets share the top score, the one that arrived earliest wins. Serving an empty queue reports -1.

Return the ticket number reported by every serve, in order.

Keeping a sorted array works, but every insert and every removal from the front shifts the whole waiting list. A binary heap keeps only the one thing that matters — who is first — and repairs itself along a single path after each change.

Example 1:

Input: ops = ["add","add","serve","add","serve","serve"], priorities = [3,8,0,8,0,0]

Output: [2,3,1]

Example 2:

Input: ops = ["add","add","add","serve","serve","serve"], priorities = [5,5,5,0,0,0]

Output: [1,2,3]

Example 3:

Input: ops = ["serve","add","serve","serve"], priorities = [0,4,0,0]

Output: [-1,1,-1]

+ 9 hidden test cases run on Submit.

Constraints:

  • ●1 ≤ ops.length ≤ 100, and priorities.length equals ops.length
  • ●Each op is either "add" or "serve"; for a "serve", the matching priorities entry is ignored
  • ●-1000 ≤ priorities[i] ≤ 1000; a higher number means a more urgent ticket
  • ●Tickets are numbered 1, 2, 3, … in the order their "add" operations occur

ops =

["add", "add", "serve", "add", "serve", "serve"]

priorities =

[3, 8, 0, 8, 0, 0]