Loading...
Loading...
A small cinema sells one row of n seats. People book single seats, cancel them, or book a group that must sit together; a group gets the lowest block of free seats next to each other.
Write a class ScreenRow with a private boolean[] booked and a private helper isSeat(int s). Give it book, cancel, bookGroup, getFreeCount() and getSeatMap(), each returning its message or value. Callers must never get the array itself.
book s, cancel s, group g or map.book: BOOKED s, ALREADY BOOKED s or NO SEAT s. cancel: CANCELLED s, NOT BOOKED s or NO SEAT s. group: BOOKED a-b (BOOKED a for a group of 1), NO g SEATS TOGETHER or INVALID GROUP g. map: one character per seat (X booked, . free), then FREE f.Example
Input
6 5 book 3 group 3 group 2 cancel 3 map
Output
BOOKED 3 BOOKED 4-6 BOOKED 1-2 CANCELLED 3 XX.XXX FREE 1
Seat 3 splits the row, so the 3-seat group gets 4-6 and the pair gets 1-2.
Try the problem first. Open one hint at a time.
Walk the row counting free seats in a row; reset the count at a booked seat.
When the count reaches g, the block ends at the current index and starts g - 1 places earlier.
getSeatMap() builds a String, so the caller sees the seats but cannot change them.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor