[LeetCode] Corporate Flight Bookings

1109. Corporate Flight Bookings

There are n flights that are labeled from 1 to n.

You are given an array of flight bookings bookings, where bookings[i] = [firsti, lasti, seatsi] represents a booking for flights firsti through lasti (inclusive) with seatsi seats reserved for each flight in the range.

Return an array answer of length n, where answer[i] is the total number of seats reserved for flight i.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
vector<int> corpFlightBookings(vector<vector<int>>& bookings, int n) {
vector<int> A(n + 1), res(n);
for(auto& b : bookings) {
A[b[0] - 1] += b[2];
A[b[1]] -= b[2];
}
for(int i = 0; i < n; i++) {
res[i] = A[i] + (i - 1 < 0 ? 0 :res[i - 1]);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/04/21/PS/LeetCode/corporate-flight-bookings/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.