这里有 n
个航班,它们分别从 1
到 n
进行编号。
有一份航班预订表 bookings
,表中第 i
条预订记录 bookings[i] = [firsti, lasti, seatsi]
意味着在从 firsti
到 lasti
(包含 firsti
和 lasti
)的 每个航班 上预订了 seatsi
个座位。
请你返回一个长度为 n
的数组 answer
,其中 answer[i]
是航班 i
上预订的座位总数。
示例 1:
输入:bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5 输出:[10,55,45,25,25] 解释: 航班编号 1 2 3 4 5 预订记录 1 : 10 10 预订记录 2 : 20 20 预订记录 3 : 25 25 25 25 总座位数: 10 55 45 25 25 因此,answer = [10,55,45,25,25]
示例 2:
输入:bookings = [[1,2,10],[2,2,15]], n = 2 输出:[10,25] 解释: 航班编号 1 2 预订记录 1 : 10 10 预订记录 2 : 15 总座位数: 10 25 因此,answer = [10,25]
提示:
1 <= n <= 2 * 104
1 <= bookings.length <= 2 * 104
bookings[i].length == 3
1 <= firsti <= lasti <= n
1 <= seatsi <= 104
class Solution:
def corpFlightBookings(self, bookings: List[List[int]], n: int) -> List[int]:
delta = [0] * n
for first, last, seats in bookings:
delta[first - 1] += seats
if last < n:
delta[last] -= seats
for i in range(n - 1):
delta[i + 1] += delta[i]
return delta
class Solution {
public int[] corpFlightBookings(int[][] bookings, int n) {
int[] delta = new int[n];
for (int[] booking : bookings) {
int first = booking[0], last = booking[1], seats = booking[2];
delta[first - 1] += seats;
if (last < n) {
delta[last] -= seats;
}
}
for (int i = 0; i < n - 1; ++i) {
delta[i + 1] += delta[i];
}
return delta;
}
}
/**
* @param {number[][]} bookings
* @param {number} n
* @return {number[]}
*/
var corpFlightBookings = function(bookings, n) {
let delta = new Array(n).fill(0);
for (let book of bookings) {
let [start, end, num] = book;
start -= 1;
delta[start] += num;
if (end != n) {
delta[end] -= num;
}
}
for (let i = 1; i < n; i++) {
delta[i] += delta[i - 1];
}
return delta;
};
class Solution {
public:
vector<int> corpFlightBookings(vector<vector<int>>& bookings, int n) {
vector<int> delta(n);
for (auto &booking : bookings) {
int first = booking[0], last = booking[1], seats = booking[2];
delta[first - 1] += seats;
if (last < n) {
delta[last] -= seats;
}
}
for (int i = 0; i < n - 1; ++i) {
delta[i + 1] += delta[i];
}
return delta;
}
};
/**
* @param {number[][]} bookings
* @param {number} n
* @return {number[]}
*/
var corpFlightBookings = function(bookings, n) {
let delta = new Array(n).fill(0);
for (let book of bookings) {
let [start, end, num] = book;
start -= 1;
delta[start] += num;
if (end != n) {
delta[end] -= num;
}
}
for (let i = 1; i < n; i++) {
delta[i] += delta[i - 1];
}
return delta;
};