Skip to content

Commit 079c84f

Browse files
committed
feat: update solutions to lc problem: No.1109
No.1109.Corporate Flight Bookings
1 parent 1d83eac commit 079c84f

File tree

7 files changed

+191
-235
lines changed

7 files changed

+191
-235
lines changed

solution/1100-1199/1109.Corporate Flight Bookings/README.md

Lines changed: 82 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,17 @@
5959

6060
**方法一:差分数组**
6161

62-
时间复杂度 O(n)。
62+
我们注意到,每一次预订都是在某个区间 `[first, last]` 内的所有航班上预订了 `seats` 个座位。因此,我们可以利用差分数组的思想,对于每一次预订,将 `first` 位置的数加上 `seats`,将 `last + 1` 位置的数减去 `seats`。最后,对差分数组求前缀和,即可得到每个航班预定的座位总数。
63+
64+
时间复杂度 $O(n)$,其中 $n$ 为航班数。忽略答案的空间消耗,空间复杂度 $O(1)$。
6365

6466
**方法二:树状数组 + 差分思想**
6567

66-
时间复杂度 O(nlogn)。
68+
我们也可以利用树状数组,结合差分的思想,来实现上述操作。我们可以将每一次预订看作是在某个区间 `[first, last]` 内的所有航班上预订了 `seats` 个座位。因此,我们可以对每一次预订,对树状数组的 `first` 位置加上 `seats`,对树状数组的 `last + 1` 位置减去 `seats`。最后,对树状数组每个位置求前缀和,即可得到每个航班预定的座位总数。
69+
70+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为航班数。
71+
72+
以下是树状数组的基本介绍:
6773

6874
树状数组,也称作“二叉索引树”(Binary Indexed Tree)或 Fenwick 树。 它可以高效地实现如下两个操作:
6975

@@ -78,41 +84,33 @@
7884

7985
<!-- 这里可写当前语言的特殊实现逻辑 -->
8086

81-
差分数组:
82-
8387
```python
8488
class Solution:
8589
def corpFlightBookings(self, bookings: List[List[int]], n: int) -> List[int]:
86-
delta = [0] * n
90+
ans = [0] * n
8791
for first, last, seats in bookings:
88-
delta[first - 1] += seats
92+
ans[first - 1] += seats
8993
if last < n:
90-
delta[last] -= seats
91-
return list(accumulate(delta))
94+
ans[last] -= seats
95+
return list(accumulate(ans))
9296
```
9397

