Schedule an agent's tool calls by priority

Agent buildMedium30 min

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. arrive is 0 or more and never decreases along the list. duration is at least 1. A higher priority is 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:

  1. A call runs for duration seconds without stopping. A call that starts at second s frees its slot at second s + duration, and another call may start in that slot at that same second.
  2. A call waits from its arrive second until it starts. It never starts before it arrives.
  3. The runtime never leaves a slot free while a call is waiting.
  4. When more calls are waiting than there are free slots, the ones with the highest priority start first. Ties go to the call earlier in the list.
  5. 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
Inputstart_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
Inputstart_times([[0, 3, 0], [1, 2, 7], [3, 1, 2], [3, 2, 9]], 1)
Output[0, 5, 7, 3]
Example 3
Inputstart_times([[0, 4, 1], [10, 2, 1], [10, 3, 5]], 3)
Output[0, 10, 10]