-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2286-booking-concert-tickets-in-groups.js
More file actions
184 lines (168 loc) · 5.68 KB
/
2286-booking-concert-tickets-in-groups.js
File metadata and controls
184 lines (168 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* 2286. Booking Concert Tickets in Groups
* https://leetcode.com/problems/booking-concert-tickets-in-groups/
* Difficulty: Hard
*
* A concert hall has n rows numbered from 0 to n - 1, each with m seats, numbered from
* 0 to m - 1. You need to design a ticketing system that can allocate seats in the following cases:
* - If a group of k spectators can sit together in a row.
* - If every member of a group of k spectators can get a seat. They may or may not sit together.
*
* Note that the spectators are very picky. Hence:
* - They will book seats only if each member of their group can get a seat with row number less
* than or equal to maxRow. maxRow can vary from group to group.
* - In case there are multiple rows to choose from, the row with the smallest number is chosen.
* If there are multiple seats to choose in the same row, the seat with the smallest number is
* chosen.
*
* Implement the BookMyShow class:
* - BookMyShow(int n, int m) Initializes the object with n as number of rows and m as number of
* seats per row.
* - int[] gather(int k, int maxRow) Returns an array of length 2 denoting the row and seat number
* (respectively) of the first seat being allocated to the k members of the group, who must sit
* together. In other words, it returns the smallest possible r and c such that all [c, c + k - 1]
* seats are valid and empty in row r, and r <= maxRow. Returns [] in case it is not possible to
* allocate seats to the group.
* - boolean scatter(int k, int maxRow) Returns true if all k members of the group can be allocated
* seats in rows 0 to maxRow, who may or may not sit together. If the seats can be allocated, it
* allocates k seats to the group with the smallest row numbers, and the smallest possible seat
* numbers in each row. Otherwise, returns false.
*/
/**
* @param {number} n
* @param {number} m
*/
var BookMyShow = function(n, m) {
this.n = n;
this.m = m;
let size = 1;
while (size < n * 2) {
size <<= 1;
}
this.segmentTree = new Array(size).fill(0).map(() => [0, 0]);
this.build(0, 0, n - 1);
};
/**
* @param {number} k
* @param {number} maxRow
* @return {number[]}
*/
BookMyShow.prototype.gather = function(k, maxRow) {
const result = this.queryMax(0, 0, this.n - 1, k, maxRow);
if (result.length) {
this.updateMax(0, 0, this.n - 1, result[0], k);
}
return result;
};
/**
* @param {number} k
* @param {number} maxRow
* @return {boolean}
*/
BookMyShow.prototype.scatter = function(k, maxRow) {
const totalAvailable = this.querySum(0, 0, this.n - 1, maxRow);
const canScatter = totalAvailable >= k;
if (canScatter) {
this.updateSum(0, 0, this.n - 1, k, maxRow);
}
return canScatter;
};
/**
* @param {number} index
* @param {number} left
* @param {number} right
*/
BookMyShow.prototype.build = function(index, left, right) {
if (left === right) {
this.segmentTree[index] = [this.m, this.m];
return;
}
const mid = Math.floor((left + right) / 2);
this.segmentTree[index] = [this.m, (right - left + 1) * this.m];
this.build(2 * index + 1, left, mid);
this.build(2 * index + 2, mid + 1, right);
};
/**
* @param {number} index
* @param {number} left
* @param {number} right
* @param {number} k
* @param {number} maxRow
* @return {number[]}
*/
BookMyShow.prototype.queryMax = function(index, left, right, k, maxRow) {
if (left > maxRow) return [];
if (this.segmentTree[index][0] < k) return [];
if (left === right) {
return [left, this.m - this.segmentTree[index][0]];
}
const mid = Math.floor((left + right) / 2);
const leftResult = this.queryMax(2 * index + 1, left, mid, k, maxRow);
if (leftResult.length) return leftResult;
return this.queryMax(2 * index + 2, mid + 1, right, k, maxRow);
};
/**
* @param {number} index
* @param {number} left
* @param {number} right
* @param {number} row
* @param {number} k
*/
BookMyShow.prototype.updateMax = function(index, left, right, row, k) {
if (left > row || right < row) return;
if (left === right) {
this.segmentTree[index][0] -= k;
this.segmentTree[index][1] -= k;
return;
}
const mid = Math.floor((left + right) / 2);
this.segmentTree[index][1] -= k;
this.updateMax(2 * index + 1, left, mid, row, k);
this.updateMax(2 * index + 2, mid + 1, right, row, k);
this.segmentTree[index][0] = Math.max(
this.segmentTree[2 * index + 1][0],
this.segmentTree[2 * index + 2][0]
);
};
/**
* @param {number} index
* @param {number} left
* @param {number} right
* @param {number} maxRow
* @return {number}
*/
BookMyShow.prototype.querySum = function(index, left, right, maxRow) {
if (left > maxRow) return 0;
if (right <= maxRow) return this.segmentTree[index][1];
const mid = Math.floor((left + right) / 2);
return this.querySum(2 * index + 1, left, mid, maxRow)
+ this.querySum(2 * index + 2, mid + 1, right, maxRow);
};
/**
* @param {number} index
* @param {number} left
* @param {number} right
* @param {number} k
* @param {number} maxRow
*/
BookMyShow.prototype.updateSum = function(index, left, right, k, maxRow) {
if (left > maxRow) return;
if (left === right) {
this.segmentTree[index][0] -= k;
this.segmentTree[index][1] -= k;
return;
}
const mid = Math.floor((left + right) / 2);
this.segmentTree[index][1] -= k;
if (mid + 1 > maxRow || this.segmentTree[2 * index + 1][1] >= k) {
this.updateSum(2 * index + 1, left, mid, k, maxRow);
} else {
const leftSum = this.segmentTree[2 * index + 1][1];
this.updateSum(2 * index + 1, left, mid, leftSum, maxRow);
this.updateSum(2 * index + 2, mid + 1, right, k - leftSum, maxRow);
}
this.segmentTree[index][0] = Math.max(
this.segmentTree[2 * index + 1][0],
this.segmentTree[2 * index + 2][0]
);
};