Sign in to devexcode.com with google.com

To continue, google.com will share your name, email address, and profile picture with this site. See this site's privacy policy.

2286. Booking Concert Tickets in Groups

Explanation:

To solve this problem, we can maintain a data structure to keep track of the available seats in each row. When a group wants to gather together, we can check row by row for the first available consecutive seats that satisfy the constraints. When a group wants to scatter, we can check row by row for the first available non-consecutive seats that satisfy the constraints.

Algorithm:

  1. Initialize a data structure to track available seats in each row.
  2. For the gather method:
    • Iterate over each row and find the first row with k consecutive available seats within the maxRow limit.
    • If found, mark those seats as occupied and return the row and seat number.
    • If no such seats are available, return an empty array.
  3. For the scatter method:
    • Iterate over each row and find the first row with k available seats within the maxRow limit.
    • If found, mark those seats as occupied and return true.
    • If no such seats are available, return false.

Time Complexity:

  • For the gather method, the time complexity is O(n*m) where n is the number of rows and m is the number of seats per row.
  • For the scatter method, the time complexity is also O(n*m).

Space Complexity:

The space complexity is O(n*m) to store the status of each seat in the concert hall.

:

class BookMyShow {
    int rows, seats;
    boolean[][] hall;
    
    public BookMyShow(int n, int m) {
        rows = n;
        seats = m;
        hall = new boolean[n][m];
    }
    
    public int[] gather(int k, int maxRow) {
        for (int i = 0; i <= maxRow; i++) {
            int count = 0;
            int startSeat = -1;
            for (int j = 0; j < seats; j++) {
                if (!hall[i][j]) {
                    if (startSeat == -1) {
                        startSeat = j;
                    }
                    count++;
                } else {
                    startSeat = -1;
                    count = 0;
                }
                if (count == k) {
                    for (int x = startSeat; x < startSeat + k; x++) {
                        hall[i][x] = true;
                    }
                    return new int[]{i, startSeat};
                }
            }
        }
        return new int[]{};
    }
    
    public boolean scatter(int k, int maxRow) {
        for (int i = 0; i <= maxRow; i++) {
            int count = 0;
            for (int j = 0; j < seats; j++) {
                if (!hall[i][j]) {
                    count++;
                    if (count == k) {
                        for (int x = j - k + 1; x <= j; x++) {
                            hall[i][x] = true;
                        }
                        return true;
                    }
                } else {
                    count = 0;
                }
            }
        }
        return false;
    }
}

Code Editor (Testing phase)

Improve Your Solution

Use the editor below to refine the provided solution. Select a programming language and try the following:

  • Add import statement if required.
  • Optimize the code for better time or space complexity.
  • Add test cases to validate edge cases and common scenarios.
  • Handle error conditions or invalid inputs gracefully.
  • Experiment with alternative approaches to deepen your understanding.

Click "Run Code" to execute your solution and view the output. If errors occur, check the line numbers and debug accordingly. Resize the editor by dragging its bottom edge.