LeetCode 3140: Consecutive Available Seats II Solution

Master LeetCode problem 3140 (Consecutive Available Seats II), 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.

3140. Consecutive Available Seats II

Medium

Problem Explanation

Explanation:

To solve this problem, we can iterate through the input list of seats and check if consecutive seats are available for a group of n seats. We can use a hashmap to store the available seats for each row and then iterate through the rows to find consecutive seats for each group. We need to consider the edge case where there can be more than n consecutive seats in a row.

Here are the steps:

  1. Create a hashmap to store the available seats for each row.
  2. Iterate through the input list of seats and store the available seats in the hashmap.
  3. For each row, iterate through the seats to find consecutive seats for a group of n seats.
  4. Check if the consecutive seats are available and count the number of groups found.

Time complexity: O(m * n) where m is the number of rows and n is the number of seats in each row. Space complexity: O(m * n) for the hashmap.

:

Solution Code

class Solution {
    public int maxNumberOfFamilies(int n, int[][] reservedSeats) {
        Map<Integer, BitSet> map = new HashMap<>();
        
        for (int[] seat : reservedSeats) {
            int row = seat[0];
            int col = seat[1];
            map.computeIfAbsent(row, k -> new BitSet(10)).set(col);
        }
        
        int count = 2 * (n - map.size());
        
        for (Map.Entry<Integer, BitSet> entry : map.entrySet()) {
            BitSet reserved = entry.getValue();
            boolean leftAvailable = true;
            boolean middleAvailable = true;
            boolean rightAvailable = true;
            
            for (int i = 2; i <= 5; i++) {
                if (reserved.get(i)) {
                    leftAvailable = false;
                    break;
                }
            }
            
            for (int i = 4; i <= 7; i++) {
                if (reserved.get(i)) {
                    middleAvailable = false;
                    break;
                }
            }
            
            for (int i = 6; i <= 9; i++) {
                if (reserved.get(i)) {
                    rightAvailable = false;
                    break;
                }
            }
            
            if ((leftAvailable && rightAvailable) || (leftAvailable && middleAvailable) || (middleAvailable && rightAvailable)) {
                count += 2;
            } else if (leftAvailable || middleAvailable || rightAvailable) {
                count++;
            }
        }
        
        return count;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3140 (Consecutive Available Seats II)?

This page provides optimized solutions for LeetCode problem 3140 (Consecutive Available Seats II) 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 3140 (Consecutive Available Seats II)?

The time complexity for LeetCode 3140 (Consecutive Available Seats II) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3140 on DevExCode?

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

Back to LeetCode Solutions