Arrays and Hashing Difference arrays
593. Corporate Flight Bookings
There are n flights, numbered from 1 to n. You are given a list of bookings. Each booking
[first, last, seats] reserves seats seats on every flight from first to last, both ends included.
Return an array answer of length n, where answer[i] is the total number of seats reserved on flight
i + 1.
Example 1
Input: bookings = [[1, 3, 20], [2, 5, 15], [4, 4, 30]]
n = 5
[20,35,35,45,15]Explanation: Flight 1 is in the first booking only, so it has 20 seats. Flights 2 and 3 are in the first two bookings, for 20 + 15 = 35. Flight 4 is in the last two, for 15 + 30 = 45, and flight 5 is in the second only.
Example 2
Input: bookings = [[4, 4, 7], [1, 1, 3], [4, 4, 5]], n = 4
[3,0,0,12]Explanation: No booking covers flights 2 and 3, so their totals are 0. Two bookings are for flight 4 alone, and their seats add up to 12.
Example 3
Input: bookings = [[1, 3, 1000000000],
[1, 2, 1000000000],
[2, 3, 1000000000]]
n = 3
[2000000000,3000000000,2000000000]Explanation: Flight 2 is in all three bookings, so it has 3 × 109 seats. That is more than a 32-bit integer can hold.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ n ≤ 1051 ≤ bookings.length ≤ 105bookings[i] = [first, last, seats]1 ≤ first ≤ last ≤ n1 ≤ seats ≤ 109
Clarifying questions
What you would ask an interviewer before writing any code.