<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Micc Wan</title>
    <description>The latest articles on Forem by Micc Wan (@miccwan).</description>
    <link>https://forem.com/miccwan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F892277%2Fb14dcee2-ce22-4167-a0ed-38a461066457.jpeg</url>
      <title>Forem: Micc Wan</title>
      <link>https://forem.com/miccwan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/miccwan"/>
    <language>en</language>
    <item>
      <title>Leetcode Challenge (The end)</title>
      <dc:creator>Micc Wan</dc:creator>
      <pubDate>Thu, 25 Aug 2022 07:17:03 +0000</pubDate>
      <link>https://forem.com/miccwan/leetcode-challenge-the-end-49ma</link>
      <guid>https://forem.com/miccwan/leetcode-challenge-the-end-49ma</guid>
      <description>&lt;p&gt;It's been a while since the last post.&lt;/p&gt;

&lt;p&gt;I'm sorry about that I am here to tell you this series will stop here, due to my personal reasons :(&lt;/p&gt;

&lt;p&gt;I might continue updating the &lt;a href="https://github.com/MiccWan/leetcode"&gt;GitHub repo&lt;/a&gt; sometimes (and maybe not), but there won't be any new post.&lt;/p&gt;

&lt;p&gt;Happy programming everyone :)&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Leetcode Challenge #4 (Postponed)</title>
      <dc:creator>Micc Wan</dc:creator>
      <pubDate>Mon, 25 Jul 2022 13:34:45 +0000</pubDate>
      <link>https://forem.com/miccwan/leetcode-challenge-4-postponed-h54</link>
      <guid>https://forem.com/miccwan/leetcode-challenge-4-postponed-h54</guid>
      <description>&lt;p&gt;I finished the problems last Thursday using my notebook, but I forgot to push them to GitHub. I won't have access to my notebook for a while. So challenge #4 will be updated after I get my notebook back.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>algorithms</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Leetcode Challenge #3</title>
      <dc:creator>Micc Wan</dc:creator>
      <pubDate>Tue, 19 Jul 2022 16:46:50 +0000</pubDate>
      <link>https://forem.com/miccwan/leetcode-challenge-3-1ki0</link>
      <guid>https://forem.com/miccwan/leetcode-challenge-3-1ki0</guid>
      <description>&lt;p&gt;Hi there! Today we are going to finish the last part of &lt;code&gt;Two pointers&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Today's problems
&lt;/h2&gt;

&lt;p&gt;Today's topic: &lt;code&gt;Two pointers&lt;/code&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;ID&lt;/th&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Difficulty&lt;/th&gt;
&lt;th&gt;Problem&lt;/th&gt;
&lt;th&gt;Solution&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;344&lt;/td&gt;
&lt;td&gt;Reverse String&lt;/td&gt;
&lt;td&gt;easy&lt;/td&gt;
&lt;td&gt;&lt;a href="https://leetcode.com/problems/reverse-string/"&gt;link&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;&lt;a href="https://github.com/MiccWan/leetcode/blob/main/js/src/0344_reverse_string.js"&gt;link&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;557&lt;/td&gt;
&lt;td&gt;Reverse Words in a String III&lt;/td&gt;
&lt;td&gt;easy&lt;/td&gt;
&lt;td&gt;&lt;a href="https://leetcode.com/problems/reverse-words-in-a-string-iii/"&gt;link&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;&lt;a href="https://github.com/MiccWan/leetcode/blob/main/js/src/0557_reverse_words_in_a_string_3.js"&gt;link&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;876&lt;/td&gt;
&lt;td&gt;Middle of the Linked List&lt;/td&gt;
&lt;td&gt;easy&lt;/td&gt;
&lt;td&gt;&lt;a href="https://leetcode.com/problems/middle-of-the-linked-list/"&gt;link&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;&lt;a href="https://github.com/MiccWan/leetcode/blob/main/js/src/0876_middle_of_the_linked_list.js"&gt;link&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;19&lt;/td&gt;
&lt;td&gt;Remove Nth Node From End of List&lt;/td&gt;
&lt;td&gt;medium&lt;/td&gt;
&lt;td&gt;&lt;a href="https://leetcode.com/problems/remove-nth-node-from-end-of-list/"&gt;link&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;&lt;a href="https://github.com/MiccWan/leetcode/blob/main/js/src/0019_remove_nth_node_from_end_of_list.js"&gt;link&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;There are 2 string manipulation problems and 2 linked list problems here.&lt;/p&gt;

