Range Queries Segment tree
612. Booking Concert Tickets in Groups
A concert hall has n rows of seats, numbered from 0 at the stage to n − 1 at the back. Every row has m
seats, numbered from 0 to m − 1. Groups of people arrive one after another and ask for seats. Write a class
BookMyShow with a constructor and two methods:
BookMyShow(n, m)creates the hall with every seat free.gather(k, maxRow)seats a group ofkpeople who want to sit next to each other in one row, in rowmaxRowor closer to the stage. It finds the smallest rowr ≤ maxRowthat haskfree seats side by side,c,c + 1, …,c + k − 1, and in that row the smallest suchc. It books those seats and returns[r, c]. If no row up tomaxRowhas room, it books nothing and returns[].scatter(k, maxRow)seats a group ofkpeople who may sit apart, anywhere in rows 0 tomaxRow. If those rows have at leastkfree seats in total, it bookskof them and returnstrue. It takes the seats in the smallest rows first, and in each row the smallest seat numbers first. Otherwise it books nothing and returnsfalse.
Your class is tested by replaying a list of calls on a new BookMyShow(n, m). ops[i] names the i-th call,
"gather" or "scatter", and that call gets k = ks[i] and maxRow = maxRows[i]. The answer lists what
every call returns, in order, so it has one entry per call.
In C# and Go the methods are Gather and Scatter. Go creates the hall with Constructor(n, m), and Rust
with BookMyShow::new(n, m). The names in ops are the same in every language.
Example 1
Input: n = 3
m = 5
ops = ["gather",
"gather",
"scatter",
"gather",
"gather",
"scatter"]
ks = [3, 3, 4, 1, 5, 1]
maxRows = [1, 1, 1, 1, 2, 2]
[[0,0],[1,0],true,[],[2,0],false]Explanation: The first group takes seats 0 to 2 of row 0. Row 0 has only 2 seats left, so the second group goes to
row 1. The scatter takes seats 3 and 4 of row 0 and seats 3 and 4 of row 1, which fills both rows. The
next gather cannot use row 2, which is past maxRow. The group of 5 then fills row 2, and the hall is
full.
Example 2
Input: n = 2
m = 4
ops = ["scatter",
"scatter",
"gather",
"gather"]
ks = [6, 6, 2, 1]
maxRows = [0, 1, 1, 1]
[false,true,[1,2],[]]Explanation: The first scatter may only use row 0, which has 4 seats, so it books nothing. The second may use both rows: it takes all of row 0 and seats 0 and 1 of row 1. The group of 2 then gets seats 2 and 3 of row 1.
Example 3
Input: n = 3
m = 1000000000
ops = ["gather",
"gather",
"gather",
"scatter",
"gather"]
ks = [1000000000, 1, 1, 1000000000, 1000000000]
maxRows = [0, 0, 2, 2, 2]
[[0,0],[],[1,0],true,[]]Explanation: The first group fills row 0, so a gather limited to row 0 fails. The scatter takes the 999999999 free seats of row 1 and seat 0 of row 2. No row then has 109 free seats.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ n ≤ 1051 ≤ m ≤ 1091 ≤ ops.length ≤ 105- Each
ops[i]is"gather"or"scatter". ks.lengthandmaxRows.lengthequalops.length.1 ≤ ks[i] ≤ 1090 ≤ maxRows[i] ≤ n − 1
Clarifying questions
What you would ask an interviewer before writing any code.