Skip to main content
  1. LeetCode/

LeetCode 1109: Corporate Flight Bookings

·1 min· ·
LeetCode Difference Array Medium
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2025-04-27

  • Total time: 5:00.00

Intuition
#

This is a classic difference array problem with a known array size n.
Thus, we can directly initialize a difference array of size n + 1.
Then, iterate through the bookings array to apply the difference updates to the appropriate indices.
Finally, perform a prefix sum over the difference array to reconstruct the final number of booked seats for each flight.

Approach
#

class Solution:
    def corpFlightBookings(self, bookings: List[List[int]], n: int) -> List[int]:
        diff_array = [0]*(n+1)

        for start, end, seat in bookings:
            diff_array[start-1] += seat
            diff_array[end] -= seat

        res = []
        diff = 0
        for seat in diff_array[:-1]:
            diff += seat
            res.append(diff)

        return res
  • start - 1 because flight numbering is 1-indexed but the array is 0-indexed.
  • end cancels the added seat count after the last flight of the booking.
  • Finally, a prefix sum restores the actual number of seats booked for each flight.

Findings
#

N/A

Encountered Problems
#

I encountered an index not found error the first time,
because I didn’t realize that the flight numbers were 1-indexed.
This caused an off-by-one mistake when updating the difference array.
After adjusting start to start - 1, the problem was solved.

Related

LeetCode 1094: Car Pooling
·2 mins
LeetCode Difference Array Medium
LeetCode Problem Solving
LeetCode 2381: Shifting Letters II
·2 mins
LeetCode Medium Difference Array
LeetCode Problem Solving
LeetCode 3527: Find the Most Common Response
·1 min
LeetCode Bi-Weekly Medium
LeetCode Problem Solving
LeetCode 3528: Unit Conversion I
·1 min
LeetCode Bi-Weekly Medium
LeetCode Problem Solving
LeetCode 2145: Count the Hidden Sequences
·2 mins
LeetCode Daily Medium
LeetCode Problem Solving
LeetCode 2799: Count Complete Subarrays in an Array
·1 min
LeetCode Daily Medium
LeetCode Problem Solving