94-
树状数组:
95-
9698
```python
9799
class BinaryIndexedTree:
98100
def __init__(self, n):
99101
self.n = n
100102
self.c = [0] * (n + 1)
101103

102-
@staticmethod
103-
def lowbit(x):
104-
return x & -x
105-
106104
def update(self, x, delta):
107105
while x <= self.n:
108106
self.c[x] += delta
109-
x += BinaryIndexedTree.lowbit(x)
107+
x += x & -x
110108

111109
def query(self, x):
112110
s = 0
113111
while x:
114112
s += self.c[x]
115-
x -= BinaryIndexedTree.lowbit(x)
113+
x -= x & -x
116114
return s
117115

118116

@@ -129,35 +127,31 @@ class Solution:
129127

130128
<!-- 这里可写当前语言的特殊实现逻辑 -->
131129

132-
差分数组:
133-
134130
```java
135131
class Solution {
136132
public int[] corpFlightBookings(int[][] bookings, int n) {
137-
int[] delta = new int[n];
138-
for (int[] booking : bookings) {
139-
int first = booking[0], last = booking[1], seats = booking[2];
140-
delta[first - 1] += seats;
133+
int[] ans = new int[n];
134+
for (var e : bookings) {
135+
int first = e[0], last = e[1], seats = e[2];
136+
ans[first - 1] += seats;
141137
if (last < n) {
142-
delta[last] -= seats;
138+
ans[last] -= seats;
143139
}
144140
}
145-
for (int i = 0; i < n - 1; ++i) {
146-
delta[i + 1] += delta[i];
141+
for (int i = 1; i < n; ++i) {
142+
ans[i] += ans[i - 1];
147143
}
148-
return delta;
144+
return ans;
149145
}
150146
}
151147
```
152148

153-
树状数组:
154-
155149
```java
156150
class Solution {
157151
public int[] corpFlightBookings(int[][] bookings, int n) {
158152
BinaryIndexedTree tree = new BinaryIndexedTree(n);
159-
for (int[] booking : bookings) {
160-
int first = booking[0], last = booking[1], seats = booking[2];
153+
for (var e : bookings) {
154+
int first =e[0], last = e[1], seats = e[2];
161155
tree.update(first, seats);
162156
tree.update(last + 1, -seats);
163157
}
@@ -181,147 +175,106 @@ class BinaryIndexedTree {
181175
public void update(int x, int delta) {
182176
while (x <= n) {
183177
c[x] += delta;
184-
x += lowbit(x);
178+
x += x & -x;
185179
}
186180
}
187181

188182
public int query(int x) {
189183
int s = 0;
190184
while (x > 0) {
191185
s += c[x];
192-
x -= lowbit(x);
186+
x -= x & -x;
193187
}
194188
return s;
195189
}
196-
197-
public static int lowbit(int x) {
198-
return x & -x;
199-
}
200190
}
201191
```
202192

203-
### **JavaScript**
204-
205-
差分数组:
206-
207-
```js
208-
/**
209-
* @param {number[][]} bookings
210-
* @param {number} n
211-
* @return {number[]}
212-
*/
213-
var corpFlightBookings = function (bookings, n) {
214-
let delta = new Array(n).fill(0);
215-
for (let [start, end, num] of bookings) {
216-
delta[start - 1] += num;
217-
if (end != n) {
218-
delta[end] -= num;
219-
}
220-
}
221-
for (let i = 1; i < n; ++i) {
222-
delta[i] += delta[i - 1];
223-
}
224-
return delta;
225-
};
226-
```
227-
228193
### **C++**
229194

230-
差分数组:
231-
232195
```cpp
233196
class Solution {
234197
public:
235198
vector<int> corpFlightBookings(vector<vector<int>>& bookings, int n) {
236-
vector<int> delta(n);
237-
for (auto& booking : bookings) {
238-
int first = booking[0], last = booking[1], seats = booking[2];
239-
delta[first - 1] += seats;
199+
vector<int> ans(n);
200+
for (auto& e : bookings) {
201+
int first = e[0], last = e[1], seats = e[2];
202+
ans[first - 1] += seats;
240203
if (last < n) {
241-
delta[last] -= seats;
204+
ans[last] -= seats;
242205
}
243206
}
244-
for (int i = 0; i < n - 1; ++i) {
245-
delta[i + 1] += delta[i];
207+
for (int i = 1; i < n; ++i) {
208+
ans[i] += ans[i - 1];
246209
}
247-
return delta;
210+
return ans;
248211
}
249212
};
250213
```
251214
252-
树状数组:
253-
254215
```cpp
255216
class BinaryIndexedTree {
256217
public:
257-
int n;
258-
vector<int> c;
259-
260218
BinaryIndexedTree(int _n): n(_n), c(_n + 1){}
261219
262220
void update(int x, int delta) {
263-
while (x <= n)
264-
{
221+
while (x <= n) {
265222
c[x] += delta;
266-
x += lowbit(x);
223+
x += x & -x;
267224
}
268225
}
269226
270227
int query(int x) {
271228
int s = 0;
272-
while (x > 0)
273-
{
229+
while (x) {
274230
s += c[x];
275-
x -= lowbit(x);
231+
x -= x & -x;
276232
}
277233
return s;
278234
}
279235
280-
int lowbit(int x) {
281-
return x & -x;
282-
}
236+
private:
237+
int n;
238+
vector<int> c;
283239
};
284240
285241
class Solution {
286242
public:
287243
vector<int> corpFlightBookings(vector<vector<int>>& bookings, int n) {
288244
BinaryIndexedTree* tree = new BinaryIndexedTree(n);
289-
for (auto& booking : bookings)
290-
{
291-
int first = booking[0], last = booking[1], seats = booking[2];
245+
for (auto& e : bookings) {
246+
int first = e[0], last = e[1], seats = e[2];
292247
tree->update(first, seats);
293248
tree->update(last + 1, -seats);
294249
}
295-
vector<int> ans;
296-
for (int i = 0; i < n; ++i) ans.push_back(tree->query(i + 1));
250+
vector<int> ans(n);
251+
for (int i = 0; i < n; ++i) {
252+
ans[i] = tree->query(i + 1);
253+
}
297254
return ans;
298255
}
299256
};
300257
```
301258

302259
### **Go**
303260

304-
差分数组:
305-
306261
```go
307262
func corpFlightBookings(bookings [][]int, n int) []int {
308-
delta := make([]int, n)
309-
for _, booking := range bookings {
310-
first, last, seats := booking[0], booking[1], booking[2]
311-
delta[first-1] += seats
263+
ans := make([]int, n)
264+
for _, e := range bookings {
265+
first, last, seats := e[0], e[1], e[2]
266+
ans[first-1] += seats
312267
if last < n {
313-
delta[last] -= seats
268+
ans[last] -= seats
314269
}
315270
}
316-
for i := 0; i < n-1; i++ {
317-
delta[i+1] += delta[i]
271+
for i := 1; i < n; i++ {
272+
ans[i] += ans[i-1]
318273
}
319-
return delta
274+
return ans
320275
}
321276
```
322277

323-
树状数组:
324-
325278
```go
326279
type BinaryIndexedTree struct {
327280
n int
@@ -333,30 +286,26 @@ func newBinaryIndexedTree(n int) *BinaryIndexedTree {
333286
return &BinaryIndexedTree{n, c}
334287
}
335288

336-
func (this *BinaryIndexedTree) lowbit(x int) int {
337-
return x & -x
338-
}
339-
340289
func (this *BinaryIndexedTree) update(x, delta int) {
341290
for x <= this.n {
342291
this.c[x] += delta
343-
x += this.lowbit(x)
292+
x += x & -x
344293
}
345294
}
346295

347296
func (this *BinaryIndexedTree) query(x int) int {
348297
s := 0
349298
for x > 0 {
350299
s += this.c[x]
351-
x -= this.lowbit(x)
300+
x -= x & -x
352301
}
353302
return s
354303
}
355304

356305
func corpFlightBookings(bookings [][]int, n int) []int {
357306
tree := newBinaryIndexedTree(n)
358-
for _, booking := range bookings {
359-
first, last, seats := booking[0], booking[1], booking[2]
307+
for _, e := range bookings {
308+
first, last, seats := e[0], e[1], e[2]
360309
tree.update(first, seats)
361310
tree.update(last+1, -seats)
362311
}
@@ -368,6 +317,29 @@ func corpFlightBookings(bookings [][]int, n int) []int {
368317
}
369318
```
370319

320+
### **JavaScript**
321+
322+
```js
323+
/**
324+
* @param {number[][]} bookings
325+
* @param {number} n
326+
* @return {number[]}
327+
*/
328+
var corpFlightBookings = function (bookings, n) {
329+
const ans = new Array(n).fill(0);
330+
for (const [first, last, seats] of bookings) {
331+
ans[first - 1] += seats;
332+
if (last < n) {
333+
ans[last] -= seats;
334+
}
335+
}
336+
for (let i = 1; i < n; ++i) {
337+
ans[i] += ans[i - 1];
338+
}
339+
return ans;
340+
};
341+
```
342+
371343
### **...**
372344

373345
```

0 commit comments

Comments
 (0)