59
59
60
60
** 方法一:差分数组**
61
61
62
- 时间复杂度 O(n)。
62
+ 我们注意到,每一次预订都是在某个区间 ` [first, last] ` 内的所有航班上预订了 ` seats ` 个座位。因此,我们可以利用差分数组的思想,对于每一次预订,将 ` first ` 位置的数加上 ` seats ` ,将 ` last + 1 ` 位置的数减去 ` seats ` 。最后,对差分数组求前缀和,即可得到每个航班预定的座位总数。
63
+
64
+ 时间复杂度 $O(n)$,其中 $n$ 为航班数。忽略答案的空间消耗,空间复杂度 $O(1)$。
63
65
64
66
** 方法二:树状数组 + 差分思想**
65
67
66
- 时间复杂度 O(nlogn)。
68
+ 我们也可以利用树状数组,结合差分的思想,来实现上述操作。我们可以将每一次预订看作是在某个区间 ` [first, last] ` 内的所有航班上预订了 ` seats ` 个座位。因此,我们可以对每一次预订,对树状数组的 ` first ` 位置加上 ` seats ` ,对树状数组的 ` last + 1 ` 位置减去 ` seats ` 。最后,对树状数组每个位置求前缀和,即可得到每个航班预定的座位总数。
69
+
70
+ 时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为航班数。
71
+
72
+ 以下是树状数组的基本介绍:
67
73
68
74
树状数组,也称作“二叉索引树”(Binary Indexed Tree)或 Fenwick 树。 它可以高效地实现如下两个操作:
69
75
78
84
79
85
<!-- 这里可写当前语言的特殊实现逻辑 -->
80
86
81
- 差分数组:
82
-
83
87
``` python
84
88
class Solution :
85
89
def corpFlightBookings (self , bookings : List[List[int ]], n : int ) -> List[int ]:
86
- delta = [0 ] * n
90
+ ans = [0 ] * n
87
91
for first, last, seats in bookings:
88
- delta [first - 1 ] += seats
92
+ ans [first - 1 ] += seats
89
93
if last < n:
90
- delta [last] -= seats
91
- return list (accumulate(delta ))
94
+ ans [last] -= seats
95
+ return list (accumulate(ans ))
92
96
```
93
97
94
- 树状数组:
95
-
96
98
``` python
97
99
class BinaryIndexedTree :
98
100
def __init__ (self , n ):
99
101
self .n = n
100
102
self .c = [0 ] * (n + 1 )
101
103
102
- @ staticmethod
103
- def lowbit (x ):
104
- return x & - x
105
-
106
104
def update (self , x , delta ):
107
105
while x <= self .n:
108
106
self .c[x] += delta
109
- x += BinaryIndexedTree.lowbit(x)
107
+ x += x & - x
110
108
111
109
def query (self , x ):
112
110
s = 0
113
111
while x:
114
112
s += self .c[x]
115
- x -= BinaryIndexedTree.lowbit(x)
113
+ x -= x & - x
116
114
return s
117
115
118
116
@@ -129,35 +127,31 @@ class Solution:
129
127
130
128
<!-- 这里可写当前语言的特殊实现逻辑 -->
131
129
132
- 差分数组:
133
-
134
130
``` java
135
131
class Solution {
136
132
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;
141
137
if (last < n) {
142
- delta [last] -= seats;
138
+ ans [last] -= seats;
143
139
}
144
140
}
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 ];
147
143
}
148
- return delta ;
144
+ return ans ;
149
145
}
150
146
}
151
147
```
152
148
153
- 树状数组:
154
-
155
149
``` java
156
150
class Solution {
157
151
public int [] corpFlightBookings (int [][] bookings , int n ) {
158
152
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 ];
161
155
tree. update(first, seats);
162
156
tree. update(last + 1 , - seats);
163
157
}
@@ -181,147 +175,106 @@ class BinaryIndexedTree {
181
175
public void update (int x , int delta ) {
182
176
while (x <= n) {
183
177
c[x] += delta;
184
- x += lowbit(x) ;
178
+ x += x & - x ;
185
179
}
186
180
}
187
181
188
182
public int query (int x ) {
189
183
int s = 0 ;
190
184
while (x > 0 ) {
191
185
s += c[x];
192
- x -= lowbit(x) ;
186
+ x -= x & - x ;
193
187
}
194
188
return s;
195
189
}
196
-
197
- public static int lowbit (int x ) {
198
- return x & - x;
199
- }
200
190
}
201
191
```
202
192
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
-
228
193
### ** C++**
229
194
230
- 差分数组:
231
-
232
195
``` cpp
233
196
class Solution {
234
197
public:
235
198
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;
240
203
if (last < n) {
241
- delta [ last] -= seats;
204
+ ans [ last] -= seats;
242
205
}
243
206
}
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 ] ;
246
209
}
247
- return delta ;
210
+ return ans ;
248
211
}
249
212
};
250
213
```
251
214
252
- 树状数组:
253
-
254
215
```cpp
255
216
class BinaryIndexedTree {
256
217
public:
257
- int n;
258
- vector<int> c;
259
-
260
218
BinaryIndexedTree(int _n): n(_n), c(_n + 1){}
261
219
262
220
void update(int x, int delta) {
263
- while (x <= n)
264
- {
221
+ while (x <= n) {
265
222
c[x] += delta;
266
- x += lowbit(x) ;
223
+ x += x & -x ;
267
224
}
268
225
}
269
226
270
227
int query(int x) {
271
228
int s = 0;
272
- while (x > 0)
273
- {
229
+ while (x) {
274
230
s += c[x];
275
- x -= lowbit(x) ;
231
+ x -= x & -x ;
276
232
}
277
233
return s;
278
234
}
279
235
280
- int lowbit(int x) {
281
- return x & -x ;
282
- }
236
+ private:
237
+ int n ;
238
+ vector<int> c;
283
239
};
284
240
285
241
class Solution {
286
242
public:
287
243
vector<int> corpFlightBookings(vector<vector<int>>& bookings, int n) {
288
244
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];
292
247
tree->update(first, seats);
293
248
tree->update(last + 1, -seats);
294
249
}
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
+ }
297
254
return ans;
298
255
}
299
256
};
300
257
```
301
258
302
259
### ** Go**
303
260
304
- 差分数组:
305
-
306
261
``` go
307
262
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
312
267
if last < n {
313
- delta [last] -= seats
268
+ ans [last] -= seats
314
269
}
315
270
}
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 ]
318
273
}
319
- return delta
274
+ return ans
320
275
}
321
276
```
322
277
323
- 树状数组:
324
-
325
278
``` go
326
279
type BinaryIndexedTree struct {
327
280
n int
@@ -333,30 +286,26 @@ func newBinaryIndexedTree(n int) *BinaryIndexedTree {
333
286
return &BinaryIndexedTree{n, c}
334
287
}
335
288
336
- func (this *BinaryIndexedTree ) lowbit (x int ) int {
337
- return x & -x
338
- }
339
-
340
289
func (this *BinaryIndexedTree ) update (x , delta int ) {
341
290
for x <= this.n {
342
291
this.c [x] += delta
343
- x += this. lowbit (x)
292
+ x += x & -x
344
293
}
345
294
}
346
295
347
296
func (this *BinaryIndexedTree ) query (x int ) int {
348
297
s := 0
349
298
for x > 0 {
350
299
s += this.c [x]
351
- x -= this. lowbit (x)
300
+ x -= x & -x
352
301
}
353
302
return s
354
303
}
355
304
356
305
func corpFlightBookings (bookings [][]int , n int ) []int {
357
306
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 ]
360
309
tree.update (first, seats)
361
310
tree.update (last+1 , -seats)
362
311
}
@@ -368,6 +317,29 @@ func corpFlightBookings(bookings [][]int, n int) []int {
368
317
}
369
318
```
370
319
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
+
371
343
### ** ...**
372
344
373
345
```
0 commit comments