A cinema has n rows of seats, numbered from 1 to n, and each row has ten seats labeled from 1 to 10. You are given reserved_seats, where reserved_seats[i] = [r, s] means seat s in row r is already reserved. Return the maximum number of four-person groups that can be seated.
A group needs four adjacent seats in one row, and the aisles leave exactly three blocks that qualify: seats 2-5, seats 4-7 (which spans the middle of the row with two people on each side) and seats 6-9.
Function Signature
def max_four_person_groups(n: int, reserved_seats: list[list[int]]) -> int:
Rules
-
A group must take all four seats of one of the blocks 2-5, 4-7 or 6-9 in a single row, and none of those seats may be reserved.
-
Groups cannot share seats, so two groups in the same row must use blocks that do not overlap.
-
Seats 1 and 10 belong to no block.
Constraints
-
1 <= n <= 10^9
-
1 <= len(reserved_seats) <= min(10 * n, 10^4)
-
len(reserved_seats[i]) == 2
-
1 <= reserved_seats[i][0] <= n
-
1 <= reserved_seats[i][1] <= 10
-
All entries of
reserved_seats
are distinct.
Examples
Input: n = 3, reserved_seats = [[1,2],[1,3],[1,8],[2,6],[3,1],[3,10]]
Output: 4
Row 1 has seats 2, 3 and 8 taken, so only the middle block 4-7 is free, and row 2 has seat 6 taken, which leaves block 2-5. Row 3 has only seats 1 and 10 taken, so blocks 2-5 and 6-9 both fit, for a total of 1 + 1 + 2 = 4.
Input: n = 2, reserved_seats = [[2,1],[1,8],[2,6]]
Output: 2
In row 1, seat 8 blocks 6-9, and the free blocks 2-5 and 4-7 overlap, so one group fits. In row 2, seat 6 blocks 4-7 and 6-9 while seat 1 does not matter, so block 2-5 holds one group.
Input: n = 4, reserved_seats = [[1,4],[1,7],[3,1],[3,10],[3,5]]
Output: 5
Seats 4 and 7 touch all three blocks, so row 1 seats nobody, and seat 5 removes blocks 2-5 and 4-7 from row 3 but leaves 6-9. Rows 2 and 4 have no reservations and hold two groups each, so the total is 0 + 2 + 1 + 2 = 5.