<?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 ChienLing on Medium]]></title>
        <description><![CDATA[Stories by ChienLing on Medium]]></description>
        <link>https://medium.com/@zaqxsw2230?source=rss-b3d76b2231db------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*FHFjk4W7kr-2SX0sBf03mg.jpeg</url>
            <title>Stories by ChienLing on Medium</title>
            <link>https://medium.com/@zaqxsw2230?source=rss-b3d76b2231db------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 09:51:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@zaqxsw2230/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[857. Minimum Cost to Hire K Workers]]></title>
            <link>https://medium.com/chienlingalgorithm/857-minimum-cost-to-hire-k-workers-c986d794acf8?source=rss-b3d76b2231db------2</link>
            <guid isPermaLink="false">https://medium.com/p/c986d794acf8</guid>
            <category><![CDATA[leetcode-hard]]></category>
            <dc:creator><![CDATA[ChienLing]]></dc:creator>
            <pubDate>Mon, 20 Jul 2020 15:48:52 GMT</pubDate>
            <atom:updated>2020-08-07T05:35:55.218Z</atom:updated>
            <content:encoded><![CDATA[<p>There are N workers. The i-th worker has a quality[i] and a minimum wage expectation wage[i].</p><p>Now we want to hire exactly K workers to form a <em>paid group</em>. When hiring a group of K workers, we must pay them according to the following rules:</p><ol><li>Every worker in the paid group should be paid in the ratio of their quality compared to other workers in the paid group.</li><li>Every worker in the paid group must be paid at least their minimum wage expectation.</li></ol><p>Return the least amount of money needed to form a paid group satisfying the above conditions.</p><h4>題意:</h4><p>K表示雇用的員工數，wage[i]表示第i號人最低的接受價位，而每位雇員受薪須滿足quality的比例。</p><p><strong>Example 1:</strong></p><pre><strong>Input: </strong>quality = [10,20,5], wage = [70,50,30], K = 2<br><strong>Output: </strong>105.00000<br><strong>Explanation</strong>: We pay 70 to 0-th worker and 35 to 2-th worker.</pre><p>例如雇用0號，顧薪為70，此時如果再雇用1號，則要花費70*(20/10)=140 (quality比為20:10)，總共花費210。若第二個雇用的為2號，則花費70*(5/10)=35(quality比為5:10)，總共花費105。</p><p>而若先雇用1號，薪水為50，此時再雇用2號，則支薪50*(5/20)=12.5，此時12.5小於30，不滿足題意。</p><p>故解為105。</p><p>來源:<a href="https://leetcode.com/problems/minimum-cost-to-hire-k-workers/">https://leetcode.com/problems/minimum-cost-to-hire-k-workers/</a></p><h4>解法:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/791/1*vuiqS9Q5vT1Z417ZLXD3Gg.png" /></figure><p>設quality 、wage 、K如左圖，則設一個vector ratio = wage/quality (每單位顧薪)。</p><p>用一個vector record 將ratio 與 quality成對存起，此時將record做sort。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/945/1*P8jeP5eYCiWmBmWiM2lE3g.png" /></figure><p>用一個vector Tem Qua紀錄record的quality部分。對Tem Qua前K-1(K=3)項做sort，並雇用前K-1人與第K人，雇用總薪資如左圖Min，每單位顧薪此時為5。record排序第K人，因以ratio作為sort的目標，故使用index較後面雇員的ratio可以保證必不會讓前面的雇員受薪低於其wage)。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1007/1*NM0eW4g7mPWcZRmFy3NOpQ.png" /></figure><p>接下來for loop會繼續計算以從第K+1人至第n人ratio為基準之總支薪。當以第K+1人作為基準時，將第K人(其index為K-1)的quality拿去與第index K-2的quality比較，若小於TemQua[K-2]，則對前K-1項做Insertion Sort，否則不做事。Min計算如右圖。loop結束後回傳Min值。</p><p>時間複雜度:</p><ol><li>對record做sort O(nlogn)</li><li>將qua後面的quality與前K-1項做Insertion Sort:O(K)，共n-(K-1)次: O(n)</li></ol><p>Time complexity : O(nlogn)</p><p>Space complexity : O(n)</p><h4>程式碼:</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1697d9aee2eeb88a5bb54b2de37c5f7d/href">https://medium.com/media/1697d9aee2eeb88a5bb54b2de37c5f7d/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c986d794acf8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/chienlingalgorithm/857-minimum-cost-to-hire-k-workers-c986d794acf8">857. Minimum Cost to Hire K Workers</a> was originally published in <a href="https://medium.com/chienlingalgorithm">LeetcodeReview</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[CPE 11284. Shopping Trip]]></title>
            <link>https://medium.com/cpe-review/cpe-11284-shopping-trip-a301208f6f8?source=rss-b3d76b2231db------2</link>
            <guid isPermaLink="false">https://medium.com/p/a301208f6f8</guid>
            <dc:creator><![CDATA[ChienLing]]></dc:creator>
            <pubDate>Tue, 14 Jul 2020 03:33:25 GMT</pubDate>
            <atom:updated>2020-07-14T07:39:48.548Z</atom:updated>
            <content:encoded><![CDATA[<p>將問題轉為Traveling-Salesman Problem。</p><ol><li>用Floyd-Warshall將各點間最短距離求出。</li><li>將可以節省費用的店家做標示，並將不可省錢的店家省去，且以最短路徑重新標上路徑成本。</li><li>使用Prim&#39;s Algo. 找出以Daniel 家為root的 Minimum Spanning Tree。</li><li>走訪各點。</li><li>各點節省之費用減去路徑成本即為解。</li></ol><p>Graph:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/930/1*Y71LpgPvpgMZKQ_WIvJr1g.png" /><figcaption>(左圖)為原圖 (右圖)為各點間最短路徑表格</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/1*XQTzqCUZlhu7LZpQ7YVa0A.png" /><figcaption>(左圖)為重新標示路徑之新圖 (右圖)DFS走訪左圖之MST</figcaption></figure><p>若以cost(T)表示MST路徑cost總和，cost(H*)表示TSP最佳解，則可得出:</p><p>cost(T)≤cost(H*)</p><p>而上述走訪路徑花費cost(P) = 2 cost(T)</p><p>可以得出走訪路徑 cost(P) ≤ 2 cost(H*)</p><p>此作法不為Shopping Trip最佳解，若部分店家可省金額遠低於路途花費，則會多花許多路徑成本，可以此類店家視作不可節省支出的店家做刪減改善結果。</p><p>程式碼:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c120fc208fe7bee01c52c2124d344e4/href">https://medium.com/media/8c120fc208fe7bee01c52c2124d344e4/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a301208f6f8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cpe-review/cpe-11284-shopping-trip-a301208f6f8">CPE 11284. Shopping Trip</a> was originally published in <a href="https://medium.com/cpe-review">CPE review</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[42. Trapping Rain Water]]></title>
            <link>https://medium.com/chienlingalgorithm/42-trapping-rain-water-d10c6fbe85b1?source=rss-b3d76b2231db------2</link>
            <guid isPermaLink="false">https://medium.com/p/d10c6fbe85b1</guid>
            <category><![CDATA[leetcode-hard]]></category>
            <dc:creator><![CDATA[ChienLing]]></dc:creator>
            <pubDate>Tue, 07 Jul 2020 07:26:38 GMT</pubDate>
            <atom:updated>2020-07-11T15:36:40.008Z</atom:updated>
            <content:encoded><![CDATA[<p>Given <em>n</em> non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.</p><p>給定n個非負整數表示各index的高度，各index寬度皆為1，若所有高低差可以盛裝雨水，求最多雨水之量。</p><p>例:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/471/1*qd0sybCT5x2GKegz2Jnvfw.png" /></figure><p>Input : [1,0,1,2,1,1,3,0,0,1,2]</p><p>Output : 8</p><p>解法:</p><p>先設 h=0，找到Input中最左&gt;h之值與最右&gt;h之值，如例題Lvalue = 1，Lidx = 0與Rvalue = 2，Ridx=10兩值。</p><p>Lvalue: 最左邊往右第一個&gt;h之值，Lidx : Lvalue 之index</p><p>Rvalue: 最右邊往左第一個&gt;h之值，Ridx : Rvalue 之index</p><p>累計雨水為Sum = min{1,2} * (Ridx-Lidx+1)，如下圖灰色區域 :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/472/1*G6Zah_x9cdMgcPoCEmKedg.png" /></figure><p>更新h=min{1,2}。</p><p>此時再從Lidx往右找&gt;h之值與從Ridx往左找&gt;h之值，此時Lvalue = 2，Lidx = 3，Rvalue = 2，Ridx = 10。</p><p>Sum += min{2,2} * (Ridx-Lidx+1)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/472/1*UQDpwxmIQJPDIQDIWLYfHQ.png" /></figure><p>直到 Ridx ≤ Lidx停止。</p><p>此時若Ridx = Lidx，則如上圖所示，會有一塊突出的黑色，面積為Input[Ridx]- h，先做紀錄。</p><p>area = input內所有值相加，Sum = Sum - area - (Input[Ridx]- h)，最後Sum為解。</p><p>程式碼：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f5c7d9890e3560efc0ddae516dce482e/href">https://medium.com/media/f5c7d9890e3560efc0ddae516dce482e/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d10c6fbe85b1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/chienlingalgorithm/42-trapping-rain-water-d10c6fbe85b1">42. Trapping Rain Water</a> was originally published in <a href="https://medium.com/chienlingalgorithm">LeetcodeReview</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[CPE 11284]]></title>
            <link>https://medium.com/cpe-review/cpe-11284-9032fcfc6a2c?source=rss-b3d76b2231db------2</link>
            <guid isPermaLink="false">https://medium.com/p/9032fcfc6a2c</guid>
            <dc:creator><![CDATA[ChienLing]]></dc:creator>
            <pubDate>Mon, 15 Jun 2020 09:28:47 GMT</pubDate>
            <atom:updated>2020-06-23T03:13:43.860Z</atom:updated>
            <content:encoded><![CDATA[<h3>CPE 11284. Shopping Trip</h3><h4>題目:</h4><iframe src="https://drive.google.com/viewerng/viewer?url=https%3A//onlinejudge.org/external/112/p11284.pdf&amp;embedded=true" width="600" height="780" frameborder="0" scrolling="no"><a href="https://medium.com/media/730c348709670fb025622fb171e6ddbd/href">https://medium.com/media/730c348709670fb025622fb171e6ddbd/href</a></iframe><p>給定店家數N(1≤N≤50)(其中Daniel家為node 0)、路徑數M(1≤M≤1000)及各路徑的成本與Daniel想買的DVD數P(1≤P≤12)及去各店家可節省之預算。</p><p>求解是否存在路徑使得Daniel出門後可以節省買DVD的花費，若有，則輸出最多可以節省之值，否則輸出&quot;Don’t leave the house&quot;。</p><h4>解法:</h4><p>使用Dynamic Programing解。</p><p>使用Floyd Warshall 計算各node( node 0 為Daniel家，其餘node為店家)間最短距離，存入dist矩陣。</p><p>將可以省錢的店家存入store矩陣，若只走node 1,3,4可以省錢，則store[0]=1, store[1]=3,store[2]=4。故store矩陣size為可以省錢的店家數 +1。</p><p>因此只須探討去各個可以省錢的店家的情況。若可以省錢的店家數為DVDstore(題目規定可以省錢的店家最多12家),則用pow(2,DVDstore)-1，則二進位表示法將有k個1。</p><p>執行迴圈:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c4591c1fe1ff1639cdf22bb5e978627b/href">https://medium.com/media/c4591c1fe1ff1639cdf22bb5e978627b/href</a></iframe><p>其中Floyd Warshall time complexity: O(N³)</p><p>For loop time complexity:: 由於P最大僅12，故可知要去的DVDstore ≤ 12，故為O(12*2¹²*12) = O(1)。</p><p>Time Complexity: O(N³)</p><p>Space Complexity: O(1)</p><p>題目來源:</p><p><a href="https://cpe.cse.nsysu.edu.tw/cpe/test_data/2020-05-26">https://cpe.cse.nsysu.edu.tw/cpe/test_data/2020-05-26</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9032fcfc6a2c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cpe-review/cpe-11284-9032fcfc6a2c">CPE 11284</a> was originally published in <a href="https://medium.com/cpe-review">CPE review</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[11730. Number Transformation]]></title>
            <link>https://medium.com/cpe-review/11730-number-transformation-83ca57363418?source=rss-b3d76b2231db------2</link>
            <guid isPermaLink="false">https://medium.com/p/83ca57363418</guid>
            <dc:creator><![CDATA[ChienLing]]></dc:creator>
            <pubDate>Mon, 08 Jun 2020 15:40:48 GMT</pubDate>
            <atom:updated>2020-06-13T07:24:05.195Z</atom:updated>
            <content:encoded><![CDATA[<p>題目:</p><iframe src="https://drive.google.com/viewerng/viewer?url=https%3A//onlinejudge.org/external/117/p11730.pdf&amp;embedded=true" width="600" height="780" frameborder="0" scrolling="no"><a href="https://medium.com/media/c73ebfdf1ed60f4a2b7b2a9d724b2129/href">https://medium.com/media/c73ebfdf1ed60f4a2b7b2a9d724b2129/href</a></iframe><p>題意:</p><p>將x轉成y，且過程中只允許使用加法，每次所能加之值為當前值之質因數(但不包含自己)。</p><p>例:x=6, y=12</p><p>6之質因數為2,3，故</p><p>6+2=8 -&gt; 8+2=10 -&gt;10+2=12/10+5=15</p><p>6+3=9 -&gt; 9+3=12</p><p>由6-&gt;8-&gt;10-&gt;12須走3步，而6-&gt;9-&gt;12僅需2步，故最小步數為2。</p><p>若沒有解答，則輸出-1。</p><h4>解法:</h4><p>1.使用FindPri 副程式先找出2–1000所有質數</p><p>2.使用FP找出1–1000內各值之質因數。</p><p>3.使用map紀錄當前值及步數，從key值最小的往後推。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/864/1*yDsFfY4b_HmTk06ZqMJGgw.png" /></figure><p>4.返回map[y]，若無則返回-1。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c620670325a6b95f6dbe4dbef5a0e0b5/href">https://medium.com/media/c620670325a6b95f6dbe4dbef5a0e0b5/href</a></iframe><p>題目來源:</p><p><a href="https://cpe.cse.nsysu.edu.tw/cpe/test_data/2020-05-26">https://cpe.cse.nsysu.edu.tw/cpe/test_data/2020-05-26</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=83ca57363418" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cpe-review/11730-number-transformation-83ca57363418">11730. Number Transformation</a> was originally published in <a href="https://medium.com/cpe-review">CPE review</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[1192. Critical Connections in a Network]]></title>
            <link>https://medium.com/chienlingalgorithm/1192-critical-connections-in-a-network-bf57ed874c1b?source=rss-b3d76b2231db------2</link>
            <guid isPermaLink="false">https://medium.com/p/bf57ed874c1b</guid>
            <category><![CDATA[leetcode-hard]]></category>
            <dc:creator><![CDATA[ChienLing]]></dc:creator>
            <pubDate>Mon, 27 Apr 2020 13:58:27 GMT</pubDate>
            <atom:updated>2020-08-24T04:42:51.659Z</atom:updated>
            <content:encoded><![CDATA[<h4>題目:</h4><p>There are n servers numbered from 0 to n-1 connected by undirected server-to-server connections forming a network where connections[i] = [a, b] represents a connection between servers a and b. Any server can reach any other server directly or indirectly through the network.</p><p>A <em>critical connection</em> is a connection that, if removed, will make some server unable to reach some other server.</p><p>Return all critical connections in the network in any order.</p><p>找出無向圖中所有critical path。</p><p>critical path定義:圖中若去掉此邊，將會增加一個component。</p><h4>解法:</h4><ol><li>先用DFS紀錄尋訪時間點</li><li>對圖中所有點都檢查其相鄰點，假設檢查點v時，若相鄰點有非DFS走訪路徑時的上一點，且該點的DFS小於自己，可知此為一條back edge，圖中必形成cycle。故將此相鄰點u至v所有點的low值改為u點的dfs尋訪值。</li><li>最後檢查所有邊，若edge(u,v)的點 u 與點 v 的low值不同，可視為不在一個cycle內，edge(u,v)可視為critical path</li></ol><h4>錯誤示範:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/767/1*EJsw5A_kHxRFa9JgI_K8Tw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/971/1*oKwj6OwuE8OWkJQVYdEv4w.png" /></figure><p>由於C點的dfs值比A小，故會將A、B、C三點low值改為A的low值(A、B、C三點low值皆等於1)，但D點dfs值較B小，故又會將B、C、D三點改為B點的low值(B、C、D三點low值皆等於1)。</p><p>此時測試所有的邊，若邊的兩點low值不同，則表示此邊為critical path。</p><p>但是實際測驗卻有錯誤，上圖是範圍測試得到正確的解，但是leetcode錯誤的範例有1000個node(其他條件測試皆通過)，目前還不知道為何錯，若過兩天還是想不到再去討論區參考其他人解答。</p><h4>程式碼:</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c9fd8e8e410932e4f41163d0e7d7ad30/href">https://medium.com/media/c9fd8e8e410932e4f41163d0e7d7ad30/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bf57ed874c1b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/chienlingalgorithm/1192-critical-connections-in-a-network-bf57ed874c1b">1192. Critical Connections in a Network</a> was originally published in <a href="https://medium.com/chienlingalgorithm">LeetcodeReview</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Floyd-Warshall Algorithm]]></title>
            <link>https://medium.com/chienlingalgorithm/floyd-warshall-algorithm-ea0f6a5b33da?source=rss-b3d76b2231db------2</link>
            <guid isPermaLink="false">https://medium.com/p/ea0f6a5b33da</guid>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[ChienLing]]></dc:creator>
            <pubDate>Sun, 26 Apr 2020 15:05:11 GMT</pubDate>
            <atom:updated>2020-04-27T14:43:55.147Z</atom:updated>
            <content:encoded><![CDATA[<p>解 all-pairs shortest path problem的演算法</p><h4>特色:</h4><ol><li>採取DP策略</li><li>可允許圖中有負邊</li></ol><h4>概念:</h4><p>設共有V個點，E個邊。任意path 點u至點v: u --&gt;...--&gt;i--&gt;...--&gt;v，不斷測試u至v若經過i點(for all i ϵ Graph.Vertex)是否能降低path長度。</p><p>若可以降低path長度，則將點 u 至點 v 的路徑長改為點 u 至點 i 的路徑長加上點 i 至點 v 的路徑長，否則不做變動。</p><p>Time complexity:O(V³)</p><p>Space complexity:O(V²)</p><h4>程式碼:</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e76396fa82fe73f1e8b0c738ed3ad493/href">https://medium.com/media/e76396fa82fe73f1e8b0c738ed3ad493/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ea0f6a5b33da" width="1" height="1" alt=""><hr><p><a href="https://medium.com/chienlingalgorithm/floyd-warshall-algorithm-ea0f6a5b33da">Floyd-Warshall Algorithm</a> was originally published in <a href="https://medium.com/chienlingalgorithm">LeetcodeReview</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Bellman Ford Algorithm]]></title>
            <link>https://medium.com/chienlingalgorithm/bellman-ford-algorithm-663277680a08?source=rss-b3d76b2231db------2</link>
            <guid isPermaLink="false">https://medium.com/p/663277680a08</guid>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[ChienLing]]></dc:creator>
            <pubDate>Sun, 26 Apr 2020 14:33:52 GMT</pubDate>
            <atom:updated>2020-04-27T13:55:13.335Z</atom:updated>
            <content:encoded><![CDATA[<p>解single-source shortest path problem的演算法</p><h4>特色:</h4><ol><li>可解有負邊的圖</li><li>使用DP策略</li></ol><h4>概念:</h4><p>定義 Dist¹[i]為i點距離起點之距離，cost(u,v)為點u到點v之weight，起點s之Dist¹[s]為零。</p><p>若存在edge(u,v)，則測試Dist[v]是否大於Dist[u] + cost(u,v)，若有，則更新點v的距離、最短路徑中v的上一點及s到v的邊數。測試所有邊共V-1次，即可完成s至所有點之最短路徑尋找。</p><p>Time complexity: O(VE)</p><p>Space complexity: O(V)</p><h4>程式碼:</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/89ee179ac2b61482c8f12820e3761a0b/href">https://medium.com/media/89ee179ac2b61482c8f12820e3761a0b/href</a></iframe><p>補充:</p><p>若再執行一次所有邊的relax，有任何一點的distance變小，則有負cycle。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=663277680a08" width="1" height="1" alt=""><hr><p><a href="https://medium.com/chienlingalgorithm/bellman-ford-algorithm-663277680a08">Bellman Ford Algorithm</a> was originally published in <a href="https://medium.com/chienlingalgorithm">LeetcodeReview</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[295. Find Median from Data Stream]]></title>
            <link>https://medium.com/chienlingalgorithm/295-find-median-from-data-stream-5d9cd1a837e6?source=rss-b3d76b2231db------2</link>
            <guid isPermaLink="false">https://medium.com/p/5d9cd1a837e6</guid>
            <category><![CDATA[leetcode-hard]]></category>
            <dc:creator><![CDATA[ChienLing]]></dc:creator>
            <pubDate>Sat, 25 Apr 2020 14:03:13 GMT</pubDate>
            <atom:updated>2020-04-25T14:03:13.497Z</atom:updated>
            <content:encoded><![CDATA[<h4>題目:</h4><p>Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.</p><p>For example:</p><p>[2,3,4], the median is 3</p><p>[2,3], the median is (2 + 3) / 2 = 2.5</p><p>製作一個object，其member function 可以做:</p><ol><li>input value</li><li>傳回按照大小排序後的數列的中間值(但不一定要做排序)</li></ol><h4>解法:</h4><p>使用兩個priority Queue(heap)，使用Queue minQ(max heap)放左半段data(值小於等於中間點的data)，Queue maxQ(min heap)放右半段data(值小於等於中間點的data)。</p><p>維持兩個Queue內資料數相差不大於一且 minQ資料數≥maxQ資料數，每次加入一個資料進去D，先檢查總資料數。</p><p>若maxQ資料數小於總資料數一半，則比較D與minQ的最大值，若D比minQ最大值小，則將D放入minQ，將minQ最大值放入maxQ並自minQ pop掉。</p><p>否則，確認是否maxQ為空，若為空，則將D 放入minQ，再將minQ最大值放入maxQ。</p><p>若maxQ不為空，比較D與maxQ最小值，若D大於maxQ最小值，則將D放入maxQ，將maxQ最小值放入minQ並自maxQ pop掉，否則直接放入minQ。</p><p>最後若資料總數為奇數，則median為minQ中最大值</p><p>否則為(minQ.top + maxQ.top)/2.0</p><p>Time complexity: O(nlogn)</p><p>Space complexity: O(n)</p><h4>示意圖:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CaEeKvQPYiI89t2cAlcFXA.png" /></figure><h4>程式碼:</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f04f519561876a6f1e225c5cfa79acd5/href">https://medium.com/media/f04f519561876a6f1e225c5cfa79acd5/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5d9cd1a837e6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/chienlingalgorithm/295-find-median-from-data-stream-5d9cd1a837e6">295. Find Median from Data Stream</a> was originally published in <a href="https://medium.com/chienlingalgorithm">LeetcodeReview</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dijkstra’s Algorithm]]></title>
            <link>https://medium.com/chienlingalgorithm/dijkstras-algorithm-f94d9da3cf6d?source=rss-b3d76b2231db------2</link>
            <guid isPermaLink="false">https://medium.com/p/f94d9da3cf6d</guid>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[ChienLing]]></dc:creator>
            <pubDate>Sat, 25 Apr 2020 08:34:59 GMT</pubDate>
            <atom:updated>2020-04-27T14:41:36.268Z</atom:updated>
            <content:encoded><![CDATA[<p>解single-source shortest path problem的演算法</p><h4>特色:</h4><p>1.為Greedy的演算法</p><p>2.只能解無負邊Graph</p><h4>概念:</h4><p>將起始點(src)的distance設為0，其餘點之distance皆設為無限大。</p><p>從vertex src開始relax其adjacency vertex，並選定下個distance最小之點，重複動作直到所有點皆被選取過。</p><p>其中relax vertex v動作表示將鄰點(vertex u)的distance與 vertex v的distance + W(v,u)比較，若u.distance &gt; v.distance + W(v,u)，則更新u.distance為v.distance + W(v,u)，且紀錄u點上一點為v點。</p><h4>示意:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/963/1*4_JclEoqgDDfMoI0JJO3nQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/553/1*Lv9vDZV-rLpsUxShlv4VJw.png" /></figure><p>如左圖，一開始將vertex <strong>0</strong>的distance設置為零。</p><p>經過一次relax後，選定剩下點中最小distance的點 vertex <strong>2，</strong>循環往復直至只剩一點沒被選定過(因為最後一點不需要再relax其他點)。</p><h4>Pseudo code:</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4c200e2b97efcca2174b44b9494194db/href">https://medium.com/media/4c200e2b97efcca2174b44b9494194db/href</a></iframe><h4>程式碼:</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c7a13fb2678d8a5c4a08ed8f30b071f/href">https://medium.com/media/8c7a13fb2678d8a5c4a08ed8f30b071f/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f94d9da3cf6d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/chienlingalgorithm/dijkstras-algorithm-f94d9da3cf6d">Dijkstra’s Algorithm</a> was originally published in <a href="https://medium.com/chienlingalgorithm">LeetcodeReview</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>