<?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 Felix Lee on Medium]]></title>
        <description><![CDATA[Stories by Felix Lee on Medium]]></description>
        <link>https://medium.com/@leeyoongti?source=rss-a923cb741aca------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*o0FORq3-RR182Bn8GvjUsA.jpeg</url>
            <title>Stories by Felix Lee on Medium</title>
            <link>https://medium.com/@leeyoongti?source=rss-a923cb741aca------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 09:54:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@leeyoongti/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[Minimum Meeting Rooms]]></title>
            <link>https://leeyoongti.medium.com/minimum-meeting-rooms-242577c92ae2?source=rss-a923cb741aca------2</link>
            <guid isPermaLink="false">https://medium.com/p/242577c92ae2</guid>
            <category><![CDATA[heap]]></category>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[Felix Lee]]></dc:creator>
            <pubDate>Sun, 02 Oct 2022 00:01:45 GMT</pubDate>
            <atom:updated>2022-10-02T00:01:45.828Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UUjvI-DXq9OFJj-FwxMunA.jpeg" /></figure><h3>Problem Statement</h3><p>Given a list of intervals representing the start and end time of ’N’ meetings, find the <strong>minimum number of rooms</strong> required to <strong>hold all the meetings</strong>.</p><p><strong>Example 1:</strong></p><pre>Meetings: [[1,4], [2,5], [7,9]]<br>Output: 2<br>Explanation: Since [1,4] and [2,5] overlap, we need two rooms to hold these two meetings. [7,9] can <br>occur in any of the two rooms later.</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IGVtaY-Y7SBJEG0722vOTA.png" /></figure><p><strong>Example 2:</strong></p><pre>Meetings: [[1,4], [2,3], [3,6]]<br>Output:2<br>Explanation: Since [1,4] overlaps with the other two meetings [2,3] and [3,6], we need two rooms to <br>hold all the meetings.</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AzOVXjL61T9md9x8hvhP_w.png" /></figure><p><strong>Example 3:</strong></p><pre>Meetings: [[1,6], [2,3], [3,6]]<br>Output:2</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DCqE-P4ILoJL3kdRlM4zbQ.png" /></figure><h3>Walkthrough</h3><p>Our task here is basically to look for the maximum number of meetings that are currently running in parallel. If a meeting starts before previous meetings end, it means we need more meeting rooms to hold all the meeting.</p><h4>Using a min heap to keep track of meeting rooms</h4><p>We can use a min heap to keep track of the meeting rooms, where the meeting with the smallest end time should be at the top of the heap. Before we push a new meeting to the heap, we check whether there is any previous meeting that has ended. If there is any, we want to remove them from the heap as the room has been freed up.</p><p>Note: Remember to sort the meeting based on start time first for our algorithm to work properly.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/65fc6c00c1c36bdf18bf30ea056d0560/href">https://medium.com/media/65fc6c00c1c36bdf18bf30ea056d0560/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=242577c92ae2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Update Sitemap / File In NextJS Without Rebuilding Project (Dynamic Update)]]></title>
            <link>https://leeyoongti.medium.com/update-sitemap-file-in-nextjs-without-rebuilding-project-dynamic-update-27477b1d9279?source=rss-a923cb741aca------2</link>
            <guid isPermaLink="false">https://medium.com/p/27477b1d9279</guid>
            <category><![CDATA[server-side-rendering]]></category>
            <category><![CDATA[updates]]></category>
            <category><![CDATA[sitemap]]></category>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[remote]]></category>
            <dc:creator><![CDATA[Felix Lee]]></dc:creator>
            <pubDate>Sun, 18 Sep 2022 12:02:11 GMT</pubDate>
            <atom:updated>2022-09-18T12:02:11.818Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*evWiCwVffl0RCeaqayMFxw.png" /></figure><p>Recently I receive a request to update the <a href="https://backlinko.com/hub/seo/sitemaps">sitemap</a> of a website periodically so that the crawler can get the latest content. A sitemap typically is located at the root of the domain (i.e. yourdomain.com/sitemap.xml). In another word, the sitemap.xml is stored in the public folder of the NextJS project.</p><p>Let’s us take a look at the <a href="https://nextjs.org/docs/basic-features/static-file-serving">NextJS documentation</a></p><blockquote><strong>Note</strong>: Only assets that are in the public directory at <a href="https://nextjs.org/docs/api-reference/cli#build">build time</a> will be served by Next.js. Files added at runtime won&#39;t be available. We recommend using a third party service like <a href="https://aws.amazon.com/s3/">AWS S3</a> for persistent file storage.</blockquote><p>Does this mean we have to rebuild and deploy our project every time just to update a file in NextJS? Nope! There’s one way we can do it — server side rendering.</p><h4>TLDR, show me the code.</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/021d0197bc892dbb7b8848e1cb032803/href">https://medium.com/media/021d0197bc892dbb7b8848e1cb032803/href</a></iframe><p>Basically, all you have to do is create a folder named sitemap.xml under the <em>pages</em> folder and serve your sitemap.xml “page”, like so pages/sitemap.xml/index.tsx .</p><p>Remember to remove the sitemap.xml in the public folder if you have any. NextJS will prompt an error if you have a static file and a page with the same name.</p><p>For this example, I host the sitemap.xml file on S3, feel free to use any storage service you prefer. So if we want to update sitemap.xml, we don’t have to build and deploy the project, we can just update the file in the S3 bucket!</p><p>You can check the full source code <a href="https://github.com/yoongti/nextjs-dynamic-sitemap">here</a></p><p>Thanks for reading! You can find me on <a href="https://www.linkedin.com/in/yoongtilee/">LinkedIn</a> or simply down me a message on Medium.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=27477b1d9279" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Start of LinkedList Cycle]]></title>
            <link>https://leeyoongti.medium.com/start-of-linkedlist-cycle-e4886a7c4353?source=rss-a923cb741aca------2</link>
            <guid isPermaLink="false">https://medium.com/p/e4886a7c4353</guid>
            <category><![CDATA[linked-lists]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[cycle]]></category>
            <dc:creator><![CDATA[Felix Lee]]></dc:creator>
            <pubDate>Sun, 04 Sep 2022 12:01:28 GMT</pubDate>
            <atom:updated>2022-09-04T12:01:28.014Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*fXAM4giWE6nhzbMNsccvuA.jpeg" /></figure><h3>Problem Statement</h3><p>Given the head of a Singly LinkedList that contains a cycle, write a function to find the starting node of the cycle.</p><h3>Key Ideas</h3><ol><li>Point 3 pointers to the head of the linked list — Slow pointer, fast pointer, and entry pointer.</li><li>Move slow pointer 1 node at a time while moving fast pointer 2 nodes at a time</li><li>At some point, both pointers will meet. At this moment, move entry pointer and slow pointer 1 node at a time.</li><li>The node that entry pointer and slow pointer meet would be the cycle.</li></ol><p>Look at the example below for a visualisation of the solution.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/482/1*4_qbLIlfvNLQy7m-ulwdGA.png" /></figure><h4>Why? How? What??</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/532/1*8lS5wjBpVsRKv5UutIEt4A.png" /></figure><p>Suppose that:</p><ul><li>a1 is the length from head to start of cycle</li><li>a2 is the length from start of cycle to where slow and fast pointers meet</li><li>a3 is the remaining length — length from meeting point to start of cycle</li><li>So the distance that slow pointer travels is a1 + a2</li><li>We know that fast pointer moves twice as fast compared to slow pointer. So the distance travels is 2 * (a1 + a2) .</li><li>We also know that fast pointer would have travelled through a3 and come back to meet the slow pointer due to the cycle. So we can write our formula like so: a1 + a2 + a3 + a2 . There are 2 a2 because fast pointer travels it twice. Remember that a2 is the length from start of cycle to where slow and fast pointers meet</li><li>So 2 * (a1 + a2) = a1 + a2 + a3 + a2</li><li>(a1) + a1 + (a2) + (a2) = (a1) + (a2) + a3 + (a2) , cancel out the variable in bracket and we get</li><li>a1 = a3</li></ul><p>Now that we are equipped with this knowledge, we can write the code!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/80114168cf1f6ac256763f38228bed6a/href">https://medium.com/media/80114168cf1f6ac256763f38228bed6a/href</a></iframe><h3>Time and Space Complexity</h3><h4>Time Complexity</h4><p>The algorithm takes <em>O(N)</em> time because it traverse the linked list to find the cycle.</p><h4>Space Complexity</h4><p>It runs in constant space <em>O(1)</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e4886a7c4353" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Shortest Unsorted Continuous Subarray in O(N) Time]]></title>
            <link>https://leeyoongti.medium.com/shortest-unsorted-continuous-subarray-in-o-n-time-a5e306d506d?source=rss-a923cb741aca------2</link>
            <guid isPermaLink="false">https://medium.com/p/a5e306d506d</guid>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[sorting]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[arrays]]></category>
            <category><![CDATA[sliding-windows]]></category>
            <dc:creator><![CDATA[Felix Lee]]></dc:creator>
            <pubDate>Sun, 21 Aug 2022 12:02:49 GMT</pubDate>
            <atom:updated>2022-08-21T12:02:49.703Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/430/1*nCEMRg7GxD466aYMz6qDQQ.jpeg" /></figure><h3>Problem Statement</h3><p>Given an integer array nums, you need to find one <strong>continuous subarray</strong> that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order.</p><p>Return <em>the shortest such subarray and output its length</em>.</p><h3>Key Ideas</h3><p>Consider the case nums = [1, 5, 2, -1, 7]</p><ol><li>Find the first element that is out of order from the left. 5 is the one out of order because it is larger than 2. The number should keep increasing as we move to the right.</li><li>Find the last element that is out of order from the right. -1 is the one out of order because it is smaller than 2 . The number should keep decreasing as we move to the left.</li><li>After Step 1 and 2, we will get the <strong>initial window/subarray</strong> ( [5, 2, -1] ). However, we will not get a sorted array by just sorting this 3 elements. We need to include the first element, 1 as well. [-1, 1, 2, 5, 7] is the correct sorted array.</li><li>So we need to expand our window to the left to include more elements if our <strong>initial subarray contains a number that is smaller than the number outside the window on the left</strong>. For example, we need to include 1 at index 0 because it is larger than -1 which exists in our initial window.</li><li>Do the same for the right side. If the initial subarray contains a number that is larger than the number outside the window on the right.</li><li>For this example, [1, 5, 2, -1] =&gt; Length = 4 is the correct answer to our problem</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d03db829a4eebae31c71403c62b812ef/href">https://medium.com/media/d03db829a4eebae31c71403c62b812ef/href</a></iframe><h3>Time and Space Complexity</h3><h4>Time Complexity</h4><p>We look through the array only once, so the algorithm is <em>O(N).</em></p><h4>Space Complexity</h4><p>The algorithm runs in constant space <em>O(1).</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a5e306d506d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Subarrays Product Less than a Target]]></title>
            <link>https://leeyoongti.medium.com/subarrays-product-less-than-a-target-87f9ffbef320?source=rss-a923cb741aca------2</link>
            <guid isPermaLink="false">https://medium.com/p/87f9ffbef320</guid>
            <category><![CDATA[leetcode-medium]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[sliding-windows]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[arrays]]></category>
            <dc:creator><![CDATA[Felix Lee]]></dc:creator>
            <pubDate>Sun, 07 Aug 2022 12:03:23 GMT</pubDate>
            <atom:updated>2022-08-07T12:03:23.090Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*MlnuFH0iqKMMnqWLG0pnsg.jpeg" /></figure><h3>Problem Statement</h3><p>Given an array with positive numbers and a positive target number, find all of its contiguous subarrays whose <strong>product is less than the target number</strong>.</p><h3>Key Ideas</h3><ol><li>Keep moving a pointer(right) from the left to the right while keeping a pointer(left) at the left. (sliding window — we expand the window by moving the right pointer)</li><li>Add the current number to product .</li><li>If at any point the product is larger or equal to the target, move the left pointer to the right. (sliding window — we are shrinking the window by moving the left pointer)</li><li>Add the subarray (from left pointer to right pointer) to the result.</li><li>If the product of the subarray is less than target , then all product of the subarray elements are less than target as well.</li><li>Consider the case [2,3,4], target = 50 , if 2 * 3 * 4 &lt; 50, then 2, 2 * 3, 3, 3 * 4 are all less than target . Add all of them to result.</li></ol><h3>Code</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a86c7d39330e053a578a685888e37e5c/href">https://medium.com/media/a86c7d39330e053a578a685888e37e5c/href</a></iframe><h3>Time and Space Complexity</h3><h4>Time Complexity</h4><p>We have an outer loop and inner loop, the worst case is that we have to iterate the entire array every time in the inner loop, so it will be <em>O(N²). </em>Notice we create a new list at line 29</p><pre>result.push([...list])</pre><p>In the worst case, it takes O(N). Thus, our algorithm takes <em>O(N³) </em>in time complexity.</p><h4>Space Complexity</h4><p>It takes <em>O(N) </em>space for our list.</p><p>How about the result list?</p><p>Finding all contiguous subarrays of an array is basically finding the number of ways to choose two indices, i and j, in the array such that i &lt;= j.</p><p>Let’s consider the array has n length</p><ul><li>When i=0 , j can be anything from 0 to n-1 =&gt; n number of choices</li><li>When i=1 , j can be anything from 1 to n-1 =&gt; n-1 number of choices</li><li>When i=2 , j can be anything from 2 to n-2 =&gt; n-2 number of choices</li><li>….</li><li>When i = n-1, j can only have only 1 choice</li></ul><p>If we combine all the choices of j , it looks something like this</p><pre>n + (n-1) + (n-2) + (n-3) ... + 3 + 2 + 1</pre><p>Which is <em>n</em>∗(<em>n</em>+1)/2 =&gt; <em>O(N²)</em></p><p>So in total, our algorithm takes O(N³) space.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=87f9ffbef320" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Triplet Sum Close to Target in O(N²) Time]]></title>
            <link>https://leeyoongti.medium.com/triplet-sum-close-to-target-in-o-n%C2%B2-time-ea10085b8f81?source=rss-a923cb741aca------2</link>
            <guid isPermaLink="false">https://medium.com/p/ea10085b8f81</guid>
            <category><![CDATA[sum]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[triplets]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[pointers]]></category>
            <dc:creator><![CDATA[Felix Lee]]></dc:creator>
            <pubDate>Sun, 24 Jul 2022 12:02:58 GMT</pubDate>
            <atom:updated>2022-07-24T12:02:58.635Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*wOTePQ1oi5hxQmWLUPEyrQ.jpeg" /><figcaption><a href="https://quotesgram.com/img/funny-triplet-quotes/7074867/">https://quotesgram.com/img/funny-triplet-quotes/7074867/</a></figcaption></figure><h3>Problem Statement</h3><p>Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target.</p><p>Return <em>the sum of the three integers</em>.</p><h3>Key Ideas</h3><ol><li>Use 3 pointers to point to the current element, next(left) element, and the last(right) element.</li><li>Sum the 3 elements.</li><li>If the difference between the current sum and target is smaller than the difference between our result and the target, set our result to the current sum.</li><li>If current sum is larger than target, move the last(right) index to the left to get a smaller sum. (Remember target — sum = difference , so if larger sum = larger difference)</li><li>Otherwise, move the next(left) index to the right to get a larger sum.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/67af330730c640539089da6c0a9c7f51/href">https://medium.com/media/67af330730c640539089da6c0a9c7f51/href</a></iframe><h3>Time and Space Complexity</h3><h4>Time Complexity</h4><p>We sort the array initially, so it’s <em>O(N * log(N))</em>. We have a loop nested inside a loop so it is <em>O(N²)</em>. Overall the function takes <em>O(N * log(N) + N²)</em> which is asymptotically equivalent to <em>O(N²)</em>.</p><h4>Space Complexity</h4><p>We sort the array at the beginning, the sorting takes <em>O(N)</em> space.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ea10085b8f81" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Words Concatenation]]></title>
            <link>https://leeyoongti.medium.com/words-concatenation-2bbbaa338d91?source=rss-a923cb741aca------2</link>
            <guid isPermaLink="false">https://medium.com/p/2bbbaa338d91</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[hashmap]]></category>
            <category><![CDATA[concatenation]]></category>
            <category><![CDATA[loop]]></category>
            <category><![CDATA[words]]></category>
            <dc:creator><![CDATA[Felix Lee]]></dc:creator>
            <pubDate>Sat, 16 Jul 2022 18:02:27 GMT</pubDate>
            <atom:updated>2022-07-16T18:02:27.263Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*tE4eOuU-aV0PFpXIz0qQfQ.jpeg" /></figure><h3>Problem Statement</h3><p>Given a string s and a list of words words of <strong>the same length</strong>. Return all starting indices of substring(s) that is a concatenation of each word <strong>exactly once</strong>, <strong>in any order</strong>, and <strong>without any intervening characters</strong>.</p><pre>Example 1<br>Input: String=&quot;catdogcat&quot;, Words=[&quot;cat&quot;, &quot;dog&quot;]<br>Output: [0, 3]<br>Explanation: The two substring containing both the words are &quot;catdog&quot; starting at index 0 &amp; &quot;dogcat&quot; starting at index 3.</pre><pre>Example 2<br>Input: String=&quot;acatcatdogcat&quot;, Words=[&quot;cat&quot;, &quot;dog&quot;]<br>Output: [4, 7]<br>Explanation: The two substring containing both the words are &quot;catdog&quot; starting at index 1 &amp; &quot;dogcat&quot; starting at index 4.</pre><pre>Example 3<br>Input: String=&quot;catadogcatdogdog&quot;, Words=[&quot;cat&quot;, &quot;dog&quot;]<br>Output: [4, 7]<br>Explanation: The substring containing both the words are &quot;dogcat&quot; starting at index 4 and &quot;catdog&quot; at index 7.</pre><h4>Key Rules</h4><ol><li>The list of words are of the same length. We can use this information(<strong>word length</strong>) to get each word from string s.</li><li>The concatenation of each word should occurs exactly once. So if a word occurs more time in string s than it is in words, we can conclude the current substring is not what we are looking for. (i.e. In Example 2, “cat” at index 1 is not a valid case because “cat” shows up again at index 4)</li><li>There should not be any intervening characters. (i.e. In Example 3, there is an “a” at index 3, so “catadog” is not a valid case)</li></ol><p>The basic idea here is to:</p><ol><li>Loop string s from index 0 using a pointer i</li><li>Extract word(substring) from s</li><li>Check whether the current word can be found in the list of words (Remember not to violate the problem’s key rules).</li><li>If all the words in the list are found (word count is met), we can add the current starting index (pointer i) to our result.</li></ol><h3>Example 1 Walkthrough</h3><p>Let’s walk through Example 1 and see how we can solve this problem. Look at the figure below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/391/1*b-cBIjdcahncLOpzm2H0Vw.png" /></figure><ol><li>The input string s is “catdogcat”.</li><li>The word length is 3. This will be helpful when we loop through s to extract word from it.</li><li>The word count is 2 . Remember that we are looking for the concatenation of words from the list <strong>exactly once.</strong></li><li>We are given a list of words. We can store the words in a hash map for easy reference. Let’s call it wordMap .</li></ol><p>The code will look something like this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5e16ec99f083c37e6ab0d350704e2edf/href">https://medium.com/media/5e16ec99f083c37e6ab0d350704e2edf/href</a></iframe><p>Remember that there is nothing stopping the list of words having duplicate words like so [&quot;cat&quot;,&quot;dog&quot;,&quot;cat&quot;] , so we also store the count in wordMap</p><p>Here is a simple flow how the first loop looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/301/1*21ib9nbzjrLVsf1OM_LHTw.png" /></figure><p>Let’s go through step by step.</p><p>Loop through string s as we need to get each word from s , starting from index 0, but when do we stop? Do we need to loop until the last index?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/156/1*1In51HtP3vQBl8w06yTlVw.png" /></figure><p>Imagine we are at index 6 and we get the word “cat”. Is there any way we can get the concatenation of entire list of words ([“cat&quot;,&quot;dog&quot;])? Not really. At index 6, we simply do not have enough word to get a valid case. We need 6 characters to make a valid case. We can get the length of concatenation of all the words with wordCount * wordLength .</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a169f9a22ab42d5f6e27eda71823a420/href">https://medium.com/media/a169f9a22ab42d5f6e27eda71823a420/href</a></iframe><ol><li>Inner loop is responsible for getting the word from s , starting at index i</li><li>We use foundMap to store the occurrence of each word later.</li></ol><p>Let’s complete the rest of the code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4b0b60256640c9f0db229b0124be2dbe/href">https://medium.com/media/4b0b60256640c9f0db229b0124be2dbe/href</a></iframe><h3>Time and Space Complexity</h3><h4>Time Complexity</h4><p>The time complexity is O(N * M * L) where N is the length of the input string (the outer loop), M is the number of words in the list, and L is the length of each word.</p><h4>Space Complexity</h4><p>We are storing the words in 2 HashMaps. Also the result list may be as long as the input string at the worst case. So the space complexity is O(M + N) where M is words in the HashMap and N is the words in the result list.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2bbbaa338d91" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Power Set]]></title>
            <link>https://leeyoongti.medium.com/power-set-1658cc755e52?source=rss-a923cb741aca------2</link>
            <guid isPermaLink="false">https://medium.com/p/1658cc755e52</guid>
            <category><![CDATA[set]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[subsets]]></category>
            <category><![CDATA[powerset]]></category>
            <dc:creator><![CDATA[Felix Lee]]></dc:creator>
            <pubDate>Sun, 03 Jul 2022 13:02:57 GMT</pubDate>
            <atom:updated>2022-07-03T13:02:57.375Z</atom:updated>
            <content:encoded><![CDATA[<p>A power set is a set of all subsets of a set, including empty set and the set itself.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/690/1*W5xzcYYYm6eiWY3ErAYmxA.jpeg" /></figure><p>Let’s look at a few examples to understand what that statement really means.</p><p>We are at the grocery store, we want to buy apple or banana:</p><p>{apple, banana}</p><ul><li>Empty -&gt; {}</li><li>Single item -&gt; {apple}, {banana}</li><li>Two items (everything) -&gt; {apple, banana}</li></ul><p>We get 4 subsets.</p><p>How about 3 items? Let’s add carrot to the item list.</p><p>{apple, banana, carrot}</p><ul><li>Empty (We don’t feel like buying anything)-&gt; {}</li><li>Single item -&gt; {apple}, {banana}, {carrot}</li><li>Two items -&gt; {apple, banana}, {apple, carrot}, {banana, carrot}</li><li>Three items (Give us everything!) -&gt; {apple, banana, carrot}</li></ul><p>If we have 3 items, we will get 8 subsets.</p><p>If we do it with 1 item, we will get 2 subsets — empty and 1 item(everything) subset. If we do it with 4 items, we will get 16 subsets!</p><p>Notice how each time we add 1 item to the set, we will get 2x of subsets that we can get. In another word, the power set of a set is 2 to the power of n, where n is the number of item in the set.</p><p>0 item -&gt; 1 subset</p><p>1 item -&gt; 2 subsets</p><p>2 items -&gt; 4 subsets</p><p>3 items -&gt; 8 subsets</p><p>4 items -&gt; 16 subsets</p><p>5 items -&gt; 32 subsets</p><p>If you are thinking, “hey, it’s binary!”, You are absolutely correct!</p><h3>Binary</h3><p>Here is a table to see how our items (a = apple, b = banana, c = carrot) can be mapped to a binary representation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*d3MBjDTsSbtdCzDKeIlsWA.png" /></figure><p>How do we solve this with code? Let’s think just for a bit.</p><p>First, we know that the number of subsets is 2^n. So let’s do that first.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d79277c81e6b8ab35b4614c2d03ad686/href">https://medium.com/media/d79277c81e6b8ab35b4614c2d03ad686/href</a></iframe><p>Next, we just have to put in the item to the subset, but how? The hint is in the table above! We are looping from 0 to 2^n. If we have 3 items, we will loop from 0 to 7.</p><p>All we have to do is to check whether it is a 1(set bit) or 0(unset bit) at that bit for that number. If it is a 1, we add that item to the subset.</p><p>We can use an AND operator to help us for this. Basically, the bitwise AND operator (&amp;) returns a 1 in each bit position if the corresponding bits of both binary numbers are 1s.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*TeX0fJ6D4JJTJ3KicJLSkQ.png" /></figure><p>We get all 0 for our subset, we won’t be adding any item to the subset, thus we get an empty subset ({})</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*PivkUWvCze7xLI29ltFOvg.png" /></figure><p>For the first loop of the inner loop, we get 1 for bit 2⁰, so we will add that item to the subset</p><p>You can continue to do this exercise and see everything falls into place.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dae686ee69eda75dbb763d3af8533856/href">https://medium.com/media/dae686ee69eda75dbb763d3af8533856/href</a></iframe><h3>Space and Time Complexity</h3><h4>Space complexity</h4><p>We are storing 2^n items in the area, so the space complexity would be O(2^n)</p><h4>Time complexity</h4><p>The outer loop iterates 2^n times and the inner loop iterates n times, so the time complexity is O(n*(2^n))</p><p>Thanks for reading! You can also find me on LinkedIn (<a href="https://www.linkedin.com/in/yoongtilee/">https://www.linkedin.com/in/yoongtilee/</a>) if you want to connect!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1658cc755e52" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Find Odd Occurring Element Using XOR]]></title>
            <link>https://leeyoongti.medium.com/find-odd-occurring-element-using-xor-c2f6d1d8310e?source=rss-a923cb741aca------2</link>
            <guid isPermaLink="false">https://medium.com/p/c2f6d1d8310e</guid>
            <category><![CDATA[xor]]></category>
            <category><![CDATA[logic]]></category>
            <category><![CDATA[boolean-logic]]></category>
            <category><![CDATA[odd]]></category>
            <category><![CDATA[arrays]]></category>
            <dc:creator><![CDATA[Felix Lee]]></dc:creator>
            <pubDate>Sun, 19 Jun 2022 13:03:09 GMT</pubDate>
            <atom:updated>2022-06-19T13:03:09.804Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bbx28GREV5ksZyYn6tU49Q.png" /><figcaption><a href="https://www.reddit.com/r/ProgrammerHumor/comments/jib9bx/trick_or_treat/">Source: Reddit</a></figcaption></figure><p>This is just one of the cool things that you can do with XOR.</p><h3>Problem Statement</h3><p>Given an array, find number that occurs odd number of times.</p><pre>Input: [1,2,2,3,3,1,5,1,5,5,5]<br>Output: 1</pre><h3>Understanding XOR (eXclusive OR)</h3><p>The logic is simple. If the bits are the same, the result is 0. If the bits are different, the result is 1.</p><p>You can use this <a href="https://xor.pw/#">calculator</a> to try out some numbers yourself.</p><p>Let’s look at a few examples. Take the number 10 and 9.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/478/1*2qCl5cApuNhDBvNGLS4DBQ.png" /></figure><p>The XOR of 10 and 9 is 3 (binary representation of 011).</p><p>What if we take the same number and do an XOR?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/474/1*zw-vrOJgB_51b0l7hydDFQ.png" /></figure><p>We will get 0 as a result!</p><p>Now what if we do an XOR between any number(i.e. 10) and a 0?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/476/1*3NIBcCNQfVUf-oC3plVCDA.png" /></figure><p>We get back the same number that we put in.</p><p>So, it means if we do an XOR on two same numbers(i.e. that number occurs even times), we will get a 0 . If we do an XOR between any number and 0, it would not change that number in any way.</p><p>To conclude:</p><pre>// Remember b ^ b cancel each others out<br>a = a ^ (b ^ b)</pre><pre>// b ^ 0 will not change b<br>b = b ^ 0</pre><h3>Solution: Find odd occurring element using XOR</h3><p>Assuming there is only one number that occurs odd number of times, it means all other numbers occur even number of times. If we do an XOR on the number that occurs even number of times, they will cancel out each others!</p><p>The number that will be left would be the one that occurs odd number of times.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7ef99ed7aa142346a76b704c38a74eda/href">https://medium.com/media/7ef99ed7aa142346a76b704c38a74eda/href</a></iframe><h3>Space and time complexity</h3><p>Space complexity is O(1) as we use constant space.</p><p>Time complexity is O(n) as we loop through the entire input array once.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c2f6d1d8310e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Counting Set Bits using Brian Kernighan’s Algorithm]]></title>
            <link>https://leeyoongti.medium.com/counting-set-bits-using-brian-kernighans-algorithm-14d5699244ae?source=rss-a923cb741aca------2</link>
            <guid isPermaLink="false">https://medium.com/p/14d5699244ae</guid>
            <category><![CDATA[bit]]></category>
            <category><![CDATA[count]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[brian-kernighan]]></category>
            <dc:creator><![CDATA[Felix Lee]]></dc:creator>
            <pubDate>Sun, 05 Jun 2022 13:02:36 GMT</pubDate>
            <atom:updated>2022-06-05T13:02:36.798Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*OhuR-s0JBBDuKueS1hP6Ew.jpeg" /></figure><p>In short, Brian Kernighan’s Algorithm is used to count the number of 1 (also known as <em>set bits</em>) in a binary number. The main concept here is when we subtract one from any number, all the bits after the least significant/rightmost <em>set bits </em>will be inverted!</p><p>Let’s look at a binary representation of 1 to 10.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/478/1*siSV1z-yqZXlQQrQEHRXaw.png" /></figure><p>Notice as we go down the list of number, the bits, including the rightmost 1 are flipped, highlighted in orange.</p><p>Now, let’s imagine what will happen when you do a bitwise AND(&amp;) operator on a number(i.e. 10) and the number before it(i.e. 9)? What is a bitwise AND operator you say?</p><h3>Bitwise AND operator</h3><p>Basically, the bitwise AND operator (&amp;) returns a 1 in each bit position if the corresponding bits of both binary numbers are 1s.</p><p>Let’s take the number 10 and the number 9 in their binary forms as example</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/478/1*kUMk7hdW9Z9pw1_2xMJjRA.png" /></figure><p>You can see that only the 1 in 2³ column remains as both the number 10 and 9 have 1 in their 2³.</p><p>How about the number 8 and 7?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/478/1*qklwNXnG5YbOqCF60sNpww.png" /></figure><p>All the 1 are gone! The bits in both number have to be 1 for AND operator to be 1 .</p><p>Now that we understands the concept, we are ready to tackle the problem. You can use this <a href="https://www.rapidtables.com/convert/number/decimal-to-binary.html">decimal-to-binary converter</a> to better visualise a number in its binary representation.</p><h3>Problem Statement: Count the number of bits</h3><p>Write a program to count the number of 1s(<em>set bits</em>) in the binary representation of an integer.</p><pre>Input: 10<br>Output: 2<br>Binary representation of 10 is 1010 -&gt; it has 2 set bits</pre><pre>Input: 32<br>Output: 1<br>Binary representation of 32 is 100000 -&gt; it has 1 set bit</pre><h3>Solution: Brian Kernighan’s Algorithm</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/183112d0e70aeab7f58bd0a37dcb84b6/href">https://medium.com/media/183112d0e70aeab7f58bd0a37dcb84b6/href</a></iframe><p>Let’s do a visual walkthrough using the number 10</p><p>On the first loop, we are doing a bitwise AND operator on 10 and 9, like so:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/478/1*kUMk7hdW9Z9pw1_2xMJjRA.png" /></figure><p>The result of number &amp; (number — 1) is 8 (2³ = 8). We assign this result to number . We then increment count by 1 (count is now 1)</p><p>On the second loop, we are doing a bitwise AND operator on 8 and 7, like so:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/476/1*6gZZmCY5WER9uLoYHZD44g.png" /></figure><p>The result of number &amp; (number — 1) is 0! We assign this result to number . We then increment count by 1 (count is now 2).</p><p>Since now number is 0, the loop will end, and count = 2 is returned.</p><p>If you do this exercise on the number 32(binary representation is 10000), the loop will end after 1 loop!</p><h3>Space and time complexity</h3><p>The space complexity is O(1) as the space used does not grow with the input.</p><p>The time complexity is O(log(n)) as the algorithm goes through as many iterations as there are set bits. An integer n has log(n) bits, so the worst case would be O(log(n)) !</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=14d5699244ae" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>