Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Corporate Flight Bookings in Python
The Corporate Flight Bookings problem involves calculating the total number of seats booked on each flight when given multiple booking ranges. Each booking specifies a range of flights and the number of seats to book across that range.
Problem Understanding
Given n flights labeled 1 to n, and a list of bookings where each booking [i, j, k] means k seats are booked from flight i to flight j (inclusive), we need to find the total seats booked on each flight.
Example
With bookings [[1,2,10],[2,3,20],[2,5,25]] and n = 5:
- Booking [1,2,10]: 10 seats on flights 1 and 2
- Booking [2,3,20]: 20 seats on flights 2 and 3
- Booking [2,5,25]: 25 seats on flights 2, 3, 4, and 5
Result: Flight 1: 10, Flight 2: 55, Flight 3: 45, Flight 4: 25, Flight 5: 25
Solution Approach
We use a difference array technique to efficiently handle range updates:
- Create a result array of size n filled with zeros
- For each booking [start, end, seats], add seats at position (start-1) and subtract seats at position end
- Apply prefix sum to get the final seat counts
Implementation
class Solution:
def corpFlightBookings(self, bookings, n):
result = [0] * n
# Apply difference array technique
for start, end, seats in bookings:
result[start - 1] += seats # Add seats at start
if end < n:
result[end] -= seats # Subtract seats after end
# Apply prefix sum to get final counts
for i in range(1, n):
result[i] += result[i - 1]
return result
# Test the solution
solution = Solution()
bookings = [[1,2,10],[2,3,20],[2,5,25]]
n = 5
print(solution.corpFlightBookings(bookings, n))
[10, 55, 45, 25, 25]
How It Works
The algorithm works in two phases:
Phase 1: Mark Range Boundaries
# Initial array: [0, 0, 0, 0, 0]
# After [1,2,10]: [10, 0, -10, 0, 0]
# After [2,3,20]: [10, 20, -10, -20, 0]
# After [2,5,25]: [10, 45, -10, -20, 0]
result = [0] * 5
bookings = [[1,2,10],[2,3,20],[2,5,25]]
for start, end, seats in bookings:
result[start - 1] += seats
if end < 5:
result[end] -= seats
print(f"After booking {[start, end, seats]}: {result}")
After booking [1, 2, 10]: [10, 0, -10, 0, 0] After booking [2, 3, 20]: [10, 20, -10, -20, 0] After booking [2, 5, 25]: [10, 45, -10, -20, 0]
Phase 2: Apply Prefix Sum
result = [10, 45, -10, -20, 0]
for i in range(1, 5):
result[i] += result[i - 1]
print(f"After step {i}: {result}")
After step 1: [10, 55, -10, -20, 0] After step 2: [10, 55, 45, -20, 0] After step 3: [10, 55, 45, 25, 0] After step 4: [10, 55, 45, 25, 25]
Time Complexity
- Time: O(bookings + n) − process each booking once, then one pass for prefix sum
- Space: O(n) − result array only
Conclusion
The difference array technique efficiently handles multiple range updates in O(1) per booking. The prefix sum converts the difference array back to actual seat counts, making this solution optimal for the corporate flight bookings problem.
