Consecutive Available Seats


  • 0

    Click here to see the full article post


  • 0
    P

    it doesn't work for the situation with seat_id={1,2,3,4,5,6},free={1,1,0,1,1,1},


  • 0

    @ppp123 Could you please explain why so? The code runs properly for the following input.

    {"headers":{"cinema":["seat_id","free"]},"rows":{"cinema":[[1,1],[2,1],[3,0],[4,1],[5,1],[6,1]]}}
    

    And, the output is as below.

    {"headers": ["seat_id"], "values": [[1], [2], [4], [5], [6]]}
    

  • 0
    Y

    @Mr.Bin This might just be an english issue but the question defines consecutive as "more than 2" (i.e >2, not >=2 ) free seats.

    The solution selects 2 or more free seats. How would you adapt the solution to select strictly more than 2 free seats?


  • 0

    @YashArya I'd better to modify the description to make it more clear. Regarding your question, you can check this problem instead https://leetcode.com/problems/consecutive-numbers/#/description, and I think you can work it out by yourself.


  • 0
    I

    this sql is not joining consicutive seats but 1with 3, 2 with 4 and so on.
    this actual logic should be "a.seat_id - b.seat_id=1 or a.seat_id - b.seat_id=0" for previous and currenct seat.
    then union with "a.seat_id - b.seat_id=-1 or a.seat_id - b.seat_id=0" for current and next seat.


  • 0
    I

    I got something is similar here:

    SELECT DISTINCT c1.seat_id AS seat_id
    FROM cinema c1, cinema c2
    WHERE c1.free = 1
    AND c2.free = 1
    AND (c1.seat_id - c2.seat_id = 1
    OR c2.seat_id - c1.seat_id = 1)
    ORDER BY seat_id;


  • 0
    R
    SELECT 
        distinct c1.seat_id
    FROM
        cinema c1, cinema c2
    WHERE 
        c1.seat_id != c2.seat_id
        AND 
        c1.free = 1 
        AND
        c2.free = 1
        AND
         ((c1.seat_id = c2.seat_id - 1) OR (c2.seat_id = c1.seat_id - 1))
    ORDER BY
        c1.seat_id
    

Log in to reply
 

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