Corporate Flight Bookings
There are n flights labeled 1 to n. You are given an array bookings where bookings[i] = [first, last, seats] means that seats seats were reserved for every flight from flight first to flight last (inclusive). Return an array answer of length n, where answer[i] is the total number of seats reserved for flight i + 1.
Open official problem prompt ↗Compute the total seats reserved for each of the n flights after applying many overlapping range reservations, without re-touching every flight for every booking.
Think of a ledger of guests entering and leaving a party. Rather than counting the crowd every minute, you mark '+k at time they arrive, -k when they leave', then walk the timeline once to know how many people are present at each moment.
- Input
- bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5
- Output
- [10, 55, 45, 25, 25]
- Why
- Flight 2 gets 10 + 20 + 25 = 55 seats from all three bookings; the others sum their overlapping bookings the same way.
1 <= n <= 2 * 10^41 <= bookings.length <= 2 * 10^4bookings[i].length == 31 <= first_i <= last_i <= n1 <= seats_i <= 10^4