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:
Example 2:
Code