&lt;h3&gt;
  
  
  344. Reverse String
&lt;/h3&gt;

&lt;p&gt;This is a simple reverse problem. Just set the 2 pointers at the begining and end at the same time, swap the values at the pointers, and then move them inward. Repeat the process until &lt;code&gt;left &amp;gt;= right&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The complexities are &lt;em&gt;O(n)&lt;/em&gt; time and &lt;em&gt;O(1)&lt;/em&gt; extra space.&lt;/p&gt;

&lt;p&gt;NOTE: The problem was asking for in-place reverse, while JavaScript does not support in-place string manipulation. So leetcode sends char array as input. So sweet!&lt;/p&gt;

&lt;h3&gt;
  
  
  577. Reverse Words in a String III
&lt;/h3&gt;

&lt;p&gt;In-place reverse is not required for this problem, so it's time to abuse the power of high-level language. LOL!&lt;br&gt;
Only one line is needed. The &lt;code&gt;split&lt;/code&gt;+&lt;code&gt;join&lt;/code&gt; combo is a powerful tool dealing with string manipulation. Use it along with map to manipulate each segment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The complexities are &lt;em&gt;O(n)&lt;/em&gt; time and &lt;em&gt;O(n)&lt;/em&gt; extra space.&lt;/p&gt;

&lt;p&gt;NOTE: &lt;em&gt;O(1)&lt;/em&gt; extra space is impossible in this problem since the problem requires a new string as return instead of char array. And JS does not support in-place string manipulation.&lt;/p&gt;

&lt;h3&gt;
  
  
  876. Middle of the Linked List
&lt;/h3&gt;

&lt;p&gt;A simple solution is to iterate through the linked list once to get the length. And iterate again for half of lengh to get the middle.&lt;/p&gt;

&lt;p&gt;Another solution is maintain two pointers both starts at the head. In each iteration, the pointer A moves 2 steps while the pointer B move 1 step. This way, when pointer A reaches the end, pointer B is at the middle of the linked list.&lt;/p&gt;

&lt;p&gt;Both solutions take &lt;em&gt;O(n)&lt;/em&gt; time and &lt;em&gt;O(1)&lt;/em&gt; extra space.&lt;/p&gt;

&lt;h3&gt;
  
  
  19. Remove Nth Node From End of List
&lt;/h3&gt;

&lt;p&gt;Same as the last problem, a simple solution is to count the length first. And iterate again to get the n-th node from the end.&lt;/p&gt;

&lt;p&gt;Another way is to set pointer A at the begining and set pointer B n step(s) ahead. Then move both of them at the same time, while pointer B reaches the end, pointer A is at the n-th node from the end.&lt;/p&gt;

&lt;p&gt;Both solutions take &lt;em&gt;O(n)&lt;/em&gt; time and &lt;em&gt;O(1)&lt;/em&gt; extra space.&lt;/p&gt;

&lt;p&gt;NOTE: For the second solution, be careful with some special cases like: removing the first element (so we have to return the second element as head instead of the removed head).&lt;/p&gt;

&lt;h2&gt;
  
  
  Additional Problem
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;ID&lt;/th&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Difficulty&lt;/th&gt;
&lt;th&gt;Problem&lt;/th&gt;
&lt;th&gt;Solution&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Add two numbers&lt;/td&gt;
&lt;td&gt;medium&lt;/td&gt;
&lt;td&gt;&lt;a href="https://leetcode.com/problems/add-two-numbers/"&gt;link&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;&lt;a href="https://github.com/MiccWan/leetcode/blob/main/js/src/0002_add_two_numbers.js"&gt;link&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;I forgot to set the filter &lt;code&gt;tag:two pointers&lt;/code&gt;, so the additional problem is not about two pointers.&lt;/p&gt;

&lt;p&gt;The problem is simply implementing the integer addition the way we've been taught in elementary school.&lt;/p&gt;

&lt;p&gt;Starts from the right most digit, add them up, record the modulo sum and carry, repeat the process in the next digit.&lt;/p&gt;

