Skip to content

Commit 9568e38

Browse files
committed
feat: add solutions to lc problem: No.1109
No.1109.Corporate Flight Bookings
1 parent 6989d6b commit 9568e38

File tree

2 files changed

+373
-1
lines changed

2 files changed

+373
-1
lines changed

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

Lines changed: 199 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,22 @@
5757

5858
<!-- 这里可写通用的实现逻辑 -->
5959

60-
差分数组。
60+
**方法一:差分数组**
61+
62+
时间复杂度 O(n)。
63+
64+
**方法二:树状数组 + 差分思想**
65+
66+
时间复杂度 O(nlogn)。
6167

6268
<!-- tabs:start -->
6369

6470
### **Python3**
6571

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

74+
差分数组:
75+
6876
```python
6977
class Solution:
7078
def corpFlightBookings(self, bookings: List[List[int]], n: int) -> List[int]:
@@ -78,10 +86,46 @@ class Solution:
7886
return delta
7987
```
8088

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+
81123
### **Java**
82124

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

127+
差分数组:
128+
85129
```java
86130
class Solution {
87131
public int[] corpFlightBookings(int[][] bookings, int n) {
@@ -101,8 +145,60 @@ class Solution {
101145
}
102146
```
103147

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+
104198
### **JavaScript**
105199

200+
差分数组:
201+
106202
```js
107203
/**
108204
* @param {number[][]} bookings
@@ -128,6 +224,8 @@ var corpFlightBookings = function (bookings, n) {
128224

129225
### **C++**
130226

227+
差分数组:
228+
131229
```cpp
132230
class Solution {
133231
public:
@@ -148,8 +246,60 @@ public:
148246
};
149247
```
150248
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+
151299
### **Go**
152300

301+
差分数组:
302+
153303
```go
154304
func corpFlightBookings(bookings [][]int, n int) []int {
155305
delta := make([]int, n)
@@ -167,6 +317,54 @@ func corpFlightBookings(bookings [][]int, n int) []int {
167317
}
168318
```
169319

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+
170368
### **...**
171369

172370
```

0 commit comments

Comments
 (0)