There are n
flights that are labeled from 1
to n
. You are given an array of flight bookings bookings
, where bookings[i] = [first<sub>i</sub>, last<sub>i</sub>, seats<sub>i</sub>]
represents a booking for flights first<sub>i</sub>
through last<sub>i</sub>
(inclusive) with seats<sub>i</sub>
seats reserved for each flight in the range. Return an array answer
of length n
, where answer[i]
is the total number of seats reserved for flight i
.
For example:
bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5
Output: [10,55,45,25,25]
Explanation:
Flight labels: 1 2 3 4 5
Booking 1 reserved: 10 10
Booking 2 reserved: 20 20
Booking 3 reserved: 25 25 25 25
Total seats: 10 55 45 25 25
Hence, answer = [10,55,45,25,25]
Another example:
bookings = [[1,2,10],[2,2,15]], n = 2
Output: [10,25]
Explanation:
Flight labels: 1 2
Booking 1 reserved: 10 10
Booking 2 reserved: 15
Total seats: 10 25
Hence, answer = [10,25]
What is the most efficient way to solve this problem? Consider approaches that minimize both time and space complexity. Break down the complexity for each approach.
A naive approach is to iterate through each booking and, for each booking, iterate through the range of flights it covers, incrementing the seat count for each flight within that range.
def corpFlightBookings_brute_force(bookings, n):
answer = [0] * n
for first, last, seats in bookings:
for i in range(first - 1, last):
answer[i] += seats
return answer
O(m*n), where m
is the number of bookings and n
is the number of flights. This is because, in the worst case, each booking could span almost all flights, leading to nested loops.
O(n), where n
is the number of flights. This is due to the answer
array used to store the seat counts for each flight.
A more efficient approach uses the prefix sum technique. Instead of iterating through the range of flights for each booking, we can update the start and end points of the range and then compute the prefix sum to get the final answer.
[first, last, seats]
, add seats
to the first
flight and subtract seats
from the last + 1
flight (if last + 1
is within the bounds).def corpFlightBookings_optimal(bookings, n):
answer = [0] * n
for first, last, seats in bookings:
answer[first - 1] += seats
if last < n:
answer[last] -= seats
for i in range(1, n):
answer[i] += answer[i - 1]
return answer
O(m + n), where m
is the number of bookings and n
is the number of flights. This is because we iterate through the bookings once (O(m)) and then iterate through the flights once to compute the prefix sum (O(n)).
O(n), where n
is the number of flights. This is due to the answer
array used to store the seat counts for each flight.
bookings
array is empty, the function should return an array of zeros.n
is 1, the function should return an array with a single element, which is the sum of all seats reserved for that flight.first > last
are invalid based on the prompt's constraint 1 <= firsti <= lasti <= n
. We can assume that such cases will not appear in the input, so we don't need to validate the input.bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5
answer = [0, 0, 0, 0, 0]
[1, 2, 10]
: answer[0] += 10
, answer[2] -= 10
(if 2 < 5). answer
becomes [10, 0, -10, 0, 0]
[2, 3, 20]
: answer[1] += 20
, answer[3] -= 20
(if 3 < 5). answer
becomes [10, 20, -10, -20, 0]
[2, 5, 25]
: answer[1] += 25
, answer[5] -= 25
(if 5 < 5). answer
becomes [10, 45, -10, -20, 0]
[10, 10+45, 55-10, 45-20, 25+0] = [10, 55, 45, 25, 25]