&lt;p&gt;There are two more things need to handle carefully. The numbers may not be in same length and don't forget to add the last carry.&lt;/p&gt;

&lt;p&gt;The complexities are &lt;em&gt;O(n)&lt;/em&gt; time and &lt;em&gt;O(n)&lt;/em&gt; space.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The 5 problems today are simpler than the last time. It only took about 50 min to finish them.&lt;br&gt;
Next time, there will be 3 medium problems. Hope it will be more interesting for you to read.&lt;/p&gt;




&lt;p&gt;&amp;lt;-- &lt;a href="https://dev.to/miccwan/leetcode-practice-2-51pg"&gt;Previous&lt;/a&gt; | &lt;a href="https://dev.to/miccwan/my-leetcode-practice-diary-3415"&gt;Home&lt;/a&gt; | Next --&amp;gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>algorithms</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Leetcode Challenge #2</title>
      <dc:creator>Micc Wan</dc:creator>
      <pubDate>Sun, 17 Jul 2022 15:33:14 +0000</pubDate>
      <link>https://forem.com/miccwan/leetcode-practice-2-51pg</link>
      <guid>https://forem.com/miccwan/leetcode-practice-2-51pg</guid>
      <description>&lt;p&gt;It has been two days since the last post. I found out that the &lt;a href="https://leetcode.com/study-plan/algorithm/"&gt;study plan&lt;/a&gt; is supposed to be done daily. It unlocks new problem set everyday. So I'll just try to finish two days at a time (at least for the part I) and see if I need to adjust my schedule when I get to the second part .&lt;/p&gt;

&lt;h2&gt;
  
  
  Today's problems
&lt;/h2&gt;

&lt;p&gt;Today's topic: &lt;code&gt;Two pointers&lt;/code&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;ID&lt;/th&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Difficulty&lt;/th&gt;
&lt;th&gt;Problem&lt;/th&gt;
&lt;th&gt;Solution&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;977&lt;/td&gt;
&lt;td&gt;Squares of a Sorted Array&lt;/td&gt;
&lt;td&gt;easy&lt;/td&gt;
&lt;td&gt;&lt;a href="https://leetcode.com/problems/squares-of-a-sorted-array/"&gt;link&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;&lt;a href="https://github.com/MiccWan/leetcode/blob/main/js/src/0977_squares_of_a_sorted_array.js"&gt;link&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;189&lt;/td&gt;
&lt;td&gt;Rotate Array&lt;/td&gt;
&lt;td&gt;medium&lt;/td&gt;
&lt;td&gt;&lt;a href="https://leetcode.com/problems/rotate-array/"&gt;link&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;&lt;a href="https://github.com/MiccWan/leetcode/blob/main/js/src/0189_rotate_array.js"&gt;link&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;283&lt;/td&gt;
&lt;td&gt;Move Zeroes&lt;/td&gt;
&lt;td&gt;easy&lt;/td&gt;
&lt;td&gt;&lt;a href="https://leetcode.com/problems/move-zeroes/"&gt;link&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;&lt;a href="https://github.com/MiccWan/leetcode/blob/main/js/src/0283_move_zeros.js"&gt;link&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;167&lt;/td&gt;
&lt;td&gt;Two Sum II - Input Array Is Sorted&lt;/td&gt;
&lt;td&gt;medium&lt;/td&gt;
&lt;td&gt;&lt;a href="https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/"&gt;link&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;&lt;a href="https://github.com/MiccWan/leetcode/blob/main/js/src/0167_two_sum_2.js"&gt;link&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The topic for Day 2~5 is &lt;code&gt;Two pointers&lt;/code&gt;. I'm not familiar with this word but I think the binary search method I used last time is a kind of two pointers.&lt;/p&gt;

&lt;p&gt;I'm gonna skip problems 977, 283, 167 since they are actually pretty easy and the solutions were intuitive.&lt;/p&gt;

&lt;h3&gt;
  
  
  189. Rotate Array
&lt;/h3&gt;

