LeetCode--603. 连续空余座位

    科技2024-08-08  66

    解题思路

    表自连接,两表座位号的绝对值等于1说明俩坐连续,再控制空余座位即可

    select distinct c1.seat_id from cinema c1, cinema c2 where abs(c1.seat_id - c2.seat_id) = 1 and c1.free = 1 and c2.free = 1 order by c1.seat_id

    变量做法

    select t1.seat_id from ( select t1.seat_id, if(t1.free = 0, @cnt1 := 0, @cnt1 := @cnt1 + 1) as cnt from cinema t1, (select @cnt1 := 0 from dual) t2 ) t1 where t1.cnt > 1 union select t2.seat_id from ( select t1.seat_id, if(t1.free = 0, @cnt := 0, @cnt := @cnt + 1) as cnt from cinema t1, (select @cnt := 0 from dual) t2 ) t1 inner join cinema t2 on t1.seat_id = t2.seat_id + 1 where t1.cnt > 1 and t2.free = 1 order by 1
    Processed: 0.009, SQL: 8