Leetcode Problem 1109. Corporate Flight Bookings

1109. Corporate Flight Bookings

Leetcode Solutions

Sweep Line Algorithm

  1. Initialize an array res of length n + 1 with all zeros.
  2. Iterate over each booking in the bookings array. a. For each booking [first, last, seats], increment res[first - 1] by seats (add seats at the start of the range). b. Decrement res[last] by seats (remove seats after the end of the range).
  3. Perform a running sum on the res array. a. Iterate from the second element to the last element of the res array. b. Update each element by adding it to the previous element's value.
  4. Remove the last element from the res array as it is not needed (it represents the seats after the last flight).
  5. Return the modified res array as the result.
UML Thumbnail

Brute Force Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Code Diffs Compare
Full Screen
Copy Answer Code
Loading...