A cinema has n
rows of seats, numbered from 1 to n
and there are ten seats in each row, labelled from 1 to 10 as shown in the figure above.
Given the array reservedSeats
containing the numbers of seats already reserved, for example, reservedSeats[i] = [3,8]
means the seat located in row 3 and labelled with 8 is already reserved.
Return the maximum number of four-person groups you can assign on the cinema seats. A four-person group occupies four adjacent seats in one single row. Seats across an aisle (such as [3,3] and [3,4]) are not considered to be adjacent, but there is an exceptional case on which an aisle split a four-person group, in that case, the aisle split a four-person group in the middle, which means to have two people on each side.
Example 1:
Input: n = 3, reservedSeats = [[1,2],[1,3],[1,8],[2,6],[3,1],[3,10]] Output: 4 Explanation: The figure above shows the optimal allocation for four groups, where seats mark with blue are already reserved and contiguous seats mark with orange are for one group.
Example 2:
Input: n = 2, reservedSeats = [[2,1],[1,8],[2,6]] Output: 2
Example 3:
Input: n = 4, reservedSeats = [[4,3],[1,4],[4,6],[1,7]] Output: 4
Constraints:
1 <= n <= 10^9
1 <= reservedSeats.length <= min(10*n, 10^4)
reservedSeats[i].length == 2
1 <= reservedSeats[i][0] <= n
1 <= reservedSeats[i][1] <= 10
reservedSeats[i]
are distinct.This problem involves efficiently determining the maximum number of four-person groups that can be seated in a cinema, given a list of already reserved seats. The solution leverages bit manipulation and hash tables for optimal performance.
Approach:
Data Representation: Each row's seating arrangement is represented using a single integer (or a bitmask). Each bit in the integer corresponds to a seat: 0 for unoccupied, 1 for occupied. The least significant bit represents seat 10, and the most significant bit represents seat 1. This compact representation allows for efficient checking of seat availability.
Hash Table (Dictionary): A hash table (dictionary in Python) is used to store the seating arrangement for each row. The row number is the key, and the bitmask representing the occupied seats is the value. This provides quick access to the state of a particular row.
Counting Available Groups: The algorithm iterates through the rows. For each row:
Unreserved Rows: If a row is not in the hash table (meaning no seats are reserved), it can accommodate two four-person groups.
Reserved Rows: If a row is in the hash table, the algorithm checks for available four-seat blocks using bitwise AND operations against predefined bitmasks representing the possible four-seat group arrangements:
0b0111100000
: Seats 2-50b0000011110
: Seats 6-90b0001111000
: Seats 4-7 (to handle the aisle split)Bitwise AND: The bitwise AND operation efficiently determines if a four-seat block is free. If it is, the count of available groups is incremented.
Final Count: The algorithm sums up the available groups from unreserved and reserved rows to obtain the maximum number of four-person groups.
Time Complexity Analysis:
reservedSeats
array once to populate the hash table, which takes O(m) time, where m is the number of reserved seats.Space Complexity Analysis:
m
entries (one for each occupied row).Code Examples (with explanations):
The code examples in Python, Java, C++, Go, and TypeScript provided earlier in the response all implement this approach. Let's focus on the Python code for a detailed explanation:
from collections import defaultdict
class Solution:
def maxNumberOfFamilies(self, n: int, reservedSeats: List[List[int]]) -> int:
d = defaultdict(int) # Hash table (defaultdict for easier handling of missing keys)
for i, j in reservedSeats:
d[i] |= 1 << (10 - j) # Set the bit corresponding to the reserved seat
masks = (0b0111100000, 0b0000011110, 0b0001111000) # Bitmasks for 4-seat groups
ans = (n - len(d)) * 2 # Account for unreserved rows (2 groups per row)
for x in d.values(): # Iterate through occupied rows
for mask in masks: # Check each possible 4-seat arrangement
if (x & mask) == 0: # Check if the group is free (bitwise AND == 0)
ans += 1 # Increment the count
break # Only count once per row if a group is found
return ans
The other languages' implementations follow the same logic, using their respective data structures and bit manipulation operators. The key is the efficient use of bit manipulation to quickly check for seat availability within rows.