There are n flights that are labeled from 1 to n.
You are given an array of flight bookings bookings, where bookings[i] = [firsti, lasti, seatsi] represents a booking for flights firsti through lasti (inclusive) with seatsi 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.
Example 1:
Input: 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]
Example 2:
Input: 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]
Constraints:
1 <= n <= 2 * 1041 <= bookings.length <= 2 * 104bookings[i].length == 31 <= firsti <= lasti <= n1 <= seatsi <= 104When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to solving the flight booking problem is like manually going through each booking and updating a list of flight seats. We consider each booking one at a time and apply its effect to every flight day covered by that booking. It's a simple but potentially slow method.
Here's how the algorithm would work step-by-step:
def corpFlightBookingsBruteForce(bookings, numberOfFlights):
flightBookings = [0] * numberOfFlights
# Iterate through each booking in the bookings list
for booking in bookings:
firstFlight = booking[0]
lastFlight = booking[1]
seatsBooked = booking[2]
# Iterate through each flight day within the current booking
# Adjust flight indices to be 0-based
for flightIndex in range(firstFlight - 1, lastFlight):
# Update the booking counts for each day of the given booking
flightBookings[flightIndex] += seatsBooked
return flightBookingsThis problem can be efficiently solved using a technique similar to prefix sums, but applied cleverly to booking counts. The core idea is to track the *changes* in the number of booked seats instead of the actual number directly, allowing us to calculate the final bookings very quickly.
Here's how the algorithm would work step-by-step:
def corporate_flight_bookings(bookings, number_of_flights):
flight_bookings = [0] * number_of_flights
# Iterate through bookings to mark start and end changes
for start, end, seats in bookings:
flight_bookings[start - 1] += seats
# If end is not the last flight, decrement after the end
if end < number_of_flights:
flight_bookings[end] -= seats
# Accumulate the bookings to determine final state of each flight
for flight_index in range(1, number_of_flights):
flight_bookings[flight_index] += flight_bookings[flight_index - 1]
return flight_bookings| Case | How to Handle |
|---|---|
| Empty bookings array | Return an array of zeros with size n (number of flights) because no bookings imply no changes to the initial flight counts. |
| Bookings array is null | Throw an IllegalArgumentException (or equivalent in the language used) to indicate invalid input, as null bookings are nonsensical. |
| n (number of flights) is zero or negative | Throw an IllegalArgumentException (or equivalent) as the number of flights must be a positive integer to be meaningful. |
| A booking has invalid indices (i > j or indices out of range) | Throw an IllegalArgumentException if i > j or if either index is out of the valid range of 1 to n, inclusive. |
| Large number of flights (large n) combined with many bookings. | The algorithm uses O(n + k) time/space (where k is number of bookings), profile for time efficiency on large datasets. |
| Bookings overlap significantly and repeatedly. | The difference array approach should efficiently handle overlapping ranges without issues, but could expose integer overflow with very high numbers of overlapping flights, which is handled through 64-bit integers or other techniques. |
| Bookings with a large value for the number of seats to book. | Ensure the data type used to store the flight counts (e.g., int) can handle the potential sum of seats across all flights and bookings to prevent integer overflow. |
| Single flight booking requesting a negative number of seats | Throw an IllegalArgumentException, as a negative number of seats to book is not a valid business case. |