KatInterview

Maximum Theater Group Seating Capacity

Math & Bit ManipulationAuto-graded · 20 test cases
MediumAuto-gradedMath & Bit Manipulationbitmaskgreedyoptimization

Last updated

Tests bitmask-based interval availability checking for constrained seat allocation with large input optimization.

A movie theater contains n rows. Each row has exactly 10 seats numbered 1 through 10. Some seats are already reserved, provided as a list of [row, seat] pairs.

You need to determine the maximum number of 4-person groups that can still be seated. Each group must occupy 4 consecutive seats within a single row, and only the following three seat ranges are valid:

  • Seats 2, 3, 4, 5
  • Seats 4, 5, 6, 7
  • Seats 6, 7, 8, 9

Return the maximum total number of groups that can be accommodated across all rows given the current reservations.

Constraints:

  • 1 ≤ n ≤ 10^9
  • 0 ≤ len(reserved_seats) ≤ 10^4
  • Each [row, seat] pair satisfies 1 ≤ row ≤ n and 1 ≤ seat ≤ 10
  • No seat is reserved more than once
  • Reserved seats may appear in any order

Examples

Example 1

Input: max_groups_seated(3, [])

Output: 6

Example 2

Input: max_groups_seated(2, [[1, 2], [1, 3], [1, 8], [2, 6]])

Output: 2

Hints

Hint 1

Notice that seats 1 and 10 cannot be part of any valid 4-seat block. Only seats 2 through 9 matter.

Hint 2

Each row can accommodate at most 2 groups. This happens only when both blocks 2–5 and 6–9 are completely available.

Hint 3

Use a bitmask to track which seats in the critical range (2–9) are occupied in each row, enabling constant-time block availability checks.

Hint 4

Since n can be up to 10^9, you cannot iterate over all rows. Count unreserved rows separately and add their contribution in one step.