LeetCode 1386: Cinema Seat Allocation Solution

Master LeetCode problem 1386 (Cinema Seat Allocation), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1386. Cinema Seat Allocation

Problem Explanation

Explanation:

To solve this problem, we can iterate through each row and check for available groups of 4 seats. We can keep track of the reserved seats in each row and then calculate the maximum number of groups that can be accommodated based on the reserved seats.

  1. Create a map to store the reserved seats by row.
  2. For each row, calculate the available groups of 4 seats by checking the presence of reserved seats in specific combinations.
  3. Add up the maximum number of groups that can be accommodated across all rows.

Time Complexity: O(n) where n is the number of rows Space Complexity: O(n) where n is the number of rows

Solution Code

class Solution {
    public int maxNumberOfFamilies(int n, int[][] reservedSeats) {
        Map<Integer, Set<Integer>> reservedMap = new HashMap<>();
        for (int[] seat : reservedSeats) {
            reservedMap.putIfAbsent(seat[0], new HashSet<>());
            reservedMap.get(seat[0]).add(seat[1]);
        }
        
        int count = 2 * n; // Initialize with 2 rows for each group
        for (int row : reservedMap.keySet()) {
            Set<Integer> reservedSeatsInRow = reservedMap.get(row);
            boolean leftGroup = true, middleGroup = true, rightGroup = true;
            
            if (reservedSeatsInRow.contains(2) || reservedSeatsInRow.contains(3) ||
                reservedSeatsInRow.contains(4) || reservedSeatsInRow.contains(5)) {
                leftGroup = false;
            }
            if (reservedSeatsInRow.contains(6) || reservedSeatsInRow.contains(7) ||
                reservedSeatsInRow.contains(8) || reservedSeatsInRow.contains(9)) {
                rightGroup = false;
            }
            if (reservedSeatsInRow.contains(4) || reservedSeatsInRow.contains(5) ||
                reservedSeatsInRow.contains(6) || reservedSeatsInRow.contains(7)) {
                middleGroup = false;
            }
            
            if (leftGroup && rightGroup) {
                count += 2;
            } else if (leftGroup || middleGroup || rightGroup) {
                count++;
            }
        }
        
        return count;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1386 (Cinema Seat Allocation)?

This page provides optimized solutions for LeetCode problem 1386 (Cinema Seat Allocation) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1386 (Cinema Seat Allocation)?

The time complexity for LeetCode 1386 (Cinema Seat Allocation) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1386 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1386 in Java, C++, or Python.

Back to LeetCode Solutions