&lt;p&gt;The problem is really interesting.&lt;br&gt;
Some simple methods like &lt;strong&gt;slice+concat&lt;/strong&gt; or &lt;strong&gt;rotate from the end&lt;/strong&gt; costs too much space (&lt;em&gt;O(n)&lt;/em&gt; and &lt;em&gt;O(k)&lt;/em&gt; respectively).&lt;br&gt;
It tooks me some time to come up with an &lt;em&gt;O(1)&lt;/em&gt; space solution (though they are all &lt;em&gt;O(n)&lt;/em&gt; time).&lt;br&gt;
The main idea is: &lt;br&gt;
If you move the &lt;strong&gt;nums[0]&lt;/strong&gt; to &lt;strong&gt;nums[k]&lt;/strong&gt;, and move the original &lt;strong&gt;nums[k]&lt;/strong&gt; to &lt;strong&gt;nums[2*k]&lt;/strong&gt;, ...(under modulo n, of course), it will come back to &lt;strong&gt;nums[0]&lt;/strong&gt; at some point. But in some cases, this cycle does not contain all of the numbers. More precisely, only indexes in the form &lt;code&gt;0 + m * gcd(n, k)&lt;/code&gt; will be included. So we need to repeat the same procedure but starts from &lt;code&gt;1 ~ gcd(n, k)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;e.g. consider &lt;code&gt;n = 9, k = 6&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0 -&amp;gt; 6 -&amp;gt; 3 -&amp;gt; 0 -&amp;gt; ...
1 -&amp;gt; 7 -&amp;gt; 4 -&amp;gt; 1 -&amp;gt; ...
2 -&amp;gt; 8 -&amp;gt; 5 -&amp;gt; 2 -&amp;gt; ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The resulting code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;rotate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;g&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;gcd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;g&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;tmp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;tmp&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;tmp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;]];&lt;/span&gt;
      &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This solution is some kind of inflexible. So I checked leetcode discussion and found a beautiful magic. The key concept is:&lt;br&gt;
&lt;code&gt;rotate(n, k) = reverse(0, n-1) + reverse(0, k-1) + reverse(k, n-1)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;e.g. consider &lt;code&gt;n = 7, n = 3&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   [1, 2, 3, 4, 5, 6, 7] // original
-&amp;gt; [7, 6, 5, 4, 3, 2, 1] // reverse(0, 6)
-&amp;gt; [5, 6, 7, 4, 3, 2, 1] // reverse(0, 2)
-&amp;gt; [5, 6, 7, 1, 2, 3, 4] // reverse(3, 6)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I haven't figured out how this magic was discovered, but it was really shocking for me.&lt;/p&gt;

&lt;h2&gt;
  
  
  Additional Problem
&lt;/h2&gt;

&lt;p&gt;Another two pointer problem I picked today is:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;ID&lt;/th&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Difficulty&lt;/th&gt;
&lt;th&gt;Problem&lt;/th&gt;
&lt;th&gt;Solution&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;11&lt;/td&gt;
&lt;td&gt;Container With Most Water&lt;/td&gt;
&lt;td&gt;medium&lt;/td&gt;
&lt;td&gt;&lt;a href="https://leetcode.com/problems/container-with-most-water/"&gt;link&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;&lt;a href="https://github.com/MiccWan/leetcode/blob/main/js/src/0011_container_with_most_water.js"&gt;link&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Unexpectedly, this problem took me about 40 minutes. According to the constraint, &lt;code&gt;n &amp;lt; 10^5&lt;/code&gt;, &lt;em&gt;O(n^2)&lt;/em&gt; is not acceptable. So brute force enumeration is not good enough.&lt;/p&gt;

&lt;p&gt;After tackled with many examples, I noticed that the area formula &lt;code&gt;(l - r) * Math.min(height[l], height[r])&lt;/code&gt; only takes the minimum of the heights into account. Some greedy algorithm that prioritize the smaller height might come in handy in this case.&lt;/p&gt;

&lt;p&gt;The final algorithm becomes: &lt;br&gt;
Starts from the most outer pair, record the area value and &lt;strong&gt;shrink the smaller side inward&lt;/strong&gt; until it found some larger height. If the new area is bigger, update the record. Then repeat the steps again and again until the two pointers coinside. Note that if the heights of the both sides are the same, we have to shrink them at the same time, since the area will never get larger if only one side is shrinked.&lt;/p&gt;

