SDE & MLE Prep

1109. Corporate Flight Bookings

Problem

Flights are numbered 1…n. bookings[i] = [first, last, seats] reserves seats seats on every flight from first to last. Return the total number of seats reserved on each flight.

Example: bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5 → [10,55,45,25,25]

Constraints: n and the number of bookings ≤ 2·10⁴.

Each booking adds seats to a range of flights: d[first] += seats, d[last + 1] -= seats. One prefix-sum pass produces all totals.

Complexity: O(n + bookings).

class Solution:
    def corpFlightBookings(self, bookings: List[List[int]], n: int) -> List[int]:
        d = [0] * (n + 1)
        for first, last, seats in bookings:
            d[first - 1] += seats
            d[last] -= seats
        return list(itertools.accumulate(d[:n]))