Similar Problems

Similar Problems not available

Cinema Seat Allocation - Leetcode Solution

Companies:

LeetCode:  Cinema Seat Allocation Leetcode Solution

Difficulty: Medium

Topics: greedy hash-table bit-manipulation array  

The Cinema Seat Allocation problem on LeetCode involves allocating seats to customers in a cinema hall. The goal is to maximize the number of satisfied customers by assigning them seats according to their preferences.

Let's take a look at the problem statement:

There are n rows of seats in a cinema hall and each row has ten seats labeled from 1 to 10. Given a list of reserved seats, your task is to allocate seats for as many customers as possible, while avoiding any conflicts between them.

Each customer has a row preference (1 to n) and a list of seat preferences (1 to 10). If a customer's preferred seat is already reserved by someone else, they will not be satisfied with any other seat. If a customer is not able to get their preferred seat, they will be satisfied with any other seat in the same row.

Write a function to find the maximum number of satisfied customers that can be accommodated in the cinema hall.

Let's break down this problem step by step:

  1. Input: The input to the function will be a list of tuples that represent the reserved seats. The tuples will contain the row number and the seat number that has been reserved.

  2. Output: The function should return an integer that represents the maximum number of satisfied customers that can be accommodated in the cinema hall.

  3. Approach: The first step is to create a dictionary that stores the reserved seats as key-value pairs. The keys of the dictionary will be the row numbers and the values will be lists of seat numbers that have been reserved in that row.

Next, we will loop through each customer and check if their preferred seat is available. If it is, we will mark the seat as reserved and move on to the next customer. If it is not, we will check if any other seat in the same row is available. If it is, we will allocate that seat to the customer and mark it as reserved. If no seat is available in the same row, we will move on to the next customer.

We will repeat this process for all customers and keep a count of the satisfied customers.

Let's take a look at the code:

def maxSatisfied(customers: List[List[int]], reserved_seats: List[Tuple[int, int]]) -> int:
    reserved_dict = {}
    for seat in reserved_seats:
        if seat[0] not in reserved_dict:
            reserved_dict[seat[0]] = []
        reserved_dict[seat[0]].append(seat[1])
    satisfied_count = 0
    for customer in customers:
        row, seat = customer[0], customer[1]
        if seat in reserved_dict.get(row, []):
            satisfied_count += 1
        elif (seat-1) not in reserved_dict.get(row, []) and (seat-2) not in reserved_dict.get(row, []) and (seat+1) not in reserved_dict.get(row, []) and (seat+2) not in reserved_dict.get(row, []):
            reserved_dict[row].extend([seat-1, seat, seat+1])
            satisfied_count += 1
    return satisfied_count + sum([len(seats) for seats in reserved_dict.values()])

In this code, we first create a dictionary reserved_dict that stores the reserved seats. Next, we initialize a counter satisfied_count to keep track of the satisfied customers.

We then loop through each customer and check if their preferred seat is available. If it is, we increment satisfied_count. If not, we check if any other seat in the same row is available. If it is, we allocate that seat to the customer and mark it as reserved. If no seat is available in the same row, we move on to the next customer.

At the end of the loop, we return the sum of satisfied_count and the total number of reserved seats that were not used.

This solution has a time complexity of O(n^2), where n is the number of customers. The space complexity is O(n) for the reserved_dict dictionary.

Cinema Seat Allocation Solution Code

1