Bank app~15 min2s · 256 MBtarget O(n log n) time, O(n) spacesorting
You work on the support desk tools of a bank app. Each ticket has a priority from 1 to 1000, where a larger number is more urgent, and an age: the minutes it has waited.
Your team lead says: "Agents should pick up urgent tickets first. Among tickets of equal priority, take the one that has waited longest."
When two tickets have the same priority and the same age, the one that came first in the list goes first.
Write order_tickets(priority, age).
t has priority priority[t] and age age[t] minutes.Input: priority = [2, 5, 2, 5], age = [10, 3, 30, 3]
Output: [1, 3, 2, 0]Priority decides first, then age, then the ticket number.
| Pick-up place | Ticket | Priority | Age |
|---|---|---|---|
| 1 | 1 | 5 | 3 |
| 2 | 3 | 5 | 3 |
| 3 | 2 | 2 | 30 |
| 4 | 0 | 2 | 10 |
Tickets 1 and 3 tie on priority 5 and age 3, so the smaller number goes first.
Input: priority = [4], age = [0]
Output: [0]One ticket gives a list of one.
Input: priority = [3, 3, 3], age = [7, 7, 7]
Output: [0, 1, 2]Equal tickets keep the input order.
1 ≤ priority.length = age.length ≤ 100,000 (tickets)1 ≤ priority[t] ≤ 1,0000 ≤ age[t] ≤ 1,000,000 (minutes)Picking the best of the remaining tickets one by one makes about n squared / 2 = 5,000,050,000 comparisons. A sort makes about n log n, around 1.6 million. The sort needs a comparison rule that never ties.