<?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 Gary Chiang on Medium]]></title>
        <description><![CDATA[Stories by Gary Chiang on Medium]]></description>
        <link>https://medium.com/@kchiang6?source=rss-1626cdbee09------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*LBDS_1W6dRHSu99cCyG29A.jpeg</url>
            <title>Stories by Gary Chiang on Medium</title>
            <link>https://medium.com/@kchiang6?source=rss-1626cdbee09------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 10:56:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kchiang6/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[Leetcode 525. Contiguous Array]]></title>
            <link>https://kchiang6.medium.com/leetcode-525-contiguous-array-53032853e764?source=rss-1626cdbee09------2</link>
            <guid isPermaLink="false">https://medium.com/p/53032853e764</guid>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[leetcode-medium]]></category>
            <dc:creator><![CDATA[Gary Chiang]]></dc:creator>
            <pubDate>Fri, 04 Feb 2022 18:50:33 GMT</pubDate>
            <atom:updated>2022-02-04T18:50:33.548Z</atom:updated>
            <content:encoded><![CDATA[<p>[medium]</p><p><a href="https://leetcode.com/problems/contiguous-array/">Contiguous Array - LeetCode</a></p><p>Given a binary array nums, return <em>the maximum length of a contiguous subarray with an equal number of </em>0<em> and </em>1.</p><p><strong>Example 1:</strong></p><pre><strong>Input:</strong> nums = [0,1]<br><strong>Output:</strong> 2<br><strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1.</pre><p><strong>Example 2:</strong></p><pre><strong>Input:</strong> nums = [0,1,0]<br><strong>Output:</strong> 2<br><strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1.</pre><p><strong>Constraints:</strong></p><ul><li>1 &lt;= nums.length &lt;= 105</li><li>nums[i] is either 0 or 1.</li></ul><p>[Think]</p><ol><li>I was think using sliding window in this question, and hashmap would be good to notice current total sum.</li><li>if we found another number with the same sum, which mean they have the same 0 and 1 between them. calculate the different between these two number</li></ol><p>3. map.put(0, -1) -&gt; current sum equal 0, add the first one if total array equal to 0.</p><p>[Java]</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/71d5bf710fba70e414d3f3a0f95b36b3/href">https://medium.com/media/71d5bf710fba70e414d3f3a0f95b36b3/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=53032853e764" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode 1514. Path with Maximum Probability]]></title>
            <link>https://kchiang6.medium.com/leetcode-1514-path-with-maximum-probability-5920969026ae?source=rss-1626cdbee09------2</link>
            <guid isPermaLink="false">https://medium.com/p/5920969026ae</guid>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[leetcode-medium]]></category>
            <dc:creator><![CDATA[Gary Chiang]]></dc:creator>
            <pubDate>Fri, 28 Jan 2022 00:37:02 GMT</pubDate>
            <atom:updated>2022-01-28T00:37:02.139Z</atom:updated>
            <content:encoded><![CDATA[<p>[medium]</p><p><a href="https://leetcode.com/problems/path-with-maximum-probability/">Path with Maximum Probability - LeetCode</a></p><p>You are given an undirected weighted graph of n nodes (0-indexed), represented by an edge list where edges[i] = [a, b] is an undirected edge connecting the nodes a and b with a probability of success of traversing that edge succProb[i].</p><p>Given two nodes start and end, find the path with the maximum probability of success to go from start to end and return its success probability.</p><p>If there is no path from start to end, <strong>return 0</strong>. Your answer will be accepted if it differs from the correct answer by at most <strong>1e-5</strong>.</p><p><strong>Example 1:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/187/0*i7vqAB9meeOxS08L.png" /></figure><pre><strong>Input:</strong> n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.2], start = 0, end = 2<br><strong>Output:</strong> 0.25000<br><strong>Explanation:</strong> There are two paths from start to end, one having a probability of success = 0.2 and the other has 0.5 * 0.5 = 0.25.</pre><p><strong>Example 2:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/189/0*h0AaPmqMoFzA3n5m.png" /></figure><pre><strong>Input:</strong> n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.3], start = 0, end = 2<br><strong>Output:</strong> 0.30000</pre><p><strong>Example 3:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/215/0*weUt-BHnGLO8ItfN.png" /></figure><pre><strong>Input:</strong> n = 3, edges = [[0,1]], succProb = [0.5], start = 0, end = 2<br><strong>Output:</strong> 0.00000<br><strong>Explanation:</strong> There is no path between 0 and 2.</pre><p><strong>Constraints:</strong></p><ul><li>2 &lt;= n &lt;= 10^4</li><li>0 &lt;= start, end &lt; n</li><li>start != end</li><li>0 &lt;= a, b &lt; n</li><li>a != b</li><li>0 &lt;= succProb.length == edges.length &lt;= 2*10^4</li><li>0 &lt;= succProb[i] &lt;= 1</li><li>There is at most one edge between every two nodes.</li></ul><p>[Think]</p><ol><li>using Dijkstra algorithm to find the highest prob in the graph</li><li>create a class “state” to track each node best prob</li><li>create a hashmap to put all undirect edge into the map with prob</li><li>priority queue to show the highest prob in the front</li><li>start with the first node, if we meet the end node, we can return the best prob</li><li>Else, check the map with linked edge and check the new prob. If the new prob is lower than current best record, no need to update</li><li>if could not find the end node, return 0;</li></ol><p>[Java]</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5480aed10b1105e2d4bf766754ebe03a/href">https://medium.com/media/5480aed10b1105e2d4bf766754ebe03a/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5920969026ae" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode 1282. Group the People Given the Group Size They Belong To]]></title>
            <link>https://kchiang6.medium.com/leetcode-1282-group-the-people-given-the-group-size-they-belong-to-a47d03ac6bc5?source=rss-1626cdbee09------2</link>
            <guid isPermaLink="false">https://medium.com/p/a47d03ac6bc5</guid>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[leetcode-medium]]></category>
            <dc:creator><![CDATA[Gary Chiang]]></dc:creator>
            <pubDate>Fri, 21 Jan 2022 23:17:42 GMT</pubDate>
            <atom:updated>2022-01-21T23:17:42.890Z</atom:updated>
            <content:encoded><![CDATA[<p>[medium]</p><p><a href="https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/">Group the People Given the Group Size They Belong To - LeetCode</a></p><p>There are n people that are split into some unknown number of groups. Each person is labeled with a <strong>unique ID</strong> from 0 to n - 1.</p><p>You are given an integer array groupSizes, where groupSizes[i] is the size of the group that person i is in. For example, if groupSizes[1] = 3, then person 1 must be in a group of size 3.</p><p>Return <em>a list of groups such that each person </em><em>i is in a group of size </em><em>groupSizes[i]</em>.</p><p>Each person should appear in <strong>exactly one group</strong>, and every person must be in a group. If there are multiple answers, <strong>return any of them</strong>. It is <strong>guaranteed</strong> that there will be <strong>at least one</strong> valid solution for the given input.</p><p><strong>Example 1:</strong></p><pre><strong>Input:</strong> groupSizes = [3,3,3,3,3,1,3]<br><strong>Output:</strong> [[5],[0,1,2],[3,4,6]]<br><strong>Explanation:</strong> <br>The first group is [5]. The size is 1, and groupSizes[5] = 1.<br>The second group is [0,1,2]. The size is 3, and groupSizes[0] = groupSizes[1] = groupSizes[2] = 3.<br>The third group is [3,4,6]. The size is 3, and groupSizes[3] = groupSizes[4] = groupSizes[6] = 3.<br>Other possible solutions are [[2,1,6],[5],[0,4,3]] and [[5],[0,6,2],[4,3,1]].</pre><p><strong>Example 2:</strong></p><pre><strong>Input:</strong> groupSizes = [2,1,3,3,3,2]<br><strong>Output:</strong> [[1],[0,5],[2,3,4]]</pre><p><strong>Constraints:</strong></p><ul><li>groupSizes.length == n</li><li>1 &lt;= n &lt;= 500</li><li>1 &lt;= groupSizes[i] &lt;= n</li></ul><p>[Think]</p><ol><li>create a map, and put same group size people into the list</li><li>if list size match the group size number, add it into the result list, and remove the list from map</li><li>In this answer, it had at least one answer, so no need to worry if anyone didn’t join the group</li><li>We could know that after the end of loop, the map should be empty</li></ol><p>[Java]</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/112b4ff1de53f77a8faeb6ec5360acfa/href">https://medium.com/media/112b4ff1de53f77a8faeb6ec5360acfa/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a47d03ac6bc5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode 875. Koko Eating Bananas]]></title>
            <link>https://kchiang6.medium.com/leetcode-875-koko-eating-bananas-62632e84b42f?source=rss-1626cdbee09------2</link>
            <guid isPermaLink="false">https://medium.com/p/62632e84b42f</guid>
            <category><![CDATA[leetcode-medium]]></category>
            <category><![CDATA[leetcode]]></category>
            <dc:creator><![CDATA[Gary Chiang]]></dc:creator>
            <pubDate>Fri, 21 Jan 2022 00:14:10 GMT</pubDate>
            <atom:updated>2022-01-21T00:14:10.223Z</atom:updated>
            <content:encoded><![CDATA[<p>[medium]</p><p><a href="https://leetcode.com/problems/koko-eating-bananas/">Koko Eating Bananas - LeetCode</a></p><p>Koko loves to eat bananas. There are n piles of bananas, the ith pile has piles[i] bananas. The guards have gone and will come back in h hours.</p><p>Koko can decide her bananas-per-hour eating speed of k. Each hour, she chooses some pile of bananas and eats k bananas from that pile. If the pile has less than k bananas, she eats all of them instead and will not eat any more bananas during this hour.</p><p>Koko likes to eat slowly but still wants to finish eating all the bananas before the guards return.</p><p>Return <em>the minimum integer</em> k <em>such that she can eat all the bananas within</em> h <em>hours</em>.</p><p><strong>Example 1:</strong></p><pre><strong>Input:</strong> piles = [3,6,7,11], h = 8<br><strong>Output:</strong> 4</pre><p><strong>Example 2:</strong></p><pre><strong>Input:</strong> piles = [30,11,23,4,20], h = 5<br><strong>Output:</strong> 30</pre><p><strong>Example 3:</strong></p><pre><strong>Input:</strong> piles = [30,11,23,4,20], h = 6<br><strong>Output:</strong> 23</pre><p><strong>Constraints:</strong></p><ul><li>1 &lt;= piles.length &lt;= 104</li><li>piles.length &lt;= h &lt;= 109</li><li>1 &lt;= piles[i] &lt;= 109</li></ul><p>[Think]</p><ol><li>find the max pile and do the binary search</li><li>if monkey could eat all the banana, move the start to become mid+1; otherwise move end to become mid-1</li><li>start the begin point on 1 instead of 0, because at least need 1 hour to finish the pile</li></ol><p>[Java]</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/52bf783ec4bac76254d39caa2b27ecb2/href">https://medium.com/media/52bf783ec4bac76254d39caa2b27ecb2/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=62632e84b42f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode 142. Linked List Cycle II]]></title>
            <link>https://kchiang6.medium.com/leetcode-142-linked-list-cycle-ii-ffabbb29dd90?source=rss-1626cdbee09------2</link>
            <guid isPermaLink="false">https://medium.com/p/ffabbb29dd90</guid>
            <category><![CDATA[leetcode-medium]]></category>
            <category><![CDATA[leetcode]]></category>
            <dc:creator><![CDATA[Gary Chiang]]></dc:creator>
            <pubDate>Wed, 19 Jan 2022 23:51:15 GMT</pubDate>
            <atom:updated>2022-01-19T23:51:15.402Z</atom:updated>
            <content:encoded><![CDATA[<p>[medium]</p><p><a href="https://leetcode.com/problems/linked-list-cycle-ii/">Linked List Cycle II - LeetCode</a></p><p>Given the head of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em>null.</p><p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail&#39;s next pointer is connected to (<strong>0-indexed</strong>). It is -1 if there is no cycle. <strong>Note that</strong> pos <strong>is not passed as a parameter</strong>.</p><p><strong>Do not modify</strong> the linked list.</p><p><strong>Example 1:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/531/0*0f5i28zH1cpzv6sH.png" /></figure><pre><strong>Input:</strong> head = [3,2,0,-4], pos = 1<br><strong>Output:</strong> tail connects to node index 1<br><strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.</pre><p><strong>Example 2:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/201/0*1vUWyJgnQSYkRtol.png" /></figure><pre><strong>Input:</strong> head = [1,2], pos = 0<br><strong>Output:</strong> tail connects to node index 0<br><strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.</pre><p><strong>Example 3:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/65/0*f-wSIEilN2gkyA_X.png" /></figure><pre><strong>Input:</strong> head = [1], pos = -1<br><strong>Output:</strong> no cycle<br><strong>Explanation:</strong> There is no cycle in the linked list.</pre><p><strong>Constraints:</strong></p><ul><li>The number of the nodes in the list is in the range [0, 104].</li><li>-105 &lt;= Node.val &lt;= 105</li><li>pos is -1 or a <strong>valid index</strong> in the linked-list.</li></ul><p><strong>Follow up:</strong> Can you solve it using O(1) (i.e. constant) memory?</p><p>[Think]</p><ol><li>if you just want to check the ListNode had cycle, we can use two pointer, one move faster, another move slower. If they meet, it means the ListNode had a cycle.</li><li>We want to know where they first meet. If we could track every position we have been to, we can have the answer.</li><li>Trick part is we can start a new ListNode from the head, and move the slow pointer and new pointer together. The place they meet will be the answer.</li><li>below photo from <a href="https://leetcode.com/wmhlailenow">wmhlailenow</a> help me understand the trick.</li></ol><p>Faster pointer run a + b +c +b (total length = a + b + c)</p><p>Slower pointer run a + b</p><p>Faster pointer run 2 times faster than slower pointer; a + 2b + c = 2(a+b);</p><p>a = c</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/0*J2_ScXn3No0bZjw-.jpg" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/52443c7c5b7776fd2cdb00627615138b/href">https://medium.com/media/52443c7c5b7776fd2cdb00627615138b/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ffabbb29dd90" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode 290. Word Pattern]]></title>
            <link>https://kchiang6.medium.com/leetcode-290-word-pattern-3d39031768f4?source=rss-1626cdbee09------2</link>
            <guid isPermaLink="false">https://medium.com/p/3d39031768f4</guid>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[leetcode-easy]]></category>
            <dc:creator><![CDATA[Gary Chiang]]></dc:creator>
            <pubDate>Mon, 17 Jan 2022 17:38:36 GMT</pubDate>
            <atom:updated>2022-01-17T17:38:36.611Z</atom:updated>
            <content:encoded><![CDATA[<p>[easy]</p><p><a href="https://leetcode.com/problems/word-pattern/">Word Pattern - LeetCode</a></p><p>Given a pattern and a string s, find if s follows the same pattern.</p><p>Here <strong>follow</strong> means a full match, such that there is a bijection between a letter in pattern and a <strong>non-empty</strong> word in s.</p><p><strong>Example 1:</strong></p><pre><strong>Input:</strong> pattern = &quot;abba&quot;, s = &quot;dog cat cat dog&quot;<br><strong>Output:</strong> true</pre><p><strong>Example 2:</strong></p><pre><strong>Input:</strong> pattern = &quot;abba&quot;, s = &quot;dog cat cat fish&quot;<br><strong>Output:</strong> false</pre><p><strong>Example 3:</strong></p><pre><strong>Input:</strong> pattern = &quot;aaaa&quot;, s = &quot;dog cat cat dog&quot;<br><strong>Output:</strong> false</pre><p><strong>Constraints:</strong></p><ul><li>1 &lt;= pattern.length &lt;= 300</li><li>pattern contains only lower-case English letters.</li><li>1 &lt;= s.length &lt;= 3000</li><li>s contains only lowercase English letters and spaces &#39; &#39;.</li><li>s <strong>does not contain</strong> any leading or trailing spaces.</li><li>All the words in s are separated by a <strong>single space</strong>.</li></ul><p>[Think]</p><ol><li>to check the pattern match, I thought transfer string s to the pattern first. However, if pattern had same character, it will transfer to wrong result.</li><li>if the string had some new letter other than the pattern, it won’t show the different</li><li>Therefore, I use two transform function to change them into a new String. And compare the two string follow the same pattern or not.</li><li>Leetcode descustion had shorter version of the answer</li></ol><p>[Gary]</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bec17288f7ba7255c45d186907007674/href">https://medium.com/media/bec17288f7ba7255c45d186907007674/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3d39031768f4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode 8. String to Integer (atoi)]]></title>
            <link>https://kchiang6.medium.com/leetcode-8-string-to-integer-atoi-1096098ec095?source=rss-1626cdbee09------2</link>
            <guid isPermaLink="false">https://medium.com/p/1096098ec095</guid>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[leetcode-medium]]></category>
            <dc:creator><![CDATA[Gary Chiang]]></dc:creator>
            <pubDate>Fri, 14 Jan 2022 22:56:40 GMT</pubDate>
            <atom:updated>2022-01-14T22:56:40.505Z</atom:updated>
            <content:encoded><![CDATA[<p>[medium]</p><p><a href="https://leetcode.com/problems/string-to-integer-atoi/">String to Integer (atoi) - LeetCode</a></p><p>Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++&#39;s atoi function).</p><p>The algorithm for myAtoi(string s) is as follows:</p><ol><li>Read in and ignore any leading whitespace.</li><li>Check if the next character (if not already at the end of the string) is &#39;-&#39; or &#39;+&#39;. Read this character in if it is either. This determines if the final result is negative or positive respectively. Assume the result is positive if neither is present.</li><li>Read in next the characters until the next non-digit character or the end of the input is reached. The rest of the string is ignored.</li><li>Convert these digits into an integer (i.e. &quot;123&quot; -&gt; 123, &quot;0032&quot; -&gt; 32). If no digits were read, then the integer is 0. Change the sign as necessary (from step 2).</li><li>If the integer is out of the 32-bit signed integer range [-231, 231 - 1], then clamp the integer so that it remains in the range. Specifically, integers less than -231 should be clamped to -231, and integers greater than 231 - 1 should be clamped to 231 - 1.</li><li>Return the integer as the final result.</li></ol><p><strong>Note:</strong></p><ul><li>Only the space character &#39; &#39; is considered a whitespace character.</li><li><strong>Do not ignore</strong> any characters other than the leading whitespace or the rest of the string after the digits.</li></ul><p><strong>Example 1:</strong></p><pre><strong>Input:</strong> s = &quot;42&quot;<br><strong>Output:</strong> 42<br><strong>Explanation:</strong> The underlined characters are what is read in, the caret is the current reader position.<br>Step 1: &quot;42&quot; (no characters read because there is no leading whitespace)<br>         ^<br>Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;)<br>         ^<br>Step 3: &quot;42&quot; (&quot;42&quot; is read in)<br>           ^<br>The parsed integer is 42.<br>Since 42 is in the range [-231, 231 - 1], the final result is 42.</pre><p><strong>Example 2:</strong></p><pre><strong>Input:</strong> s = &quot;   -42&quot;<br><strong>Output:</strong> -42<br><strong>Explanation:</strong><br>Step 1: &quot;   -42&quot; (leading whitespace is read and ignored)<br>            ^<br>Step 2: &quot;   -42&quot; (&#39;-&#39; is read, so the result should be negative)<br>             ^<br>Step 3: &quot;   -42&quot; (&quot;42&quot; is read in)<br>               ^<br>The parsed integer is -42.<br>Since -42 is in the range [-231, 231 - 1], the final result is -42.</pre><p><strong>Example 3:</strong></p><pre><strong>Input:</strong> s = &quot;4193 with words&quot;<br><strong>Output:</strong> 4193<br><strong>Explanation:</strong><br>Step 1: &quot;4193 with words&quot; (no characters read because there is no leading whitespace)<br>         ^<br>Step 2: &quot;4193 with words&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;)<br>         ^<br>Step 3: &quot;4193 with words&quot; (&quot;4193&quot; is read in; reading stops because the next character is a non-digit)<br>             ^<br>The parsed integer is 4193.<br>Since 4193 is in the range [-231, 231 - 1], the final result is 4193.</pre><p><strong>Constraints:</strong></p><ul><li>0 &lt;= s.length &lt;= 200</li><li>s consists of English letters (lower-case and upper-case), digits (0-9), &#39; &#39;, &#39;+&#39;, &#39;-&#39;, and &#39;.&#39;.</li></ul><p>[Think]</p><ol><li>straight forward to track sign, digit and space. But got run time error in overflow cases</li><li>be careful for the overflow situation</li></ol><p>[Java]</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/19de7d29770fcc6c7e85fb45dee987bb/href">https://medium.com/media/19de7d29770fcc6c7e85fb45dee987bb/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1096098ec095" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode 452. Minimum Number of Arrows to Burst Balloons]]></title>
            <link>https://kchiang6.medium.com/leetcode-452-minimum-number-of-arrows-to-burst-balloons-a671dcc58d68?source=rss-1626cdbee09------2</link>
            <guid isPermaLink="false">https://medium.com/p/a671dcc58d68</guid>
            <category><![CDATA[leetcode-medium]]></category>
            <category><![CDATA[leetcode]]></category>
            <dc:creator><![CDATA[Gary Chiang]]></dc:creator>
            <pubDate>Fri, 14 Jan 2022 00:18:54 GMT</pubDate>
            <atom:updated>2022-01-14T00:18:54.464Z</atom:updated>
            <content:encoded><![CDATA[<p>[medium]</p><p><a href="https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/">Minimum Number of Arrows to Burst Balloons - LeetCode</a></p><p>There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array points where points[i] = [xstart, xend] denotes a balloon whose <strong>horizontal diameter</strong> stretches between xstart and xend. You do not know the exact y-coordinates of the balloons.</p><p>Arrows can be shot up <strong>directly vertically</strong> (in the positive y-direction) from different points along the x-axis. A balloon with xstart and xend is <strong>burst</strong> by an arrow shot at x if xstart &lt;= x &lt;= xend. There is <strong>no limit</strong> to the number of arrows that can be shot. A shot arrow keeps traveling up infinitely, bursting any balloons in its path.</p><p>Given the array points, return <em>the </em><strong><em>minimum</em></strong><em> number of arrows that must be shot to burst all balloons</em>.</p><p><strong>Example 1:</strong></p><pre><strong>Input:</strong> points = [[10,16],[2,8],[1,6],[7,12]]<br><strong>Output:</strong> 2<br><strong>Explanation:</strong> The balloons can be burst by 2 arrows:<br>- Shoot an arrow at x = 6, bursting the balloons [2,8] and [1,6].<br>- Shoot an arrow at x = 11, bursting the balloons [10,16] and [7,12].</pre><p><strong>Example 2:</strong></p><pre><strong>Input:</strong> points = [[1,2],[3,4],[5,6],[7,8]]<br><strong>Output:</strong> 4<br><strong>Explanation:</strong> One arrow needs to be shot for each balloon for a total of 4 arrows.</pre><p><strong>Example 3:</strong></p><pre><strong>Input:</strong> points = [[1,2],[2,3],[3,4],[4,5]]<br><strong>Output:</strong> 2<br><strong>Explanation:</strong> The balloons can be burst by 2 arrows:<br>- Shoot an arrow at x = 2, bursting the balloons [1,2] and [2,3].<br>- Shoot an arrow at x = 4, bursting the balloons [3,4] and [4,5].</pre><p><strong>Constraints:</strong></p><ul><li>1 &lt;= points.length &lt;= 105</li><li>points[i].length == 2</li><li>-231 &lt;= xstart &lt; xend &lt;= 231 - 1</li></ul><p>[Think]</p><ol><li>it’s like sliding window problem, and we can use greedy algorithm to solve</li><li>sort the array by the ending point, and loop the array</li><li>if next ballon starting point before previous ending point, we can shoot them together.</li><li>to avoid overflow, use Integer.compare(a[1], b[1]), instead of (a[1]-b[1])</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8927574af734b5b0e2116ab1cc660c7f/href">https://medium.com/media/8927574af734b5b0e2116ab1cc660c7f/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a671dcc58d68" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode 862. Shortest Subarray with Sum at Least K]]></title>
            <link>https://kchiang6.medium.com/leetcode-862-shortest-subarray-with-sum-at-least-k-9c145dbd782d?source=rss-1626cdbee09------2</link>
            <guid isPermaLink="false">https://medium.com/p/9c145dbd782d</guid>
            <category><![CDATA[leetcode-hard]]></category>
            <category><![CDATA[leetcode]]></category>
            <dc:creator><![CDATA[Gary Chiang]]></dc:creator>
            <pubDate>Wed, 12 Jan 2022 20:14:40 GMT</pubDate>
            <atom:updated>2022-01-12T20:14:40.543Z</atom:updated>
            <content:encoded><![CDATA[<p>[hard]</p><p><a href="https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/">LeetCode - The World&#39;s Leading Online Programming Learning Platform</a></p><p>Given an integer array nums and an integer k, return <em>the length of the shortest non-empty </em><strong><em>subarray</em></strong><em> of </em>nums<em> with a sum of at least </em>k. If there is no such <strong>subarray</strong>, return -1.</p><p>A <strong>subarray</strong> is a <strong>contiguous</strong> part of an array.</p><p><strong>Example 1:</strong></p><pre><strong>Input:</strong> nums = [1], k = 1<br><strong>Output:</strong> 1</pre><p><strong>Example 2:</strong></p><pre><strong>Input:</strong> nums = [1,2], k = 4<br><strong>Output:</strong> -1</pre><p><strong>Example 3:</strong></p><pre><strong>Input:</strong> nums = [2,-1,2], k = 3<br><strong>Output:</strong> 3</pre><p><strong>Constraints:</strong></p><ul><li>1 &lt;= nums.length &lt;= 105</li><li>-105 &lt;= nums[i] &lt;= 105</li><li>1 &lt;= k &lt;= 109</li></ul><p>[Gary-brute force-Java]</p><ol><li>create two loop and sum them together to find the shortest path.</li><li>TLE for the run time</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9c5928a0e753796540baefb936880488/href">https://medium.com/media/9c5928a0e753796540baefb936880488/href</a></iframe><p>[Using deque]</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c307a59f908407123c771f253aed5a6c/href">https://medium.com/media/c307a59f908407123c771f253aed5a6c/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9c145dbd782d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode 701. Insert into a Binary Search Tree]]></title>
            <link>https://kchiang6.medium.com/leetcode-701-insert-into-a-binary-search-tree-4dbb5a616e39?source=rss-1626cdbee09------2</link>
            <guid isPermaLink="false">https://medium.com/p/4dbb5a616e39</guid>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[leetcode-medium]]></category>
            <dc:creator><![CDATA[Gary Chiang]]></dc:creator>
            <pubDate>Wed, 12 Jan 2022 05:02:32 GMT</pubDate>
            <atom:updated>2022-01-12T05:02:32.966Z</atom:updated>
            <content:encoded><![CDATA[<p>[medium]</p><p><a href="https://leetcode.com/problems/insert-into-a-binary-search-tree/">Insert into a Binary Search Tree - LeetCode</a></p><p>You are given the root node of a binary search tree (BST) and a value to insert into the tree. Return <em>the root node of the BST after the insertion</em>. It is <strong>guaranteed</strong> that the new value does not exist in the original BST.</p><p><strong>Notice</strong> that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return <strong>any of them</strong>.</p><p><strong>Example 1:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/752/0*J-OFJDfJ4fRI48Nj.jpg" /></figure><pre><strong>Input:</strong> root = [4,2,7,1,3], val = 5<br><strong>Output:</strong> [4,2,7,1,3,5]<br><strong>Explanation:</strong> Another accepted tree is:</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/352/0*SSLqq8vF8Z2sf42O.jpg" /></figure><p><strong>Example 2:</strong></p><pre><strong>Input:</strong> root = [40,20,60,10,30,50,70], val = 25<br><strong>Output:</strong> [40,20,60,10,30,50,70,null,null,25]</pre><p><strong>Example 3:</strong></p><pre><strong>Input:</strong> root = [4,2,7,1,3,null,null,null,null,null,null], val = 5<br><strong>Output:</strong> [4,2,7,1,3,5]</pre><p><strong>Constraints:</strong></p><ul><li>The number of nodes in the tree will be in the range [0, 104].</li><li>-108 &lt;= Node.val &lt;= 108</li><li>All the values Node.val are <strong>unique</strong>.</li><li>-108 &lt;= val &lt;= 108</li><li>It’s <strong>guaranteed</strong> that val does not exist in the original BST.</li></ul><p>[Think]</p><ol><li>it’s a BST, if the value large than current node -&gt; right side of tree; smaller than current node -&gt; left side of tree</li><li>create a current node to be a pointer, and return the root at the end of the answer</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/744dd17811ffca1b22bdb61e6940a9ea/href">https://medium.com/media/744dd17811ffca1b22bdb61e6940a9ea/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4dbb5a616e39" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>