&lt;p&gt;Each time we shrink the border, the width of the container will become smaller. So we only needs to focus on &lt;strong&gt;maximizing the heights&lt;/strong&gt;. The greedy algorithm works due to the property of area formula.&lt;/p&gt;

&lt;p&gt;The complexities are &lt;em&gt;O(n)&lt;/em&gt; time and &lt;em&gt;O(1)&lt;/em&gt; space, since the process of shrinking border will be done in total of n-1 times and calculating the area is &lt;em&gt;O(1)&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The five problems above took me about 1.5 hrs, due to the unexpected difficulty of the last problem. Also, they are more difficult than the problems last time. The proof of correctness for the last algorithm even require some mathematical techniques, e.g. Loop invariant. But this series is not an algorithm tutorial, sorry I will skip those details.&lt;/p&gt;

&lt;p&gt;So that's my challenge today, hope u enjoy. :)&lt;/p&gt;




&lt;p&gt;&amp;lt;-- &lt;a href="https://dev.to/miccwan/leetcode-practice-day-1-1k"&gt;Previous&lt;/a&gt; | &lt;a href="https://dev.to/miccwan/my-leetcode-practice-diary-3415"&gt;Home&lt;/a&gt; | &lt;a href="https://dev.to/miccwan/leetcode-challenge-3-1ki0"&gt;Next&lt;/a&gt; --&amp;gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>algorithms</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Leetcode Challenge #1</title>
      <dc:creator>Micc Wan</dc:creator>
      <pubDate>Fri, 15 Jul 2022 11:09:58 +0000</pubDate>
      <link>https://forem.com/miccwan/leetcode-practice-day-1-1k</link>
      <guid>https://forem.com/miccwan/leetcode-practice-day-1-1k</guid>
      <description>&lt;p&gt;P.S. If you are new here, check my &lt;a href="https://dev.to/miccwan/my-leetcode-practice-diary-3415"&gt;Challenge Introduction&lt;/a&gt; before this post!&lt;/p&gt;




&lt;p&gt;So let's start the challenge.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Algorithm Study Plan
&lt;/h2&gt;

&lt;p&gt;I found this &lt;a href="https://leetcode.com/study-plan/algorithm"&gt;study plan&lt;/a&gt; in the leetcode home page, it might be a good start for me. For the first part, the plan includes 2~3 problems(most are easy, some medium and no hard) every day. I'll try to finish them each day and maybe 0~2 additional problems to make sure I used the whole hour.&lt;/p&gt;

&lt;h2&gt;
  
  
  Today's problems
&lt;/h2&gt;

&lt;p&gt;Today's topic: &lt;code&gt;Binary Search&lt;/code&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;ID&lt;/th&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Difficulty&lt;/th&gt;
&lt;th&gt;Problem&lt;/th&gt;
&lt;th&gt;Solution&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;704&lt;/td&gt;
&lt;td&gt;Binary Search&lt;/td&gt;
&lt;td&gt;easy&lt;/td&gt;
&lt;td&gt;&lt;a href="https://leetcode.com/problems/binary-search/"&gt;link&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;&lt;a href="https://github.com/MiccWan/leetcode/blob/main/js/src/0704_binary_search.js"&gt;link&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;278&lt;/td&gt;
&lt;td&gt;First Bad Version&lt;/td&gt;
&lt;td&gt;easy&lt;/td&gt;
&lt;td&gt;&lt;a href="https://leetcode.com/problems/first-bad-version/"&gt;link&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;&lt;a href="https://github.com/MiccWan/leetcode/blob/main/js/src/0278_first_bad_version.js"&gt;link&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;35&lt;/td&gt;
&lt;td&gt;Search Insert Position&lt;/td&gt;
&lt;td&gt;easy&lt;/td&gt;
&lt;td&gt;&lt;a href="https://leetcode.com/problems/search-insert-position/"&gt;link&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;&lt;a href="https://github.com/MiccWan/leetcode/blob/main/js/src/0035_search_insert_position.js"&gt;link&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;I've seen many implementations of binary search. They should all be done in &lt;em&gt;O(log(n))&lt;/em&gt; time and &lt;em&gt;O(1)&lt;/em&gt; space.&lt;/p&gt;

