这里有 n
个航班,它们分别从 1
到 n
进行编号。
有一份航班预订表 bookings
,表中第 i
条预订记录 bookings[i] = [firsti, lasti, seatsi]
意味着在从 firsti
到 lasti
(包含 firsti
和 lasti
)的 每个航班 上预订了 seatsi
个座位。
请你返回一个长度为 n
的数组 answer
,里面的元素是每个航班预定的座位总数。
示例 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
方法一:差分数组
时间复杂度 O(n)。
方法二:树状数组 + 差分思想
时间复杂度 O(nlogn)。
树状数组,也称作“二叉索引树”(Binary Indexed Tree)或 Fenwick 树。 它可以高效地实现如下两个操作:
- 单点更新
update(x, delta)
: 把序列 x 位置的数加上一个值 delta; - 前缀和查询
query(x)
:查询序列[1,...x]
区间的区间和,即位置 x 的前缀和。
这两个操作的时间复杂度均为 O(log n)
。
差分数组:
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
return list(accumulate(delta))
树状数组:
class BinaryIndexedTree:
def __init__(self, n):
self.n = n
self.c = [0] * (n + 1)
@staticmethod
def lowbit(x):
return x & -x
def update(self, x, delta):
while x <= self.n:
self.c[x] += delta
x += BinaryIndexedTree.lowbit(x)
def query(self, x):
s = 0
while x:
s += self.c[x]
x -= BinaryIndexedTree.lowbit(x)
return s
class Solution:
def corpFlightBookings(self, bookings: List[List[int]], n: int) -> List[int]:
tree = BinaryIndexedTree(n)
for first, last, seats in bookings:
tree.update(first, seats)
tree.update(last + 1, -seats)
return [tree.query(i + 1) for i in range(n)]
差分数组:
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;
}
}
树状数组:
class Solution {
public int[] corpFlightBookings(int[][] bookings, int n) {
BinaryIndexedTree tree = new BinaryIndexedTree(n);
for (int[] booking : bookings) {
int first = booking[0], last = booking[1], seats = booking[2];
tree.update(first, seats);
tree.update(last + 1, -seats);
}
int[] ans = new int[n];
for (int i = 0; i < n; ++i) {
ans[i] = tree.query(i + 1);
}
return ans;
}
}
class BinaryIndexedTree {
private int n;
private int[] c;
public BinaryIndexedTree(int n) {
this.n = n;
c = new int[n + 1];
}
public void update(int x, int delta) {
while (x <= n) {
c[x] += delta;
x += lowbit(x);
}
}
public int query(int x) {
int s = 0;
while (x > 0) {
s += c[x];
x -= lowbit(x);
}
return s;
}
public static int lowbit(int x) {
return x & -x;
}
}
差分数组:
/**
* @param {number[][]} bookings
* @param {number} n
* @return {number[]}
*/
var corpFlightBookings = function (bookings, n) {
let delta = new Array(n).fill(0);
for (let [start, end, num] of bookings) {
delta[start - 1] += 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;
}
};
树状数组:
class BinaryIndexedTree {
public:
int n;
vector<int> c;
BinaryIndexedTree(int _n): n(_n), c(_n + 1){}
void update(int x, int delta) {
while (x <= n)
{
c[x] += delta;
x += lowbit(x);
}
}
int query(int x) {
int s = 0;
while (x > 0)
{
s += c[x];
x -= lowbit(x);
}
return s;
}
int lowbit(int x) {
return x & -x;
}
};
class Solution {
public:
vector<int> corpFlightBookings(vector<vector<int>>& bookings, int n) {
BinaryIndexedTree* tree = new BinaryIndexedTree(n);
for (auto& booking : bookings)
{
int first = booking[0], last = booking[1], seats = booking[2];
tree->update(first, seats);
tree->update(last + 1, -seats);
}
vector<int> ans;
for (int i = 0; i < n; ++i) ans.push_back(tree->query(i + 1));
return ans;
}
};
差分数组:
func corpFlightBookings(bookings [][]int, n int) []int {
delta := make([]int, n)
for _, booking := range bookings {
first, last, seats := booking[0], booking[1], booking[2]
delta[first-1] += seats
if last < n {
delta[last] -= seats
}
}
for i := 0; i < n-1; i++ {
delta[i+1] += delta[i]
}
return delta
}
树状数组:
type BinaryIndexedTree struct {
n int
c []int
}
func newBinaryIndexedTree(n int) *BinaryIndexedTree {
c := make([]int, n+1)
return &BinaryIndexedTree{n, c}
}
func (this *BinaryIndexedTree) lowbit(x int) int {
return x & -x
}
func (this *BinaryIndexedTree) update(x, delta int) {
for x <= this.n {
this.c[x] += delta
x += this.lowbit(x)
}
}
func (this *BinaryIndexedTree) query(x int) int {
s := 0
for x > 0 {
s += this.c[x]
x -= this.lowbit(x)
}
return s
}
func corpFlightBookings(bookings [][]int, n int) []int {
tree := newBinaryIndexedTree(n)
for _, booking := range bookings {
first, last, seats := booking[0], booking[1], booking[2]
tree.update(first, seats)
tree.update(last+1, -seats)
}
ans := make([]int, n)
for i := range ans {
ans[i] = tree.query(i + 1)
}
return ans
}