oneby7

Range Queries Segment tree

612. Booking Concert Tickets in Groups

Hard

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 of k people who want to sit next to each other in one row, in row maxRow or closer to the stage. It finds the smallest row r ≤ maxRow that has k free seats side by side, c, c + 1, …, c + k − 1, and in that row the smallest such c. It books those seats and returns [r, c]. If no row up to maxRow has room, it books nothing and returns [].
  • scatter(k, maxRow) seats a group of k people who may sit apart, anywhere in rows 0 to maxRow. If those rows have at least k free seats in total, it books k of them and returns true. It takes the seats in the smallest rows first, and in each row the smallest seat numbers first. Otherwise it books nothing and returns false.

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]

Output: [[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]

Output: [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]

Output: [[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.

n = 2 m = 6 ops = ["gather", "gather", "gather"] ks = [4, 3, 2] maxRows = [1, 1, 1]

Constraints

  • 1 ≤ n ≤ 105
  • 1 ≤ m ≤ 109
  • 1 ≤ ops.length ≤ 105
  • Each ops[i] is "gather" or "scatter".
  • ks.length and maxRows.length equal ops.length.
  • 1 ≤ ks[i] ≤ 109
  • 0 ≤ maxRows[i] ≤ n − 1

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.