Corporate Flight Bookings - Problem
Imagine you're working for a corporate travel management system that handles flight reservations for multiple business trips. Your airline operates n flights numbered from 1 to n.
You receive an array of booking requests where each booking bookings[i] = [first_i, last_i, seats_i] represents:
first_i: The first flight number in the rangelast_i: The last flight number in the range (inclusive)seats_i: Number of seats to reserve on each flight in this range
Your task is to calculate the total number of reserved seats for each individual flight. Return an array answer of length n where answer[i] represents the total seats reserved for flight i+1.
Example: If you have booking [2, 4, 10], it means reserve 10 seats on flights 2, 3, and 4.
Input & Output
example_1.py โ Basic Range Bookings
$
Input:
bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5
โบ
Output:
[10,55,45,25,25]
๐ก Note:
Flight 1: booking [1,2,10] โ 10 seats. Flight 2: bookings [1,2,10] + [2,3,20] + [2,5,25] โ 55 seats. Flight 3: bookings [2,3,20] + [2,5,25] โ 45 seats. Flights 4,5: booking [2,5,25] โ 25 seats each.
example_2.py โ Single Booking
$
Input:
bookings = [[1,2,10]], n = 2
โบ
Output:
[10,10]
๐ก Note:
Only one booking reserves 10 seats for flights 1 and 2, so both flights have exactly 10 reserved seats.
example_3.py โ Non-overlapping Ranges
$
Input:
bookings = [[1,2,10],[3,4,20]], n = 4
โบ
Output:
[10,10,20,20]
๐ก Note:
Flights 1-2 get 10 seats from first booking, flights 3-4 get 20 seats from second booking. No overlap between ranges.
Constraints
- 1 โค bookings.length โค 2 ร 104
- 1 โค firsti โค lasti โค n โค 2 ร 104
- 1 โค seatsi โค 104
- All flight numbers are 1-indexed
Visualization
Tap to expand
Understanding the Visualization
1
Booking Requests
Guests book rooms for date ranges [check-in, check-out] with room counts
2
Smart Tracking
Instead of updating every night, mark check-in (+rooms) and check-out (-rooms)
3
Daily Calculation
Process chronologically to get running totals for each night
4
Final Occupancy
Each night shows total occupied rooms from all overlapping bookings
Key Takeaway
๐ฏ Key Insight: Range update problems can be solved efficiently using difference arrays - mark boundaries instead of updating entire ranges, then compute prefix sums.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code