#2286

Booking Concert Tickets in Groups

grandmaster · 2220 · lc hard +32 · 19.3% accepted · 351 likes · top 1%

Description

A concert venue has n rows (numbered 0 to n - 1), each with m seats (numbered 0 to m - 1). Design a ticket booking system supporting two operations:

- Seat k people consecutively in a single row.

- Seat k people anywhere (spread across rows if needed).

Constraints:

- Bookings are only allowed in rows numbered at most maxRow.

- Among valid options, the lowest-numbered row is preferred; within a row, the lowest seat number is preferred.

Implement the BookMyShow class:

- BookMyShow(int n, int m) Initializes with n rows of m seats each.

- int[] gather(int k, int maxRow) Returns [row, firstSeat] for k consecutive empty seats in one row, or [] if impossible.

- boolean scatter(int k, int maxRow) Seats k people greedily in the earliest available seats across eligible rows and returns true. Returns false if not enough seats exist.

Example 1:

Input
["BookMyShow", "gather", "gather", "scatter", "scatter"]
[[2, 5], [4, 0], [2, 0], [5, 1], [5, 1]]
Output
[null, [0, 0], [], true, false]

Example 2:

Explanation
BookMyShow bms = new BookMyShow(2, 5); // There are 2 rows with 5 seats each
bms.gather(4, 0); // return [0, 0]
// The group books seats [0, 3] of row 0.
bms.gather(2, 0); // return []
// There is only 1 seat left in row 0,
// so it is not possible to book 2 consecutive seats.
bms.scatter(5, 1); // return True
// The group books seat 4 of row 0 and seats [0, 3] of row 1.
bms.scatter(5, 1); // return False
// There is only one seat left in the hall.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16