Assign Each Flight a Runway
An airport has n runways and m flights. Flight i wants the runway from start_i to end_i. Model the controller as a scheduler: runways are workers, flights are tasks, and each flight goes to the runway that becomes free soonest.
Process flights in order of start time (ties by index). A runway that is already free when the flight's start arrives takes it and is busy until end_i. If every runway is still busy, the flight waits for the one that frees up first and occupies it for the flight's full duration from that moment, so the runway is next free at freeTime + (end_i - start_i). Among runways free at the same time, pick the lowest index.
Return an array of length m whose i-th element is the runway assigned to flight i, in the input order.
n = 2, flights = [[1, 2], [2, 3], [3, 5]] -> [0, 1, 0] n = 1, flights = [[0, 10], [1, 2], [2, 3]] -> [0, 0, 0] (flights 1 and 2 wait)
1 <= n, m <= 100 and 0 <= start < end <= 1000.
Hints
Hint 1
Sort flight indices by start time; the greedy only works chronologically.
Hint 2
Keep the runways in a min-heap (or, at this size, a scan) keyed by (freeTime, index). Pop the earliest free runway, assign, and push it back with its new free time.
Hint 3
The delayed case is the one to get right: the new free time is max(freeTime, start) + (end - start), not end.
Topics
Asked at
Sign in to use the assistant — ask what the problem means, get a hint when you're stuck, or have your current work reviewed. Conversations save to your account.