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 range
  • last_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
๐Ÿจ Hotel Reservation SystemNight 1Night 2Night 3Night 4Night 5Booking A: 10 roomsBooking B: 20 roomsBooking C: 25 rooms1055452525Total occupied rooms per night from overlapping bookings
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.
Asked in
Google 45 Amazon 38 Microsoft 32 Bloomberg 28
52.3K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen