Codementor Events

~600ms JavaScript BookMyShow

Published May 11, 2023

Intuition

This problem seems to be related to managing the seating arrangement in a cinema hall or similar establishment using the BookMyShow class. The overall aim is to efficiently and fairly allocate seats to the customers according to their requests (number of seats and the maximum row they are willing to be seated in) while also ensuring maximum utilization of the hall.

Approach

The BookMyShow class uses two main methods: gather and scatter.

gather(k, maxRow) method tries to allocate k seats in a row that's up to maxRow. The row should have at least k empty seats. The method iterates from the first row that has enough empty seats to maxRow, and once it finds a suitable row, it allocates the seats and updates the seating structure.

scatter(k, maxRow) method tries to allocate k seats in any possible way in rows up to maxRow. The method iterates from the first row with empty seats to maxRow, and fills as many seats as possible in each row until all k seats are allocated.

The class uses several helper structures:

first_row: the first row that has at least one empty seat.
first_row_table: a table that keeps track of the first row that has enough empty seats for k people.
first_seats: an array that keeps track of the first empty seat in each row.
unable_value_set: a set that stores pairs of k and maxRow for which we couldn't allocate the seats in the scatter method.

Complexity

Time complexity for both the gather and scatter method, has a time complexity of O(n). Where n is the number of rows. In the worst-case scenario, it needs to iterate through all the rows to find a suitable one.

The space complexity is the same. Where n is the number of rows. This is because we store the state of each row in first_seats array, and in the worst case, we store each row in the first_row_table and unable_value_set.

Code

class BookMyShow {
    constructor(n, m) {
        this.N = n;
        this.M = m;
        this.first_row = 0;
        this.first_row_table = {};
        this.first_seats = new Array(n).fill(0);
        this.unable_value_set = new Set();
    }

    gather(k, maxRow) {
        if (k > this.M) {
            return [];
        }
        if (this.first_row_table[k] > maxRow) {
            return [];
        }
        for (let row = Math.max(this.first_row, this.first_row_table[k] || 0); row < Math.min(this.N, maxRow + 1); row++) {
            if (this.M - this.first_seats[row] >= k) {
                let result = [row, this.first_seats[row]];
                this.first_seats[row] += k;
                while (this.first_row < this.N && this.first_seats[this.first_row] === this.M) {
                    this.first_row++;
                }
                this.first_row_table[k] = Math.max(this.first_row, row);
                row = this.first_row_table[k];
                while (row < this.N && this.M - this.first_seats[row] < k) {
                    row++;
                }
                this.first_row_table[k] = row;                    
                return result;
            }
        }
        return [];                
    }

    scatter(k, maxRow) {
        if (this.unable_value_set.has(`${k}_${maxRow}`)) {
            return false;
        }
        
        let k1 = k;
        for (let row = this.first_row; row < Math.min(this.N, maxRow + 1); row++) {
            if (this.M - this.first_seats[row] > k) {
                this.first_seats[row] += k;
                this.first_row = row;                
                return true;
            } else if (this.M - this.first_seats[row] === k) {
                this.first_row = row + 1;
                return true;
            } else {
                k -= this.M - this.first_seats[row];
            }
        }
        
        this.unable_value_set.add(`${k1}_${maxRow}`);
        return false;
    }
}

// Usage:
// let obj = new BookMyShow(n, m);
// let param_1 = obj.gather(k,maxRow);
// let param_2 = obj.scatter(k,maxRow);

Discover and read more posts from Brett "Hans" McMurdy
get started
post commentsBe the first to share your opinion
Show more replies