<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實(shí)戰(zhàn)603:連續(xù)空余座位

          共 1038字,需瀏覽 3分鐘

           ·

          2022-05-10 23:03

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識(shí)和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個(gè)公眾號(hào)后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做?連續(xù)空余座位,我們先來看題面:
          https://leetcode.cn/problems/consecutive-available-seats/

          解題

          https://blog.csdn.net/qq_25886325/article/details/118635352


          1、join

          思路:只有一張表,需要通過seat_id自己連自己,條件是abs(c1.seat_id - c2.seat_id) = 1。
          代碼:

          select

          ??distinct?c1.seat_id

          from?cinema c1 join?cinema c2

          on?abs(c1.seat_id - c2.seat_id) = 1?and?c1.free = 1?and?c2.free = 1

          order?by?c1.seat_id;

          2、row_number() over()

          思路:
          S1:使用窗口函數(shù) row_number 對(duì) cinema 進(jìn)行排序,seat_id - row_number() over() as k ,如果座位連續(xù),這組 k 值應(yīng)該是相等的。
          S2:將第一步和第二步的結(jié)果作為臨時(shí)表 temp,將臨時(shí)表 temp 按照 k 進(jìn)行分組查詢,并且篩選出大于 2 的 k 值
          S3:從臨時(shí)表 temp 中查詢出 seat_id ,篩選出 k 值在第四步中的值

          代碼:

          with temp as (

          ??select

          ????seat_id,

          ????seat_id - row_number() over() as?k

          ??from?cinema where?free = 1

          )

          select?seat_id from?temp where?k in?(

          ??select?k from?temp group?by?k having?count(*) >= 2

          );


          原文鏈接:https://blog.csdn.net/HeavenDan/article/details/123448972
          上期推文:
          LeetCode1-600題匯總,希望對(duì)你有點(diǎn)幫助!
          LeetCode刷題實(shí)戰(zhàn)601:體育館的人流量
          LeetCode刷題實(shí)戰(zhàn)602:好友申請(qǐng) II :誰有最多的好友

          瀏覽 18
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  欧美福利在线观看 | 久操免费在线 | 人人爱人人操黄色大片 | 破坏版无码AV在线播放 | 日韩不卡一二三 |