#1109

Corporate Flight Bookings

Medium Difference array

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

There are n flights. Each booking [first, last, seats] reserves seats on every flight from first to last. Return the total seats for each flight.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: many bookings each add seats to a range of flights; total per flight?

Why this works: instead of updating every flight in a range, mark +seats where the range starts and -seats just after it ends. One running sum at the end spreads the marks.

Example: bookings [1,2,10], [2,3,20], [2,5,25], n = 5

marks: +10 at 1, -10 at 3; +20 at 2, -20 at 4; +25 at 2running: 10, 55, 45, 25, 25

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Mixing 1-based flight numbers with 0-based indexes — subtract one from first, and last becomes the "after" index.