Corporate Flight Bookings - Problem

There are n flights that are labeled from 1 to n.

You are given an array of flight bookings bookings, where bookings[i] = [first_i, last_i, seats_i] represents a booking for flights first_i through last_i (inclusive) with seats_i 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.

Input & Output

Example 1 — Basic Overlapping Bookings
$ Input: bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5
Output: [10,55,45,25,25]
💡 Note: Flight 1: 10 seats from [1,2,10]. Flight 2: 10+20+25=55 seats (all three bookings cover flight 2). Flight 3: 20+25=45 seats. Flights 4,5: 25 seats each from [2,5,25].
Example 2 — Single Booking
$ Input: bookings = [[1,2,10]], n = 2
Output: [10,10]
💡 Note: One booking [1,2,10] covers flights 1 and 2, so both get 10 seats.
Example 3 — Non-overlapping Ranges
$ Input: bookings = [[1,2,10],[3,4,20]], n = 4
Output: [10,10,20,20]
💡 Note: First booking covers flights 1-2 with 10 seats each. Second booking covers flights 3-4 with 20 seats each. No overlap.

Constraints

  • 1 ≤ n ≤ 2 × 104
  • 1 ≤ bookings.length ≤ 2 × 104
  • bookings[i].length == 3
  • 1 ≤ firsti ≤ lasti ≤ n
  • 1 ≤ seatsi ≤ 104

Visualization

Tap to expand
Corporate Flight Bookings OverviewBookings:[1,2,10][2,3,20][2,5,25]Flights:1055452525Flight 1Flight 2Flight 3Flight 4Flight 5Flight 2 has most bookings: 10+20+25 = 55 seatsFlights 4,5 only covered by booking [2,5,25]Result: [10, 55, 45, 25, 25]
Understanding the Visualization
1
Input
Bookings: [[1,2,10],[2,3,20],[2,5,25]], n=5
2
Process
Each booking reserves seats for a range of flights
3
Output
Count total seats per flight: [10,55,45,25,25]
Key Takeaway
🎯 Key Insight: Use difference array to convert range updates into point updates, then compute prefix sum
Asked in
Amazon 15 Google 12 Microsoft 8
28.5K Views
Medium Frequency
~15 min Avg. Time
892 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