Booking Concert Tickets in Groups - Problem
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
kspectators can sit together in a row. - If every member of a group of
kspectators 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.maxRowcan 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 withnas number of rows andmas 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 thekmembers of the group, who must sit together. In other words, it returns the smallest possiblerandcsuch that all[c, c + k - 1]seats are valid and empty in rowr, andr <= maxRow. Returns[]in case it is not possible to allocate seats to the group.boolean scatter(int k, int maxRow)Returnstrueif allkmembers of the group can be allocated seats in rows0tomaxRow, who may or may not sit together. If the seats can be allocated, it allocateskseats to the group with the smallest row numbers, and the smallest possible seat numbers in each row. Otherwise, returnsfalse.
Input & Output
Example 1 — Basic Operations
$
Input:
operations = [["BookMyShow",2,5],["gather",4,0],["gather",2,0],["scatter",5,1],["scatter",5,1]]
›
Output:
[null,[0,0],[0,4],true,false]
💡 Note:
Initialize 2×5 hall. First gather(4,0) allocates seats 0-3 in row 0. Second gather(2,0) fails (row 0 full). Scatter(5,1) uses remaining seat in row 0 plus 4 in row 1. Second scatter(5,1) fails (no seats left).
Example 2 — Multiple Rows
$
Input:
operations = [["BookMyShow",3,3],["gather",3,1],["scatter",2,2]]
›
Output:
[null,[0,0],true]
💡 Note:
Initialize 3×3 hall. Gather(3,1) finds 3 consecutive seats in row 0. Scatter(2,2) places 2 seats in row 1.
Example 3 — Edge Case
$
Input:
operations = [["BookMyShow",1,1],["gather",1,0],["gather",1,0]]
›
Output:
[null,[0,0],[]]
💡 Note:
Single seat hall. First gather succeeds, second fails as hall is full.
Constraints
- 1 ≤ n ≤ 5 × 104
- 1 ≤ m ≤ 109
- 1 ≤ k ≤ min(maxRow + 1, n) × m
- 0 ≤ maxRow ≤ n - 1
- At most 5 × 104 calls to gather and scatter
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code