&lt;p&gt;The one I love the most is to maintain the &lt;code&gt;left&lt;/code&gt; and &lt;code&gt;right&lt;/code&gt; pointers under certain conditions, since it is intuitive to use. This works for all three problems above.&lt;/p&gt;

&lt;p&gt;Another interesting implemention I've seen is to maintain one pointer which will be added either &lt;code&gt;2^k&lt;/code&gt; or &lt;code&gt;0&lt;/code&gt; in each iteration. &lt;br&gt;
E.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;binarySearch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;step&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;step&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;step&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;step&lt;/span&gt; &lt;span class="o"&gt;/=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One could make use of the constrain of the problem to get the initial step size. Or dynamically find it with another loop.&lt;/p&gt;

&lt;h2&gt;
  
  
  Additional Problem
&lt;/h2&gt;

&lt;p&gt;I picked another medium binary search problem today.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;ID&lt;/th&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Difficulty&lt;/th&gt;
&lt;th&gt;Problem&lt;/th&gt;
&lt;th&gt;Solution&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;33&lt;/td&gt;
&lt;td&gt;Search in Rotated Sorted Array&lt;/td&gt;
&lt;td&gt;medium&lt;/td&gt;
&lt;td&gt;&lt;a href="https://leetcode.com/problems/search-in-rotated-sorted-array/"&gt;link&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;&lt;a href="https://github.com/MiccWan/leetcode/blob/main/js/src/0033_search_in_rotated_sorted_array.js"&gt;link&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;First I come up with the most basic idea: use binary search to find the pivot, then apply binary search on either the first or the second part depends on whether &lt;code&gt;nums[0] &amp;lt; target&lt;/code&gt;. This takes 53 lines and it's not easy to read.&lt;/p&gt;

&lt;p&gt;Then I realize I could simply apply binary search on the whole array since &lt;code&gt;realIndex = (originalIndex + pivot) % arr.length&lt;/code&gt;.&lt;br&gt;
With this trick, the resulting code is reduced to 40 lines and it's more readable for me! The complexity are still &lt;em&gt;O(log(n))&lt;/em&gt; time and &lt;em&gt;O(1)&lt;/em&gt; space.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;That's my first post of my practice. Any suggestions would be appreciated! Thanks for your reading.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://dev.to/miccwan/my-leetcode-practice-diary-3415"&gt;Home&lt;/a&gt; | &lt;a href="https://dev.to/miccwan/leetcode-practice-2-51pg"&gt;Next&lt;/a&gt; --&amp;gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>algorithms</category>
      <category>beginners</category>
    </item>
    <item>
      <title>My Leetcode Challenge</title>
      <dc:creator>Micc Wan</dc:creator>
      <pubDate>Fri, 15 Jul 2022 05:57:01 +0000</pubDate>
      <link>https://forem.com/miccwan/my-leetcode-practice-diary-3415</link>
      <guid>https://forem.com/miccwan/my-leetcode-practice-diary-3415</guid>
      <description>&lt;p&gt;Helloworld from &lt;strong&gt;Taiwan&lt;/strong&gt;! This is &lt;a href="https://github.com/miccwan"&gt;Michael&lt;/a&gt;. I'm a &lt;strong&gt;junior JS fullstack engineer&lt;/strong&gt; who just graduated from university. I'm going to practice my English writing skill and programming skill before being enlisted in the army(mid October).&lt;/p&gt;

&lt;p&gt;Starting today, I will solve some leetcode problems (with JS) about three times a week, one hour at a time, and post the progress here. Any suggestions or corrections are welcome!&lt;/p&gt;

&lt;p&gt;The code will be collected in this Github repository:&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--566lAguM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/github-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/MiccWan"&gt;
        MiccWan
      &lt;/a&gt; / &lt;a href="https://github.com/MiccWan/leetcode"&gt;
        leetcode
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;


&lt;h3&gt;
  
  
  Posts
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://dev.to/miccwan/leetcode-practice-day-1-1k"&gt;Binary Search&lt;/a&gt; (7/15)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://dev.to/miccwan/leetcode-practice-2-51pg"&gt;Two Pointers #1&lt;/a&gt; (7/17)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://dev.to/miccwan/leetcode-challenge-3-1ki0"&gt;Two Pointers #2&lt;/a&gt; (7/20)&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
