Problem Description
Given a cinema with n rows and 10 seats per row, some seats are reserved. A group of four must sit together in one row, and they can only use seats that are adjacent except when the group is split by the aisle—in which case the group occupies 2 seats on each side of the aisle. The available sets of seats for placing a group are: seats 2-5 (left block), seats 6-9 (right block), or seats 4-7 (middle block, which is used when neither left nor right block is available). The task is to determine the maximum number of groups that can be allocated in the cinema.
Key Insights
- Rows with no reserved seats can always seat 2 groups.
- Only rows that have at least one reservation influence the allocation.
- For a row with reserved seats, check three seat blocks:
- Left block (seats 2-5)
- Right block (seats 6-9)
- Middle block (seats 4-7) as a fallback if neither left nor right block is available.
- Use a hash map (or dictionary) to store reserved seats per row, limiting processing only to affected rows.
- The overall answer is the free rows count × 2 plus the count from rows that have reservations.
Space and Time Complexity
Time Complexity: O(m) where m is the number of reserved seats, since we only process rows that have reservations. Space Complexity: O(m) for storing reserved seat information per affected row.
Solution
The solution leverages a hash table to track the reserved seats of each affected row. We first compute the number of rows with reservations and assume that all other rows can fully accommodate 2 groups each. For each reserved row, we check:
- If seats 2, 3, 4, and 5 are free, mark the left block as available.
- If seats 6, 7, 8, and 9 are free, mark the right block as available.
- If neither block is available, then check if the middle block (seats 4-7) is free. Accordingly, we determine the count of groups that can be allocated in that specific row. The final answer is the sum of groups from the reserved rows and the groups from completely free rows.