<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Kaminyou on Medium]]></title>
        <description><![CDATA[Stories by Kaminyou on Medium]]></description>
        <link>https://medium.com/@ikaminyou?source=rss-2a1ce3813f59------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*j5zbMARTCLsEm7KVFiwCtg.jpeg</url>
            <title>Stories by Kaminyou on Medium</title>
            <link>https://medium.com/@ikaminyou?source=rss-2a1ce3813f59------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 12:18:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ikaminyou/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[【GRE高分獎學金心得】台大資工所上班族 一戰323 工作下班後準備 感謝Mason GRE]]></title>
            <link>https://ikaminyou.medium.com/gre%E9%AB%98%E5%88%86%E7%8D%8E%E5%AD%B8%E9%87%91%E5%BF%83%E5%BE%97-%E5%8F%B0%E5%A4%A7%E8%B3%87%E5%B7%A5%E6%89%80%E4%B8%8A%E7%8F%AD%E6%97%8F-%E4%B8%80%E6%88%B0323-%E5%B7%A5%E4%BD%9C%E4%B8%8B%E7%8F%AD%E5%BE%8C%E6%BA%96%E5%82%99-%E6%84%9F%E8%AC%9Dmason-gre-23f6f7fdb2c1?source=rss-2a1ce3813f59------2</link>
            <guid isPermaLink="false">https://medium.com/p/23f6f7fdb2c1</guid>
            <dc:creator><![CDATA[Kaminyou]]></dc:creator>
            <pubDate>Mon, 10 Apr 2023 15:12:58 GMT</pubDate>
            <atom:updated>2023-04-10T15:12:58.869Z</atom:updated>
            <content:encoded><![CDATA[<p>【考試成績】 GRE一戰323 V155 Q168 AW3.5</p><p>【身份背景】 科技公司Data scientist、台大資工所、成大藥學系</p><p>【英文能力】 學測15</p><p>【準備時間】 2022.12–2023.01 (Mason GRE 9月 平日班)</p><p>.</p><p>【為什麼想要考GRE？為什麼想出國？】</p><p>考量到台灣的產業仍偏向硬體且整個國家計算資源不足，若要往軟體發展或開發各種機器學習背後的演算法，勢必得去美國才有機會。另外也是體會到若只有碩士學歷，在未來職涯升遷上恐會遇到不少障礙，加上自己對computer vision研究尚有熱誠，想趁還能夠熬夜趕paper的年紀至美國把PhD學位拿到手。而考GRE是想說先準備起來放著，申請留學時可作為必要或加分項目，同時也提升英文閱讀能力。</p><p>.</p><p>【念GRE的額外收穫 】</p><p>自己在撰寫paper時用字遣詞有更多選擇了，且也能使用的更精準，寫出更學術性的語句避免被reviewer直接因為寫作狀況不佳而reject。</p><p>.</p><p>【為什麼選擇 Mason GRE？】</p><p>目前所認識的所有學長皆都選擇Mason GRE，且另外因為是在職中準備，上課時間必須是晚上，在確認一切時間都能配合現有生活狀態後，便直接選擇之。</p><p>.</p><p>【各科準備心得】</p><p>【字彙填空】</p><p>六堂單字課會教不少神奇的記法或利用影片幫助記那些難以背起來的字，自己的體驗是上課只要有特別強調過的字，之後就再也不會忘記了，因此在後期準備時，只要特別去記其他額外的字即可。另一方面，上課提供的講義已經足以應付考試，也不需要額外再買教材。</p><p>.</p><p>【邏輯閱讀</p><p>上課時反覆提及閱讀時要事先想好各種題目上的邏輯問題，這個方法在事後練習時確實有不少幫助。此外課程中也會把各種題型都帶過一次，因此能快速理解究竟會考什麼，提前擬定應對方法。</p><p>.</p><p>【寫作(AW) 】</p><p>雖然GRE的寫作需準備的內容相較起來很少，但課程正好也是如此的設計，讓我得以在很短的時間內把這個考試項目搞定，而且也不需要背模板，只要照著課堂教的邏輯撰文便能拿到基本成績了。</p><p>.</p><p>【數學】</p><p>基本上，數學這個考項對一般台灣學生不構成巨大威脅，課堂上主要就是提醒一些容易忘記的觀念，以及讓我們熟悉考題中數學專有名詞，只要回去後把提供的題本中的題目練習完後，相信考試時不會有太大問題。</p><p>.</p><p>【考試時程安排】</p><p>當時9月上完課後，10月底剛好得去以色列參加ECCV兩週，故把考試準備時程挪到11月開始，考試排在2月上旬，但當時又因為較為忙碌，故整體準備時程是12月至1月，共兩個月(不過1月中旬又去日本一趟，可能準備的時間加總起來約50天)。第一個月是把全部課堂講義寫完並把1000單背完，第二個月則是開始加上時間限制來刷題以及把2000單背完，另外作文的練習不管是issue或argument，我在最後兩週每兩天寫一篇故寫了七篇，也把所有題目都看過，把比較難發揮的題目提早想了一遍。整個過程亦搭配quizlet以及hackMD來協助我把不熟的字彙記起來。</p><p>.</p><p>【應考時臨場狀況】</p><p>基本上我的體感是正式考試的難度比練習時高了一些，像是字彙填空就有兩三題的選項連看都沒看過，而數學也比練習時難了一點，雖然基本上是寫得出來，但就沒辦法像練習時在20分鐘內就全部完成了。另外是當時考試人數挺多，大家寫的時間也不一樣，可能中途有人會走來走去，自己忘了帶耳塞去有稍微影響到讀題速度。最後考出來的分數跟報名考試提供的2份模考題的分數差不多，故模考題的分數應該可以作為大家擬定準備策略的基準。</p><p>.</p><p>【總結以及感謝語】</p><p>謝謝Mason GRE讓我花短短幾個月把這個麻煩的考試搞定，而能有更多時間去處理其他申請上得準備的資料</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=23f6f7fdb2c1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[VSCode Remote SSH整合ChatGPT extension (GPT4 + Google auth)教學 (MacOS)]]></title>
            <link>https://ikaminyou.medium.com/vscode-remote-ssh%E6%95%B4%E5%90%88chatgpt-extension-gpt4-google-auth-%E6%95%99%E5%AD%B8-macos-7dab1d04e812?source=rss-2a1ce3813f59------2</link>
            <guid isPermaLink="false">https://medium.com/p/7dab1d04e812</guid>
            <category><![CDATA[chatgpt]]></category>
            <category><![CDATA[remote-ssh]]></category>
            <category><![CDATA[macos]]></category>
            <category><![CDATA[gpt-4]]></category>
            <category><![CDATA[vscode]]></category>
            <dc:creator><![CDATA[Kaminyou]]></dc:creator>
            <pubDate>Sat, 18 Mar 2023 05:50:01 GMT</pubDate>
            <atom:updated>2023-03-18T05:50:01.431Z</atom:updated>
            <content:encoded><![CDATA[<p>在官方現有的solution中，尚未對remote SSH並且需要使用browser login的方案有詳細的教學(因目前token方案並無法提供GPT 4 model的選項，以及大部分的人可能都不是購買token方案的)，故這份教學為整合了GitHub上其他開發者提供的解法，提供一個可行的方案。</p><p><strong>Prerequisite</strong></p><ol><li>ChatGPT Plus subscribers (使用Google帳戶)</li><li>VSCode + Remote SSH extension</li><li>有Linux server，並且在SSH config已經設定完畢</li><li>Local為MacOS</li></ol><h3>步驟</h3><p><strong>STEP 0: 請先使用VSCode Remote SSH連上遠端Linux server</strong></p><p><strong>STEP 1: 搜尋並安裝VSCode ChatGPT extension</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3EwOlDMZZwY-5SObbwsiXQ.png" /></figure><p>安裝後左邊應該會出現ChatGPT的圖示，點進去應可見</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*21ui7uu28TcE7gOzeQZVbg.png" /></figure><p><strong>STEP 2: 更改setting</strong></p><p>由此處可以點開設定</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oTwRSRnWacp5w5FNuMP8mQ.png" /></figure><p>更改前面這兩項（User browser integration + Google Auth.）</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8fDDLWTS2agz9b2NN_tdMg.png" /></figure><p>往下找，選擇model (Browser autologin中的任何項目，依據需求選擇即可)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RATtrhhVFI4-fvGc3zX_qA.png" /></figure><p><strong>STEP 3: 在Linux server執行以下command</strong></p><pre>$ sudo apt update &amp;&amp; sudo apt install wget xauth xorg<br>$ wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb<br>$ sudo apt install ./google-chrome-stable_current_amd64.deb</pre><p><strong>STEP 4: 在Linux server更改/etc/ssh/sshd_config</strong></p><pre>$ vim /etc/ssh/sshd_config<br># edit or add<br>X11Forwarding yes<br>X11DisplayOffset 10<br>X11UseLocalhost yes</pre><p><strong>STEP 5: 於Local MacOS，確認SSH_AUTH_SOCK已經設定了</strong></p><pre>$ echo $SSH_AUTH_SOCK<br># should show /private/tmp/com.apple.launchd.xxxx/Listeners</pre><p><strong>STEP 6: 於Local MacOS，安裝XQuartz (</strong><a href="https://www.xquartz.org/"><strong>https://www.xquartz.org/</strong></a><strong>)，安裝好後重開VSCode</strong></p><p><strong>STEP 7: 於Local MacOS，更改ssh config，在最前面加上</strong></p><pre>Host *<br>     ForwardAgent yes<br>     ForwardX11 yes</pre><p><strong>STEP 8: SSH remote server，在remote執行</strong></p><pre>$ xeyes</pre><p>若有成功在local跳出眼睛的視窗，那代表全部設定都成功了</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/420/1*Trfj40PZnN3yqxEsXK-LIw.png" /></figure><p><strong>STEP 9: Log in</strong></p><p>接著就能回到remote server中點下Log in</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h-LOy1yWf1eOjy8JLbjpUQ.png" /></figure><p>此時local端就會跳出登入畫面，登入後就能使用了</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6xAxC6SsDnZbuChrESIXyA.png" /></figure><p><strong>STEP 10: 開始使用</strong></p><p>可以右鍵選擇使用ChatGPT優化code了</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RsqYfEfqcqq-la473RBQqA.png" /></figure><p>點下去後左邊的畫面如下</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vxjr_fK7Vc3RAsb5gbUR-w.png" /></figure><h3><strong>參考資料</strong></h3><ul><li><a href="https://github.com/gencay/vscode-chatgpt/issues/39#issuecomment-1370272656">Doesn&#39;t work in remote workspace (Remote - SSH) · Issue #39 · gencay/vscode-chatgpt</a></li><li><a href="https://github.com/microsoft/vscode-remote-release/issues/4522">Test Remote-SSH X11 forwarding · Issue #4522 · microsoft/vscode-remote-release</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7dab1d04e812" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[用python寫一個自動打卡系統]]></title>
            <link>https://ikaminyou.medium.com/%E7%94%A8python%E5%AF%AB%E4%B8%80%E5%80%8B%E8%87%AA%E5%8B%95%E6%89%93%E5%8D%A1%E7%B3%BB%E7%B5%B1-38c75473d66a?source=rss-2a1ce3813f59------2</link>
            <guid isPermaLink="false">https://medium.com/p/38c75473d66a</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[docker-compose]]></category>
            <category><![CDATA[system]]></category>
            <dc:creator><![CDATA[Kaminyou]]></dc:creator>
            <pubDate>Tue, 03 Jan 2023 12:41:16 GMT</pubDate>
            <atom:updated>2023-01-03T13:01:25.534Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>任何能用自己電腦上網打卡的系統，必定能照著以下思路寫出自動打卡器。</blockquote><p>想必大多數的人，不管是在學校、實習、工作期間，免不了被要求上下班打卡，但大部分的打卡系統應該都是要求大家登入後按下上下班打卡鍵即可，不禁讓人產生是否可以寫個python script定時在上下班時間自動打卡，省去忘記打卡的窘境。因此，今天便會以概念上講述如何開發這個系統，以及自己利用公司的打卡系統開發出的程式作為範例。</p><p>目前自己實作出來的系統已經開源在<a href="https://github.com/Kaminyou/femascloud-auto-puncher">GitHub repo</a>(請點擊之)中，應該對於使用xxxxx打卡系統的所有企業都可以直接clone下來使用，若是使用其他打卡系統的公司也可以比照此法進行開發。</p><h3>觀察API運作</h3><p>首先，請先打開chrome的開發者模式查看network，去觀察登入是打哪個API、打卡是打哪個API，以及在登入時有什麼cookie是被設置的、打卡時什麼cookie或是header是必要傳遞的、什麼data是要跟著傳過去的。</p><p>以下的內容便是以這樣的思維編排：</p><h4>A. 登入是打哪個API</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YJlj5rN9o_KaCIkFDkJ5ZA.png" /></figure><p>可以觀察到主要好像是前面的login以及下一個main?from=/…做了一些事情，因此點進去查看打的api以及封包：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9i6yVuCmCG5x_Rss1JBPSA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uaGjQiMjLBCeF97iJrYO3w.png" /></figure><p>發現只有login 這個以POST打了/domain/Account/login的API並且同時傳了帳號密碼過去，另一個main?from=/… 只是上一個動作後的重新導向。</p><p>接著觀察一下自己的瀏覽器，看一下有沒有什麼cookie被設定了。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/862/1*7a6wNOowqnohUOl-m8GnaQ.png" /></figure><p>看起來有三個cookie被設定了，但setTopMenu看了一下內容是空的，所以合理推測可能只有上面兩個有用途。</p><h4>B. 打卡是打哪個API</h4><p>接著來觀察打卡的API以及哪些cookie以及什麼data是需要被傳遞的。一樣以開發者模式觀察按下<strong>下班的打卡鍵</strong>後network中的狀況。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LlY0qOXLpQytK6CgtB6mng.png" /></figure><p>以POST打了兩個API： /users/clock_listing與/users/att_status_listing 。其中看起來只有/users/clock_listing的payload帶有有用訊息：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aKBLHd0LzT4G4qjTgv5ltQ.png" /></figure><p>另外測試了上班的打卡鍵，發現也是打/users/clock_listing但只有payload中data[ClockRecord][clock_type]的值會變換成S。而不管是上下班卡data[ClockRecord][user_id]與data[AttRecord][user_id] 都是同樣的值，推測這代表著要打誰的卡，但一般來說自己的帳號只能打自己的卡，為了測試這個行為，在下一個section中送POST時有試圖改user_id，發現確實能打到別人的卡，因此這個server實作上只會確認登入者是合法的，但不會檢查打卡跟登入者是否為同一人。</p><p>以上的觀察都沒有在payload中找到任何關於時間的蹤跡，故推測打卡的時間紀錄就是戳到該API瞬間的時間為主。</p><h4>C. 開始進行測試</h4><p>把整個POST request以curl的格式複製出來：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hdghuVq2cs8I7o0LPiRmPA.png" /></figure><p>雖然也可以直接用curl打，但我習慣把它轉成python requests的格式，可以使用<a href="https://curlconverter.com/">這個網站</a>來做：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z8bFknrHtcXq8AIw7rAfPQ.png" /></figure><p>直接用python執行這份script，<strong>發現打卡成功了</strong>！而<strong>下班時間也被更新到發出這個requests的時間點</strong>！而若把data[ClockRecord][clock_type]的值變換成S ，上班打卡原本已經打的時間並不會更新，因此推測：</p><ol><li>上下班卡都可以在同一天打無數次</li><li>上班卡，會紀錄最早那次的打卡時間</li><li>下班卡，會紀錄最晚那次的打卡時間</li></ol><p>所以若從早上9點到晚上7點，每個整點時間都同時打上下班卡，則最後的上班卡時間會是9點，下班卡時間是晚上7點。</p><h4>D. 什麼cookie或是header是必要傳遞的</h4><p>接著都以打下班卡的API來做實驗，因為只有下班卡才能確認API有打成功。首先，先確認什麼cookie是重要的，setTopMenu 必然沒用途直接忽略，剩下兩個cookie進行總共四種的所有組合的測試（全無、全有、有A無B、有B無A），發現lifeTim… 也是沒必要的，所以只有一個cookie是必要送出的，下一節再討論取得cookie的問題。</p><p>而header中也有許多冗余的項目，例如sec-ch-ua就不像是會需要的東西，經過一番嘗試，最後簡化的版本如<a href="https://github.com/Kaminyou/femascloud-auto-puncher/blob/main/puncher.py#L65">GitHub repo中request裡送出的header所示</a>。</p><h4>E. 如何取得cookie</h4><p>在network中搜尋Set-Cookie，發現cookie在進入登入頁面時便會被設置，接著POST /domain/Account/login 登入時，這個cookie同時會被送出去，而這個cookie也會在接下來打卡時一直被使用。總而言之，cookie在GET /accounts/login即可拿到，且登入後也不會被換掉。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4oFgZoirjLV8k6mE-VauAg.png" /></figure><p>為了確保接下來實作正確性，特別POST/domain/Account/login 登入但不帶上cookie，發現server端也會再提供另一組cookie，不過基本上代表用一開始GET /accounts/login 拿到的cookie即可。</p><h4>F. 如何得知user_id</h4><p>總不可能在這個系統實作出來後，使用者必須用以上這些方法才能得知自己的user_id，所以就在登入後的頁面切到Elements中搜尋看看，果然找到了一些地方擁有這樣的資訊，例如這個name=data[EboardBrowser][user_id]的element中：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*4DVcqm01Xqbugj8wgeBU3g.png" /></figure><h3>實作</h3><h4>A. 重整邏輯</h4><ol><li>GET /accounts/login 頁面拿到cookie</li><li>POST /Accounts/login 登入（payload置入帳號密碼，此時就要帶上cookie）</li><li>拿到上述的response後，找name=data[EboardBrowser][user_id] element拿到user_id</li><li>POST /users/clock_listing （payload置入user_id與要打上班或下班卡的flag，此時也要帶上cookie）</li><li>似乎不需要logout，那就不做了</li></ol><h4>B. 以python實作</h4><p>因為要處理cookie的問題，實作上以requests.Session搭配context manager會比較方便，請見<a href="https://github.com/Kaminyou/femascloud-auto-puncher/blob/main/puncher.py">repo</a>。整體實作起來的感覺如下：</p><pre>import requests<br><br>class Puncher:<br><br>    def __init__(self, account: str, password: str, subdomain: str):<br>        self.account = account<br>        self.password = password<br>        self.subdomain = subdomain<br><br>    def _api(self, api_name: str) -&gt; str:<br>        return f&quot;https://xxx_domain.com&quot; + api_name<br><br>    def __enter__(self):<br>        self.session = requests.Session()<br>        r = self.session.get(self._api(&quot;/accounts/login&quot;))<br><br>        headers = {<br>            ...<br>        }<br><br>        data = {<br>            &#39;data[Account][username]&#39;: self.account,<br>            &#39;data[Account][passwd]&#39;: self.password,<br>            &#39;data[remember]&#39;: &#39;0&#39;,<br>        }<br><br>        r = self.session.post(<br>            self._api(&quot;/Accounts/login&quot;),<br>            headers=headers,<br>            data=data,<br>        )<br>        soup = BeautifulSoup(r.text, &quot;html.parser&quot;)<br>        self.user_id = soup.find(<br>            &quot;input&quot;,<br>            {&quot;name&quot;: &quot;data[EboardBrowser][user_id]&quot;},<br>        ).attrs[&quot;value&quot;]<br><br>        return self<br><br>    def __exit__(self, type, value, traceback):<br>        self.session.close()<br><br>    def punch_in(self, user_id: t.Union[int, str]):<br>        headers = {<br>            ...<br>        }<br><br>        data = {<br>            &#39;_method&#39;: &#39;POST&#39;,<br>            &#39;data[ClockRecord][user_id]&#39;: str(user_id),<br>            &#39;data[AttRecord][user_id]&#39;: str(user_id),<br>            &#39;data[ClockRecord][shift_id]&#39;: &#39;10&#39;,<br>            &#39;data[ClockRecord][period]&#39;: &#39;1&#39;,<br>            &#39;data[ClockRecord][clock_type]&#39;: &#39;S&#39;,  # might use a flag to control<br>            &#39;data[ClockRecord][latitude]&#39;: &#39;&#39;,<br>            &#39;data[ClockRecord][longitude]&#39;: &#39;&#39;,<br>        }<br><br>        r = self.session.post(<br>            self._api(&quot;/users/clock_listing&quot;),<br>            headers=headers,<br>            data=data,<br>        )<br><br>        if r.status_code != HTTPStatus.OK:<br>            raise requests.exceptions.HTTPError(f&quot;status {r.status_code}&quot;)</pre><h4>C. 定時任務</h4><p>有了上述的Puncher class，接著就是讓它能定時在每天上下班時間自動打卡了，這邊選擇使用python celery library提供的<a href="https://docs.celeryq.dev/en/stable/userguide/periodic-tasks.html">Periodic Tasks</a>作法，請見<a href="https://github.com/Kaminyou/femascloud-auto-puncher/blob/main/main.py">repo</a>。</p><h4>D. Dockerize</h4><p>最後，如果能在隨手撿到的一台server都能部署這樣的服務幫忙自動打卡，那麼就真的方便極了！因此我把整個system寫成了<a href="https://github.com/Kaminyou/femascloud-auto-puncher/blob/main/docker-compose.yml">docker-compose</a>能一鍵部署的形式，另外celery需要用到的redis也能同時一併起起來！</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=38c75473d66a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[LeetCode 刷1500題心路歷程]]></title>
            <link>https://ikaminyou.medium.com/leetcode-%E5%88%B71500%E9%A1%8C%E5%BF%83%E8%B7%AF%E6%AD%B7%E7%A8%8B-8614284f03da?source=rss-2a1ce3813f59------2</link>
            <guid isPermaLink="false">https://medium.com/p/8614284f03da</guid>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[Kaminyou]]></dc:creator>
            <pubDate>Sun, 18 Dec 2022 06:45:13 GMT</pubDate>
            <atom:updated>2025-04-21T17:02:15.920Z</atom:updated>
            <content:encoded><![CDATA[<p>先附個圖，從2021/12/16買了一年premium開始從零開始刷到2022/12/16總共寫了1524題，並參加了58場weekly contest，基本上都以C++為主。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5WcfWHYTnIzie9-xaJ8MpA.jpeg" /></figure><p>以下講了含初階到進階的內容，若你還是新手中的新手也不想看這麼多字，請直接從<a href="https://github.com/youngyangyang04/leetcode-master"><strong>代碼隨想錄</strong></a>提供的200題包含講解開始刷起。</p><blockquote>結論：時間充足的話請從<a href="https://github.com/youngyangyang04/leetcode-master"><strong>代碼隨想錄</strong></a><strong>200題</strong>開始刷</blockquote><h4>動機</h4><p>起初只是為了準備FAANG等級的technical面試，到了後期目標轉向準備打各種年度競賽(e.g., Google Code Jam)，所以除了寫LeetCode，也有額外寫Codeforces的題以及打div2的比賽。此外，若LeetCode Contest Rating達到2300+，還有資格去應徵<a href="https://apply.workable.com/leetcode/j/A44EF0E1E1/">Internal Contest Tester</a> (part time)。</p><h4>什麼時候開始刷</h4><p>越早越好，不要等到需要用到時(例如面試或打比賽)再開始，自己是畢業後的一年利用工作閒暇之餘練習的，但這樣會挺勞累的。只是若還在學生時期，可能每天會很難有一段時間能好好練，需要有足夠的動機。</p><h4>開始練習前的Prerequisite</h4><p>至少先會<strong>任何</strong>語言的基本語法，並且修過資料結構及演算法。</p><h4>要用什麼語言寫，以及對語言的熟悉程度</h4><p>根據你所在地的各公司開出的職缺以及自己預計的發展目標選擇，若都沒想法C++是個不錯的選擇，因為職缺通常比較多，不過語言不是太大的問題，刷題只是練思維，想要轉換個語言只要記一下對應的資料結構用法即可，以及可能需要額外實作什麼資料結構。</p><p>例如使用C++刷題，沒意外的話會用到的standard template library (STL)<strong>只含vector、list、stack、queue、priority_queue、unordered_set、set、unordered_map、map、multiset、mulitmap</strong>需要額外實作的資料結構是<strong>Disjoint set、Trie、Binary index tree、Segment tree</strong>，若使用python，可能就沒有像C++有set (self-balancing binary search tree)能夠使用，需另求其他方法。</p><p>對於需要實作資料結構的狀況，建議練習到不需要思考就能憑著手感刻出來，例如實作Binary index tree時：</p><pre>BIT {<br>private:<br>    vector&lt;int&gt; tree;<br>public:<br>    BIT(int n) {<br>        tree.resize(n + 1, 0);<br>    }<br>    <br>    int lsb(int x) {<br>        return x &amp; (-x);<br>    }<br>    <br>    void add(int index, int value) {<br>        while (index &lt; tree.size()) {<br>            tree[index] += value;<br>            index += lsb(index);<br>        }<br>    }<br>    int query(int index) {<br>        int sum = 0;<br>        while (index &gt; 0) {<br>            sum += tree[index];<br>            index -= lsb(index);<br>        }   <br>        return sum;<br>    }<br>    int queryRange(int low, int high) {<br>        // [low, high]<br>        return query(high) - query(low - 1);<br>    }<br>};</pre><p>盡量練習到不需要額外思考究竟要用 &lt; 還是 ≤ ，或是lsb到底該怎麼做。</p><p>而對於一些常用演算法，如實作binary search時，很多人會遇到許多邊界狀況不太確定要用 &lt; 還是 ≤的狀況，以下是個人最常用的模板：</p><pre>int binarySearchLowerBound(vector&lt;int&gt;&amp; v, int target) {<br>    int left = 0;<br>    int right = v.size();<br>    <br>    while (left &lt; right) {<br>        int mid = left + (right - left) / 2;<br>        if (v[mid] &gt;= target) right = mid;<br>        else left = mid + 1;<br>    }<br>    return left;<br>}</pre><p>盡量練習到不需要額外思考究竟要用 &lt; 還是 ≤ ，至於要用哪種模板，則看自己的喜好。另外因為使用C++，需要額外考慮overflow問題，才會有left + (right — left) / 2的這行，若是用python，就不用有這個疑慮，但除法則要記得得使用整數除法。</p><h4>要不要買premium</h4><p>如果只是想看解答，那非official的解答已經足夠了，也時常寫得比official更好，此外直接google search也能查到不錯的教學。但premium有附上各種不同類型題目的整理集，以及各大公司的tag，買一年來把能力練起來是挺划算的。</p><h4>想找中文的解答以及講解</h4><p>以下附一些可以參考的網站</p><ul><li><a href="https://www.youtube.com/@wisdompeak">[Youtube] Huifeng Guan</a>：每週更新較困難的週賽題講解</li><li><a href="https://www.cnblogs.com/grandyang/">[博客園] Grandyang</a>：收錄題數很多，幾乎都找得到</li></ul><h4>最完整的題目分類</h4><p>請參見這個<a href="https://github.com/wisdompeak/LeetCode">repository(wisdompeak/LeetCode)</a>，分類到非常細緻的境界</p><h4>練習的順序</h4><p>若時間急迫，那<a href="https://leetcode.com/discuss/general-discussion/460599/blind-75-leetcode-questions">blind 75</a>是個好的開始，若有一段時間可以練習，這邊提供自己寫題的順序</p><ul><li>[0–100] <a href="https://leetcode.com/discuss/general-discussion/460599/blind-75-leetcode-questions">blind 75</a></li><li>[100–300] <a href="https://github.com/youngyangyang04/leetcode-master"><strong>代碼隨想錄</strong></a>：非常好用，講解超詳細</li><li>[300–500] <a href="https://docs.google.com/spreadsheets/d/1yRCOJ8KysRVkq0O9IlDriT01tC6lzPapmFO4PCmDJQA/edit#gid=126913158">花花酱 LeetCode 题目分類</a>：開始寫一些難題</li><li>[500–700] Premium限定，Dynamic programing I, II, III, IV等套餐</li><li>[700–1000] Premium限定，依照各大公司的題目集，寫完(例如我把google題目集的全部400多題寫完了)</li><li>[1000–1500] 自由隨機練習，在這個階段我把題數#1~#950還沒寫的寫完，以及寫題時把遇到的相關題目也寫了，此時也有一陣子強迫自己只寫hard題</li></ul><p>另外大約100題後能每天開始寫daily challenge，也能逐漸開始打weekly/biweekly contest。</p><h4>要寫多少才夠</h4><p>不少人會有疑惑究竟多少題才足夠面試，在這邊提供一點心境上的變化</p><ul><li>[0–100] medium以上都非常卡，不會寫的題看解答都要好久</li><li>[100–300] 部分medium題目能自己完成了，也漸漸學到各種技巧</li><li>[300–500] 逐漸熟悉常見的套路題</li><li>[500–1000] contest有機會四題完賽</li><li>[1000–1500] contest幾乎都能四題完賽，此時練的是思維上通靈的速度以提升排名</li></ul><p>所以基本上300題後非外商面試都不會有太大問題，500題後外商會有機會，不過真正判斷標準應該是以contest rating較客觀，也建議把1900+作為一個最低標：</p><ul><li>1800+~1900+：非外商面試都不會有太大問題</li><li>1900+~2000+：Google外的外商面試都不會有太大問題</li><li>2100+~2200+：外商面試都不會有太大問題，除非不考你一般演算法題</li></ul><p>另外若以題目難度來分，則：</p><ul><li>1800+~1900+：medium能寫出來</li><li>1900+~2000+：medium能快速寫出來，hard可能有一半機率能寫出來</li><li>2100+~2200+：hard大部分都能寫出來</li></ul><h4>整理寫題的紀錄</h4><p>為了能加速練習的進程，額外維護了一個google sheet來協助自己釐清不熟的題目，並且把寫過的題都分門別類整理在<a href="https://github.com/Kaminyou/MyLeetcode">GitHub</a>上。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x3p1Gw_T7aRsgdVZJaYBQA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*51nlFgow-BNjofQadRo3Pg.png" /></figure><p>google sheet上我標示了各種類別，另外也記錄了每次完成的日期，後期則在日期上額外加了顏色以區別當次完成的狀況，例如：</p><ul><li>綠色：不看hint自己寫完並且時間/空間複雜度達到最佳</li><li>黃色：時間/空間複雜度尚待改進</li><li>橘色：看了hint</li><li>淺藍色：implementation有需要改進之處</li><li>紫色：看了解答但沒看實作</li><li>紅色：看了解答又看了實作</li></ul><p>這樣每當下次daily challenge遇到之前已經綠色的題目，則可直接跳過避免浪費時間。</p><h4>空間時間複雜度(time/space complexity)需要追求到什麼極致</h4><p>很多人非常在意究竟能不能在time/space complexity上beat 100%，但基本上只要確定在big-O範疇跟最佳解一樣即可，例如一題array題，最佳解time complexity是O(N)，但自己只想到O(N²)或O(NlogN)的算法，那就需要額外學一下O(N)怎麼辦到的。</p><p>而許多的linked list題，常常有把O(2N)降到O(N)即single pass的技巧，若有時間這部分也是直得參考的，惝若你已經實作了一個O(N)算法且你確信自己的implementation前面的常數很小卻有沒辦法beat 100%，那去參考別人怎麼達到beat 100%就不是優先事項。舉<a href="https://leetcode.com/problems/middle-of-the-linked-list/">#876 Middle of the Linked List</a>為例，題目是要取出linked list最中間node的值：</p><ul><li>最簡單的做法可能是把linked list的值倒出來到一個vector&lt;int&gt;裡，此時就能得到size並直接從vector拿出最中間的點的值，這個方法time complexity是O(2N)，space complexity是O(N)</li><li>接著思考能不能不要把值取出的方法，若跑過兩次linked list，第一次得知node有多少個，第二次跑到中間時直接拿取答案，這樣這個方法time complexity是O(2N)，space complexity是O(1)</li><li>而最佳方法則是用slow/fast pointer，如以下所示time complexity是O(N)，space complexity是O(1)，其中這題不用使用<a href="https://en.wikipedia.org/wiki/Sentinel_node">sentinel</a>技巧，只是個人習慣操作linked list題會加上sentinel。</li></ul><pre>class Solution {<br>public:<br>    ListNode* middleNode(ListNode* head) {<br>        ListNode* sentinel = new ListNode(0, head);<br>        ListNode* slow = sentinel;<br>        ListNode* fast = sentinel;<br>        while(fast-&gt;next &amp;&amp; fast-&gt;next-&gt;next) {<br>            slow = slow-&gt;next;<br>            fast = fast-&gt;next-&gt;next;<br>        }<br>        return slow-&gt;next;<br>    }<br>};</pre><p>另外dynamic program題也有許多降space complexity的技巧，例如把space complexity O(N²)降到O(N)，或把O(N)降到O(1)。這也是有閒暇時值得學的。</p><p>有一題經典的<a href="https://leetcode.com/problems/longest-increasing-subsequence/">#300. Longest Increasing Subsequence</a>，就有很多不同複雜度的寫法：</p><ul><li>Brute-force: time complexity O(2^N); space complexity O(N)</li><li>Dynamic program: time complexity O(N²); space complexity O(N)</li><li>Binary search/BIT/Segment Tree: time complexity O(NlogN); space complexity O(N)</li></ul><p>這題的進階題<a href="https://leetcode.com/problems/longest-increasing-subsequence-ii/">#2407. Longest Increasing Subsequence II</a>則是必須使用Maximum Segment Tree才能達到不超時的時間複雜度。</p><h4>通靈出時間複雜度的需求</h4><p>比賽或寫題時，除了以自身經驗判斷最低的複雜度外，也能透過題目測資來猜測，例如：</p><ul><li>N=10⁵以上：time complexity只能接受O(N)或O(NlogN)</li><li>N=10³~10⁴：time complexity可接受O(N²)</li><li>N=10²：time complexity可接受O(N³)</li><li>N=神秘數字(不一定)，此時要猜測是否轉化題目而降低複雜度，例如<a href="https://leetcode.com/problems/substring-with-largest-variance/description/">#2272 Substring With Largest Variance</a>可以從iterate string變成iterate char (a-&gt;z)來降低複雜度</li></ul><h4>舉一個例子驗證多寫題目對思維的幫助</h4><p>這邊以2022/12/11週賽<a href="https://leetcode.com/problems/maximum-number-of-points-from-grid-queries/">#2503. Maximum Number of Points From Grid Queries</a>舉例，驗證多寫題目在思維上的助益。</p><p>這題給了一個矩陣(m x n)，再給了一個query array Q長度為k，要回傳一個長度為k的array A，裡面每個數值A[i]是假設當下Q[i]為最高限制，那從matrix左上角開始走，可以走上下左右，遇到比Q[i]則不走，那可以走多少個cell。</p><p>這題如果直接brute force解，對於每個query開始從矩陣左上角BFS，複雜度為O(mnk)，不可接受。</p><p>這題比賽當下看完題目我就想到了解法，所以時間就僅花在implementation上：</p><ol><li><strong>重新排序query</strong>，從小到大，並且記錄原先數值在query中的index：在許多medium~hard題有用這個技巧(待我翻找出來後更新)。</li><li>因為query變成由小到大，那對應的answer也會從小到大，所以對於每個Q[i]，只要目前所在的點周圍比該數值低，就繼續走，也就是隨著query增加，逐漸拓增版圖：在許多medium~hard題有用這個技巧(待我翻找出來後更新)。</li></ol><p>解題的思維或技巧，需要在很多練習下建立起自己的資料庫，每遇到新的題目就拿來用用看。</p><h4>統整一些題目類型（粗體者是一定要精熟的）</h4><ul><li><strong>Simulation</strong>：題目說什麼就照著做，通常測資很小就沒問題</li><li><strong>String</strong>：非常多元，實質上是array，這種題目在hard上可能可以以char的iteration來考慮，要細分的話尚有<strong>Rabin-Karp</strong>與KMP兩個類型</li><li><strong>Array</strong>：千奇百怪，細分則還有<strong>Prefix sum</strong>與Difference array類型</li><li><strong>Matrix</strong>：千奇百怪，細分同上還有<strong>Prefix sum</strong>與Difference array類型</li><li><strong>Linked list</strong>：挺單一的</li><li><strong>Stack</strong>：挺單一的，但萬一出了<strong>Monotonic stack</strong>就很需要解題技術</li><li><strong>Queue</strong>：挺單一的</li><li><strong>Deque</strong>：挺單一的，但萬一出了Monotonic deque就很需要解題技術</li><li><strong>Two pointers(=sliding window)</strong>：常用的技巧</li><li><strong>Backtracking</strong>：常用的技巧，本質算是DFS</li><li><strong>BFS/DFS</strong>：常用</li><li><strong>Disjoint set</strong> (=<strong>Union find</strong>)：常常可以神奇地解掉問題</li><li><strong>Greedy</strong>：常用的技巧，DP的每次狀態轉移也算是一種greedy</li><li><strong>Tree</strong>：千奇百怪，使用DFS recursive方式能解掉許多困難題</li><li><strong>Quad Tree</strong>：不常見</li><li><strong>Self-balanced binary search tree/Multiset</strong>：常用的結構拿來解如729. My Calendar I, 731. My Calendar II, 732. My Calendar III類似的題</li><li><strong>Trie</strong>：常常一看就知道要用它</li><li>Binary Index Tree (Fenwick tree)：偏不常見</li><li>Segment tree：不常見</li><li><strong>Heap</strong>(=<strong>Priority queue</strong>)：會時常在greedy算法中使用</li><li><strong>Graph</strong>：主要含<strong>Topological sort</strong>、<strong>Dijkstra’s algorithm</strong>、Bellman-Ford algorithm、Tarjan’s algorithm、Hierholzer’s algorithm，出現率由大到小</li><li><strong>Binary search</strong>：很重要，拿來搜答案，LeetCode中有<a href="https://leetcode.com/discuss/general-discussion/786126/Python-Powerful-Ultimate-Binary-Search-Template.-Solved-many-problems">一篇技巧文</a>值得一讀</li><li><strong>Dynamic programming</strong>：千奇百怪</li><li>Divide and conquer：不常見</li><li><strong>Bit operation</strong>：千奇百怪，在DP中使用的bit mask可能也算這類型的應用</li><li><strong>Geometry</strong>：千奇百怪，像是Andrew’s Monotone Chain</li><li><strong>System design</strong>：須結合上面各種結構技巧</li><li><strong>Math</strong>：考驗數學知識水平與通靈能力，如Bézout’s identity、Algorithm R、Qubit</li><li>Others：De Bruijn sequence with Eulerian Circuit、Boyer-Moore Voting Algorithm、<strong>Kadane’s algorithm</strong>(屬於greedy的一種)、Sieve of Eratosthenes、Binary lifting</li></ul><h4>總結</h4><p>一開始會是非常艱難的過程，但真的花時間練下去，進步是很可觀的，例如在多年前課堂作業出了<a href="https://leetcode.com/problems/candy-crush/">#723. Candy Crush</a> (當時並沒特別去網路上找答案就自己開刻了)，花了將近20小時才完成，如今，只要10分鐘就能完成了！另外這些演算法思維，只要維持著，那在工作或面試等都時不時有相當助益，也能避免突然需要用上時沒空練習的窘境。</p><p>若對寫題目還有其他問題或是想進行google style mock interview，歡迎來信詢問(ikaminyou@gmail.com)。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8614284f03da" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>