Schedule an agent's tool calls by priority
Your task
Your agent's runtime runs tool calls in the background, with at most slots running at once. Calls keep arriving while others run. When a slot frees up, the runtime starts the most urgent waiting call, not the oldest one.
Write start_times(calls, slots) that returns the second at which each call starts.
calls: a list of[arrive, duration, priority], whole numbers, in the order the calls were made.arriveis 0 or more and never decreases along the list.durationis at least 1. A higherpriorityis more urgent.slots: a whole number, at least 1.- Return a list with each call's start second, in the same order as
calls.
Rules:
- A call runs for
durationseconds without stopping. A call that starts at secondsfrees its slot at seconds + duration, and another call may start in that slot at that same second. - A call waits from its
arrivesecond until it starts. It never starts before it arrives. - The runtime never leaves a slot free while a call is waiting.
- When more calls are waiting than there are free slots, the ones with the highest
prioritystart first. Ties go to the call earlier in the list. - Within one second, the slots freed at that second count as free, and the calls arriving at that second count as waiting, before any call starts.
Examples
start_times(calls: list[list[int]], slots: int) -> list[int]Example 1
Input
start_times([[0, 5, 1], [0, 3, 1], [1, 2, 5], [2, 4, 9], [2, 1, 0]], 2)Output
[0, 0, 5, 3, 7]Example 2
Input
start_times([[0, 3, 0], [1, 2, 7], [3, 1, 2], [3, 2, 9]], 1)Output
[0, 5, 7, 3]Example 3
Input
start_times([[0, 4, 1], [10, 2, 1], [10, 3, 5]], 3)Output
[0, 10, 10]