oneby7

Arrays and Hashing Difference arrays

593. Corporate Flight Bookings

Medium

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

Output: [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

Output: [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

Output: [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.

bookings = [[2, 3, 10]], n = 4

Constraints

  • 1 ≤ n ≤ 105
  • 1 ≤ bookings.length ≤ 105
  • bookings[i] = [first, last, seats]
  • 1 ≤ first ≤ last ≤ n
  • 1 ≤ seats ≤ 109

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.