Simple answer using join

  • 1

    Join two tables of left and right seats and check for the conditions where middle and left or right are empty.

    SELECT middle_seat.seat_id
    FROM cinema middle_seat
    LEFT JOIN cinema left_seat
        ON middle_seat.seat_id - left_seat.seat_id = 1
    LEFT JOIN cinema right_seat
        ON right_seat.seat_id - middle_seat.seat_id = 1
    WHERE = 1 AND ( = 1 OR = 1)
    ORDER BY middle_seat.seat_id

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.