Skip to content

Commit 2c96c8e

Browse files
authored
feat: add solutions to lc problem: No.2286 (doocs#3565)
No.2286.Booking Concert Tickets in Groups
1 parent 10ce174 commit 2c96c8e

File tree

4 files changed

+448
-5
lines changed

4 files changed

+448
-5
lines changed

solution/2200-2299/2286.Booking Concert Tickets in Groups/README.md

+138
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ bms.scatter(5, 1); // 返回 False
126126

127127
```python
128128
class Node:
129+
__slots__ = "l", "r", "s", "mx"
130+
129131
def __init__(self):
130132
self.l = self.r = 0
131133
self.s = self.mx = 0
@@ -629,6 +631,142 @@ func (t *segmentTree) pushup(u int) {
629631
*/
630632
```
631633

634+
#### TypeScript
635+
636+
```ts
637+
class Node {
638+
l: number;
639+
r: number;
640+
mx: number;
641+
s: number;
642+
643+
constructor() {
644+
this.l = 0;
645+
this.r = 0;
646+
this.mx = 0;
647+
this.s = 0;
648+
}
649+
}
650+
651+
class SegmentTree {
652+
private tr: Node[];
653+
private m: number;
654+
655+
constructor(n: number, m: number) {
656+
this.m = m;
657+
this.tr = Array.from({ length: n << 2 }, () => new Node());
658+
this.build(1, 1, n);
659+
}
660+
661+
private build(u: number, l: number, r: number): void {
662+
this.tr[u].l = l;
663+
this.tr[u].r = r;
664+
if (l === r) {
665+
this.tr[u].s = this.m;
666+
this.tr[u].mx = this.m;
667+
return;
668+
}
669+
const mid = (l + r) >> 1;
670+
this.build(u << 1, l, mid);
671+
this.build((u << 1) | 1, mid + 1, r);
672+
this.pushup(u);
673+
}
674+
675+
public modify(u: number, x: number, v: number): void {
676+
if (this.tr[u].l === x && this.tr[u].r === x) {
677+
this.tr[u].s = v;
678+
this.tr[u].mx = v;
679+
return;
680+
}
681+
const mid = (this.tr[u].l + this.tr[u].r) >> 1;
682+
if (x <= mid) {
683+
this.modify(u << 1, x, v);
684+
} else {
685+
this.modify((u << 1) | 1, x, v);
686+
}
687+
this.pushup(u);
688+
}
689+
690+
public querySum(u: number, l: number, r: number): number {
691+
if (this.tr[u].l >= l && this.tr[u].r <= r) {
692+
return this.tr[u].s;
693+
}
694+
const mid = (this.tr[u].l + this.tr[u].r) >> 1;
695+
let v = 0;
696+
if (l <= mid) {
697+
v += this.querySum(u << 1, l, r);
698+
}
699+
if (r > mid) {
700+
v += this.querySum((u << 1) | 1, l, r);
701+
}
702+
return v;
703+
}
704+
705+
public queryIdx(u: number, l: number, r: number, k: number): number {
706+
if (this.tr[u].mx < k) {
707+
return 0;
708+
}
709+
if (this.tr[u].l === this.tr[u].r) {
710+
return this.tr[u].l;
711+
}
712+
const mid = (this.tr[u].l + this.tr[u].r) >> 1;
713+
if (this.tr[u << 1].mx >= k) {
714+
return this.queryIdx(u << 1, l, r, k);
715+
}
716+
if (r > mid) {
717+
return this.queryIdx((u << 1) | 1, l, r, k);
718+
}
719+
return 0;
720+
}
721+
722+
private pushup(u: number): void {
723+
this.tr[u].s = this.tr[u << 1].s + this.tr[(u << 1) | 1].s;
724+
this.tr[u].mx = Math.max(this.tr[u << 1].mx, this.tr[(u << 1) | 1].mx);
725+
}
726+
}
727+
728+
class BookMyShow {
729+
private n: number;
730+
private m: number;
731+
private tree: SegmentTree;
732+
733+
constructor(n: number, m: number) {
734+
this.n = n;
735+
this.m = m;
736+
this.tree = new SegmentTree(n, m);
737+
}
738+
739+
public gather(k: number, maxRow: number): number[] {
740+
++maxRow;
741+
const i = this.tree.queryIdx(1, 1, maxRow, k);
742+
if (i === 0) {
743+
return [];
744+
}
745+
const s = this.tree.querySum(1, i, i);
746+
this.tree.modify(1, i, s - k);
747+
return [i - 1, this.m - s];
748+
}
749+
750+
public scatter(k: number, maxRow: number): boolean {
751+
++maxRow;
752+
if (this.tree.querySum(1, 1, maxRow) < k) {
753+
return false;
754+
}
755+
let i = this.tree.queryIdx(1, 1, maxRow, 1);
756+
for (let j = i; j <= this.n; ++j) {
757+
const s = this.tree.querySum(1, j, j);
758+
if (s >= k) {
759+
this.tree.modify(1, j, s - k);
760+
return true;
761+
}
762+
k -= s;
763+
this.tree.modify(1, j, 0);
764+
}
765+
return true;
766+
}
767+
}
768+
```
769+
632770
<!-- tabs:end -->
633771

634772
<!-- solution:end -->

solution/2200-2299/2286.Booking Concert Tickets in Groups/README_EN.md

+177-5
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ tags:
5454
[null, [0, 0], [], true, false]
5555

5656
<strong>Explanation</strong>
57-
BookMyShow bms = new BookMyShow(2, 5); // There are 2 rows with 5 seats each
57+
BookMyShow bms = new BookMyShow(2, 5); // There are 2 rows with 5 seats each
5858
bms.gather(4, 0); // return [0, 0]
59-
// The group books seats [0, 3] of row 0.
59+
// The group books seats [0, 3] of row 0.
6060
bms.gather(2, 0); // return []
6161
// There is only 1 seat left in row 0,
62-
// so it is not possible to book 2 consecutive seats.
62+
// so it is not possible to book 2 consecutive seats.
6363
bms.scatter(5, 1); // return True
64-
// The group books seat 4 of row 0 and seats [0, 3] of row 1.
64+
// The group books seat 4 of row 0 and seats [0, 3] of row 1.
6565
bms.scatter(5, 1); // return False
6666
// There is only one seat left in the hall.
6767
</pre>
@@ -82,14 +82,50 @@ bms.scatter(5, 1); // return False
8282

8383
<!-- solution:start -->
8484

85-
### Solution 1
85+
### Solution 1: Segment Tree
86+
87+
From the problem description, we can deduce the following:
88+
89+
- For the `gather(k, maxRow)` operation, the goal is to seat $k$ people on the same row with consecutive seats. In other words, we need to find the smallest row where the remaining seats are greater than or equal to $k$.
90+
- For the `scatter(k, maxRow)` operation, we just need to find $k$ seats in total, but we want to minimize the row number. Therefore, we need to find the first row that has more than $0$ seats remaining, allocate seats there, and continue searching for the rest.
91+
92+
We can implement this using a segment tree. Each segment tree node contains the following information:
93+
94+
- `l`: The left endpoint of the node's interval
95+
- `r`: The right endpoint of the node's interval
96+
- `s`: The total remaining seats in the interval corresponding to the node
97+
- `mx`: The maximum remaining seats in the interval corresponding to the node
98+
99+
Note that the index range for the segment tree starts from $1$.
100+
101+
The operations of the segment tree are as follows:
102+
103+
- `build(u, l, r)`: Builds node $u$, corresponding to the interval $[l, r]$, and recursively builds its left and right children.
104+
- `modify(u, x, v)`: Starting from node $u$, finds the first node corresponding to the interval $[l, r]$ where $l = r = x$, and modifies the `s` and `mx` values of this node to $v$, then updates the tree upwards.
105+
- `query_sum(u, l, r)`: Starting from node $u$, calculates the sum of `s` values in the interval $[l, r]$.
106+
- `query_idx(u, l, r, k)`: Starting from node $u$, finds the first node in the interval $[l, r]$ where `mx` is greater than or equal to $k$, and returns the left endpoint `l` of this node. When searching, we start from the largest interval $[1, maxRow]$. Since we need to find the leftmost node with `mx` greater than or equal to $k$, we check whether the `mx` of the first half of the interval meets the condition. If so, the answer is in the first half, and we recursively search that half. Otherwise, the answer is in the second half, and we search that half recursively.
107+
- `pushup(u)`: Updates the information of node $u$ using the information from its children.
108+
109+
For the `gather(k, maxRow)` operation, we first use `query_idx(1, 1, n, k)` to find the first row where the remaining seats are greater than or equal to $k$, denoted as $i$. Then, we use `query_sum(1, i, i)` to get the remaining seats in this row, denoted as $s$. Next, we use `modify(1, i, s - k)` to modify the remaining seats of this row to $s - k$, and update the tree upwards. Finally, we return the result $[i - 1, m - s]$.
110+
111+
For the `scatter(k, maxRow)` operation, we first use `query_sum(1, 1, maxRow)` to calculate the total remaining seats in the first $maxRow$ rows, denoted as $s$. If $s \lt k$, there are not enough seats, so we return `false`. Otherwise, we use `query_idx(1, 1, maxRow, 1)` to find the first row where the remaining seats are greater than or equal to $1$, denoted as $i$. Starting from this row, we use `query_sum(1, i, i)` to get the remaining seats in row $i$, denoted as $s_i$. If $s_i \geq k$, we directly use `modify(1, i, s_i - k)` to modify the remaining seats of this row to $s_i - k$, update the tree upwards, and return `true`. Otherwise, we update $k = k - s_i$, modify the remaining seats of this row to $0$, and update the tree upwards. Finally, we return `true`.
112+
113+
Time complexity:
114+
115+
- The initialization time complexity is $O(n)$.
116+
- The time complexity of `gather(k, maxRow)` is $O(\log n)$.
117+
- The time complexity of `scatter(k, maxRow)` is $O((n + q) \times \log n)$.
118+
119+
The overall time complexity is $O(n + q \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the number of rows, and $q$ is the number of operations.
86120

87121
<!-- tabs:start -->
88122

89123
#### Python3
90124

91125
```python
92126
class Node:
127+
__slots__ = "l", "r", "s", "mx"
128+
93129
def __init__(self):
94130
self.l = self.r = 0
95131
self.s = self.mx = 0
@@ -593,6 +629,142 @@ func (t *segmentTree) pushup(u int) {
593629
*/
594630
```
595631

632+
#### TypeScript
633+
634+
```ts
635+
class Node {
636+
l: number;
637+
r: number;
638+
mx: number;
639+
s: number;
640+
641+
constructor() {
642+
this.l = 0;
643+
this.r = 0;
644+
this.mx = 0;
645+
this.s = 0;
646+
}
647+
}
648+
649+
class SegmentTree {
650+
private tr: Node[];
651+
private m: number;
652+
653+
constructor(n: number, m: number) {
654+
this.m = m;
655+
this.tr = Array.from({ length: n << 2 }, () => new Node());
656+
this.build(1, 1, n);
657+
}
658+
659+
private build(u: number, l: number, r: number): void {
660+
this.tr[u].l = l;
661+
this.tr[u].r = r;
662+
if (l === r) {
663+
this.tr[u].s = this.m;
664+
this.tr[u].mx = this.m;
665+
return;
666+
}
667+
const mid = (l + r) >> 1;
668+
this.build(u << 1, l, mid);
669+
this.build((u << 1) | 1, mid + 1, r);
670+
this.pushup(u);
671+
}
672+
673+
public modify(u: number, x: number, v: number): void {
674+
if (this.tr[u].l === x && this.tr[u].r === x) {
675+
this.tr[u].s = v;
676+
this.tr[u].mx = v;
677+
return;
678+
}
679+
const mid = (this.tr[u].l + this.tr[u].r) >> 1;
680+
if (x <= mid) {
681+
this.modify(u << 1, x, v);
682+
} else {
683+
this.modify((u << 1) | 1, x, v);
684+
}
685+
this.pushup(u);
686+
}
687+
688+
public querySum(u: number, l: number, r: number): number {
689+
if (this.tr[u].l >= l && this.tr[u].r <= r) {
690+
return this.tr[u].s;
691+
}
692+
const mid = (this.tr[u].l + this.tr[u].r) >> 1;
693+
let v = 0;
694+
if (l <= mid) {
695+
v += this.querySum(u << 1, l, r);
696+
}
697+
if (r > mid) {
698+
v += this.querySum((u << 1) | 1, l, r);
699+
}
700+
return v;
701+
}
702+
703+
public queryIdx(u: number, l: number, r: number, k: number): number {
704+
if (this.tr[u].mx < k) {
705+
return 0;
706+
}
707+
if (this.tr[u].l === this.tr[u].r) {
708+
return this.tr[u].l;
709+
}
710+
const mid = (this.tr[u].l + this.tr[u].r) >> 1;
711+
if (this.tr[u << 1].mx >= k) {
712+
return this.queryIdx(u << 1, l, r, k);
713+
}
714+
if (r > mid) {
715+
return this.queryIdx((u << 1) | 1, l, r, k);
716+
}
717+
return 0;
718+
}
719+
720+
private pushup(u: number): void {
721+
this.tr[u].s = this.tr[u << 1].s + this.tr[(u << 1) | 1].s;
722+
this.tr[u].mx = Math.max(this.tr[u << 1].mx, this.tr[(u << 1) | 1].mx);
723+
}
724+
}
725+
726+
class BookMyShow {
727+
private n: number;
728+
private m: number;
729+
private tree: SegmentTree;
730+
731+
constructor(n: number, m: number) {
732+
this.n = n;
733+
this.m = m;
734+
this.tree = new SegmentTree(n, m);
735+
}
736+
737+
public gather(k: number, maxRow: number): number[] {
738+
++maxRow;
739+
const i = this.tree.queryIdx(1, 1, maxRow, k);
740+
if (i === 0) {
741+
return [];
742+
}
743+
const s = this.tree.querySum(1, i, i);
744+
this.tree.modify(1, i, s - k);
745+
return [i - 1, this.m - s];
746+
}
747+
748+
public scatter(k: number, maxRow: number): boolean {
749+
++maxRow;
750+
if (this.tree.querySum(1, 1, maxRow) < k) {
751+
return false;
752+
}
753+
let i = this.tree.queryIdx(1, 1, maxRow, 1);
754+
for (let j = i; j <= this.n; ++j) {
755+
const s = this.tree.querySum(1, j, j);
756+
if (s >= k) {
757+
this.tree.modify(1, j, s - k);
758+
return true;
759+
}
760+
k -= s;
761+
this.tree.modify(1, j, 0);
762+
}
763+
return true;
764+
}
765+
}
766+
```
767+
596768
<!-- tabs:end -->
597769

598770
<!-- solution:end -->

solution/2200-2299/2286.Booking Concert Tickets in Groups/Solution.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class Node:
2+
__slots__ = "l", "r", "s", "mx"
3+
24
def __init__(self):
35
self.l = self.r = 0
46
self.s = self.mx = 0

0 commit comments

Comments
 (0)