Corporate Flight Bookings LeetCode solution in JavaScript

1
var corpFlightBookings = function (bookings, n) {
2
// +1 as dummy guard on the tail, which allow us not to check right boundary every time
3
let unitStep = Array(n + 1).fill(0);
4

5
for (const [first, last, seatVector] of bookings) {
6
// -1 because booking flight is 1-indexed, given by description
7
let [left, right] = [first - 1, last - 1];
8

9
unitStep[left] += seatVector;
10
unitStep[right + 1] -= seatVector;
11
}
12

13
// Reconstruct booking as drawing combination of retangle signal, built with unit step impulse
14
for (let i = 1; i < unitStep.length; i++) {
15
unitStep[i] += unitStep[i - 1];
16
}
17

18
// last one is dummy guard on the tail, no need to return
19
return unitStep.slice(0, n);
20
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0