57
57
58
58
<!-- 这里可写通用的实现逻辑 -->
59
59
60
- 差分数组。
60
+ ** 方法一:差分数组**
61
+
62
+ 时间复杂度 O(n)。
63
+
64
+ ** 方法二:树状数组 + 差分思想**
65
+
66
+ 时间复杂度 O(nlogn)。
61
67
62
68
<!-- tabs:start -->
63
69
64
70
### ** Python3**
65
71
66
72
<!-- 这里可写当前语言的特殊实现逻辑 -->
67
73
74
+ 差分数组:
75
+
68
76
``` python
69
77
class Solution :
70
78
def corpFlightBookings (self , bookings : List[List[int ]], n : int ) -> List[int ]:
@@ -78,10 +86,46 @@ class Solution:
78
86
return delta
79
87
```
80
88
89
+ 树状数组:
90
+
91
+ ``` python
92
+ class BinaryIndexedTree :
93
+ def __init__ (self , n ):
94
+ self .n = n
95
+ self .c = [0 ] * (n + 1 )
96
+
97
+ @ staticmethod
98
+ def lowbit (x ):
99
+ return x & - x
100
+
101
+ def update (self , x , delta ):
102
+ while x <= self .n:
103
+ self .c[x] += delta
104
+ x += BinaryIndexedTree.lowbit(x)
105
+
106
+ def query (self , x ):
107
+ s = 0
108
+ while x:
109
+ s += self .c[x]
110
+ x -= BinaryIndexedTree.lowbit(x)
111
+ return s
112
+
113
+
114
+ class Solution :
115
+ def corpFlightBookings (self , bookings : List[List[int ]], n : int ) -> List[int ]:
116
+ tree = BinaryIndexedTree(n)
117
+ for first, last, seats in bookings:
118
+ tree.update(first, seats)
119
+ tree.update(last + 1 , - seats)
120
+ return [tree.query(i + 1 ) for i in range (n)]
121
+ ```
122
+
81
123
### ** Java**
82
124
83
125
<!-- 这里可写当前语言的特殊实现逻辑 -->
84
126
127
+ 差分数组:
128
+
85
129
``` java
86
130
class Solution {
87
131
public int [] corpFlightBookings (int [][] bookings , int n ) {
@@ -101,8 +145,60 @@ class Solution {
101
145
}
102
146
```
103
147
148
+ 树状数组:
149
+
150
+ ``` java
151
+ class Solution {
152
+ public int [] corpFlightBookings (int [][] bookings , int n ) {
153
+ BinaryIndexedTree tree = new BinaryIndexedTree (n);
154
+ for (int [] booking : bookings) {
155
+ int first = booking[0 ], last = booking[1 ], seats = booking[2 ];
156
+ tree. update(first, seats);
157
+ tree. update(last + 1 , - seats);
158
+ }
159
+ int [] ans = new int [n];
160
+ for (int i = 0 ; i < n; ++ i) {
161
+ ans[i] = tree. query(i + 1 );
162
+ }
163
+ return ans;
164
+ }
165
+ }
166
+
167
+ class BinaryIndexedTree {
168
+ private int n;
169
+ private int [] c;
170
+
171
+ public BinaryIndexedTree (int n ) {
172
+ this . n = n;
173
+ c = new int [n + 1 ];
174
+ }
175
+
176
+ public void update (int x , int delta ) {
177
+ while (x <= n) {
178
+ c[x] += delta;
179
+ x += lowbit(x);
180
+ }
181
+ }
182
+
183
+ public int query (int x ) {
184
+ int s = 0 ;
185
+ while (x > 0 ) {
186
+ s += c[x];
187
+ x -= lowbit(x);
188
+ }
189
+ return s;
190
+ }
191
+
192
+ public static int lowbit (int x ) {
193
+ return x & - x;
194
+ }
195
+ }
196
+ ```
197
+
104
198
### ** JavaScript**
105
199
200
+ 差分数组:
201
+
106
202
``` js
107
203
/**
108
204
* @param {number[][]} bookings
@@ -128,6 +224,8 @@ var corpFlightBookings = function (bookings, n) {
128
224
129
225
### ** C++**
130
226
227
+ 差分数组:
228
+
131
229
``` cpp
132
230
class Solution {
133
231
public:
@@ -148,8 +246,60 @@ public:
148
246
};
149
247
```
150
248
249
+ 树状数组:
250
+
251
+ ```cpp
252
+ class BinaryIndexedTree {
253
+ public:
254
+ int n;
255
+ vector<int> c;
256
+
257
+ BinaryIndexedTree(int _n): n(_n), c(_n + 1){}
258
+
259
+ void update(int x, int delta) {
260
+ while (x <= n)
261
+ {
262
+ c[x] += delta;
263
+ x += lowbit(x);
264
+ }
265
+ }
266
+
267
+ int query(int x) {
268
+ int s = 0;
269
+ while (x > 0)
270
+ {
271
+ s += c[x];
272
+ x -= lowbit(x);
273
+ }
274
+ return s;
275
+ }
276
+
277
+ int lowbit(int x) {
278
+ return x & -x;
279
+ }
280
+ };
281
+
282
+ class Solution {
283
+ public:
284
+ vector<int> corpFlightBookings(vector<vector<int>>& bookings, int n) {
285
+ BinaryIndexedTree* tree = new BinaryIndexedTree(n);
286
+ for (auto& booking : bookings)
287
+ {
288
+ int first = booking[0], last = booking[1], seats = booking[2];
289
+ tree->update(first, seats);
290
+ tree->update(last + 1, -seats);
291
+ }
292
+ vector<int> ans;
293
+ for (int i = 0; i < n; ++i) ans.push_back(tree->query(i + 1));
294
+ return ans;
295
+ }
296
+ };
297
+ ```
298
+
151
299
### ** Go**
152
300
301
+ 差分数组:
302
+
153
303
``` go
154
304
func corpFlightBookings (bookings [][]int , n int ) []int {
155
305
delta := make ([]int , n)
@@ -167,6 +317,54 @@ func corpFlightBookings(bookings [][]int, n int) []int {
167
317
}
168
318
```
169
319
320
+ 树状数组:
321
+
322
+ ``` go
323
+ type BinaryIndexedTree struct {
324
+ n int
325
+ c []int
326
+ }
327
+
328
+ func newBinaryIndexedTree (n int ) *BinaryIndexedTree {
329
+ c := make ([]int , n+1 )
330
+ return &BinaryIndexedTree{n, c}
331
+ }
332
+
333
+ func (this *BinaryIndexedTree ) lowbit (x int ) int {
334
+ return x & -x
335
+ }
336
+
337
+ func (this *BinaryIndexedTree ) update (x , delta int ) {
338
+ for x <= this.n {
339
+ this.c [x] += delta
340
+ x += this.lowbit (x)
341
+ }
342
+ }
343
+
344
+ func (this *BinaryIndexedTree ) query (x int ) int {
345
+ s := 0
346
+ for x > 0 {
347
+ s += this.c [x]
348
+ x -= this.lowbit (x)
349
+ }
350
+ return s
351
+ }
352
+
353
+ func corpFlightBookings (bookings [][]int , n int ) []int {
354
+ tree := newBinaryIndexedTree (n)
355
+ for _ , booking := range bookings {
356
+ first , last , seats := booking[0 ], booking[1 ], booking[2 ]
357
+ tree.update (first, seats)
358
+ tree.update (last+1 , -seats)
359
+ }
360
+ ans := make ([]int , n)
361
+ for i := range ans {
362
+ ans[i] = tree.query (i + 1 )
363
+ }
364
+ return ans
365
+ }
366
+ ```
367
+
170
368
### ** ...**
171
369
172
370
```
0 commit comments