<?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>DEV Community: Andrew Hsu</title>
    <description>The latest articles on DEV Community by Andrew Hsu (@drewhsu86).</description>
    <link>https://dev.to/drewhsu86</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%2F372982%2Fba5462a8-f920-4536-b0d0-a137b3798b89.png</url>
      <title>DEV Community: Andrew Hsu</title>
      <link>https://dev.to/drewhsu86</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/drewhsu86"/>
    <language>en</language>
    <item>
      <title>Leetcode Daily - Sum of Left Leaves</title>
      <dc:creator>Andrew Hsu</dc:creator>
      <pubDate>Mon, 24 Aug 2020 17:57:29 +0000</pubDate>
      <link>https://dev.to/drewhsu86/leetcode-daily-sum-of-left-leaves-17k8</link>
      <guid>https://dev.to/drewhsu86/leetcode-daily-sum-of-left-leaves-17k8</guid>
      <description>&lt;h1&gt;
  
  
  Leetcode Daily - August 24, 2020
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Sum of Left Leaves
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/552/week-4-august-22nd-august-28th/3435/"&gt;Link to Leetcode Question&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lately I've been grinding Leetcode and decided to record some of my thoughts on this blog. This is both to help me look back on what I've worked on as well as help others see how one might think about the problems.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;However, since many people post their own solutions in the discussions section of Leetcode, I won't necessarily be posting the optimal solution.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Question
&lt;/h2&gt;

&lt;p&gt;(Copy Pasted From Leetcode)&lt;/p&gt;

&lt;p&gt;Find the sum of all left leaves in a given binary tree.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    3
   / \
  9  20
    /  \
   15   7

There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  My Approach(es)
&lt;/h2&gt;

&lt;p&gt;I won't go over all the code for all attempts but I will explain my approach(es) qualitatively.&lt;/p&gt;

&lt;h3&gt;
  
  
  Attempt 1 - DFS
&lt;/h3&gt;

&lt;h5&gt;
  
  
  (Submission - Accepted)
&lt;/h5&gt;

&lt;p&gt;When I see a tree problem like this, my go-to is DFS or BFS (I usually start with DFS by default), and I tend to use a stack (you can use recursion as well).&lt;/p&gt;

&lt;p&gt;The principle is to simply check each node to find nodes that: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Are leaves&lt;/li&gt;
&lt;li&gt;Were the left of their parent &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once a node is found that meets these conditions, I can simply add their value to a sum that is initialized to zero at the start of the program. &lt;/p&gt;

&lt;p&gt;The only additional thing I really need is a data structure that tells the current node if it was the left or right of its parent (or root if it is root).&lt;/p&gt;

&lt;p&gt;I implemented this with a dictionary (in Python) that stores the node, and 'nType', or node type. The node type is currently either 'root', 'right' or 'left'. I left the type as a string for the purposes of quickly submitting code, but an enum can be used since the data can only be one of a possible set.&lt;/p&gt;

&lt;p&gt;Submitted Code (Python):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;object&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sumOfLeftLeaves&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; 
        &lt;span class="n"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="s"&gt;'node'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'nType'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'root'&lt;/span&gt;&lt;span class="p"&gt;}]&lt;/span&gt;

        &lt;span class="c1"&gt;# dfs using a stack 
&lt;/span&gt;        &lt;span class="c1"&gt;# add to result if left and a leaf 
&lt;/span&gt;        &lt;span class="c1"&gt;# should add an additional data, whether it was left or right or root
&lt;/span&gt;        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; 
            &lt;span class="n"&gt;currNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'node'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="n"&gt;currType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'nType'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

            &lt;span class="c1"&gt;# check if leaf 
&lt;/span&gt;            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;currNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;currNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;currType&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;'left'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;currNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;

            &lt;span class="c1"&gt;# add children to tree 
&lt;/span&gt;            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;currNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="s"&gt;'node'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;currNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'nType'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'left'&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;

            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;currNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="s"&gt;'node'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;currNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'nType'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'right'&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;

        &lt;span class="c1"&gt;# end of while loop
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Discussion and Conclusions
&lt;/h2&gt;

&lt;p&gt;As mentioned in the approach section, a better data structure such as an enum could be used for storing the status of the current node. But my main concern for optimizing performance is whether I can consistently search less than O(n) (where n is the number of nodes in the tree) nodes.&lt;/p&gt;

&lt;p&gt;I've been thinking about how to potentially rule out certain portions of the tree, but based on the node data structure I'm not sure that's feasible. Basically, since any node can have a left child, and each node points to its children but not its parents, we would have to check every node to make sure that it's not both left and a leaf node.&lt;/p&gt;

</description>
      <category>python</category>
      <category>codenewbie</category>
      <category>challenge</category>
    </item>
    <item>
      <title>Leetcode Daily - Longest Palindrome </title>
      <dc:creator>Andrew Hsu</dc:creator>
      <pubDate>Fri, 14 Aug 2020 15:45:28 +0000</pubDate>
      <link>https://dev.to/drewhsu86/leetcode-daily-longest-palindrome-525</link>
      <guid>https://dev.to/drewhsu86/leetcode-daily-longest-palindrome-525</guid>
      <description>&lt;h1&gt;
  
  
  Leetcode Daily - August 14, 2020
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Longest Palindrome
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/550/week-2-august-8th-august-14th/3423/"&gt;Link to Leetcode Question&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lately I've been grinding Leetcode and decided to record some of my thoughts on this blog. This is both to help me look back on what I've worked on as well as help others see how one might think about the problems.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;However, since many people post their own solutions in the discussions section of Leetcode, I won't necessarily be posting the optimal solution.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Question
&lt;/h2&gt;

&lt;p&gt;(Copy Pasted From Leetcode)&lt;/p&gt;

&lt;p&gt;Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.&lt;/p&gt;

&lt;p&gt;This is case sensitive, for example "Aa" is not considered a palindrome here.&lt;/p&gt;

&lt;p&gt;Note:&lt;br&gt;
Assume the length of given string will not exceed 1,010.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input:
"abccccdd"

Output:
7

Explanation:
One longest palindrome that can be built is "dccaccd", whose length is 7.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  My Approach(es)
&lt;/h2&gt;

&lt;p&gt;I won't go over all the code for all attempts but I will explain my approach(es) qualitatively.&lt;/p&gt;

&lt;h3&gt;
  
  
  Attempt 1 - Determine Logical Conditions For Longest Palindrome Length
&lt;/h3&gt;

&lt;h5&gt;
  
  
  (Submission - Accepted)
&lt;/h5&gt;

&lt;p&gt;I ended up approaching this as a logic problem instead of as a computer science problem. The first thing I noticed is that, except for a middle character, palindromes have matching pairs of the same character symmetric with the middle. So if we were to count how many of each unique character we have, all even sets would be able to go on a palindrome, but there is only space for only one odd "spare" letter.&lt;/p&gt;

&lt;p&gt;I decided to pseudocode first and then write my code following this blueprint:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Count each of the letters and store the counts &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Go through each of the counts and start adding them to a sum&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The sum is the length of the longest palindrome length &lt;/li&gt;
&lt;li&gt;If a count is even, we add it&lt;/li&gt;
&lt;li&gt;If the count is odd, and we haven't seen an odd, we add it &lt;/li&gt;
&lt;li&gt;If the count is odd, and we already added an odd, we add that count minus one (the largest even value we can add)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Submitted Code (Javascript):&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;longestPalindrome&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;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// palindrome can only contain up to one odd set of letters &lt;/span&gt;
    &lt;span class="c1"&gt;// all even sets of letters work &lt;/span&gt;
    &lt;span class="c1"&gt;// go through the string and store all the unique letter counts &lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dict&lt;/span&gt; &lt;span class="o"&gt;=&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;s&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;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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;[&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;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;dict&lt;/span&gt;&lt;span class="p"&gt;[&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;i&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;[&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;i&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// make an array of our letter counts to iterate on &lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;letterCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; 
    &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;letterCount&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;

    &lt;span class="c1"&gt;// create variables to remember how long our longest palindrome is &lt;/span&gt;
    &lt;span class="c1"&gt;// as well as whether we have our one odd letter set &lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;seenEven&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; 
    &lt;span class="c1"&gt;// start adding up letter sets&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;count&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;letterCount&lt;/span&gt;&lt;span class="p"&gt;)&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;count&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&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="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; 
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&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="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;seenEven&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="c1"&gt;// add odd set if haven't seen one yet&lt;/span&gt;
                &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; 
                &lt;span class="nx"&gt;seenEven&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; 
            &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="c1"&gt;// turn into even set and add &lt;/span&gt;
                &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Discussion and Conclusions
&lt;/h2&gt;

&lt;p&gt;I think the solution based on logic is very straightforward, and has time and space complexity of O(n) where n is the length of s. There are probably programming and computer science tricks that can optimize this code even further.&lt;/p&gt;

&lt;p&gt;For example, I thought about it afterwards and instead of storing whether we have seen an odd or not, we could always add the "evenized" value, for example &lt;code&gt;count - (count%2)&lt;/code&gt;. Then add the end, if the longest palindrome length sum were less than s.length, we could simply add 1 (there are spare letters left).&lt;/p&gt;

&lt;p&gt;Resubmitted Code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;longestPalindrome&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;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// palindrome can only contain up to one odd set of letters &lt;/span&gt;
    &lt;span class="c1"&gt;// all even sets of letters work &lt;/span&gt;
    &lt;span class="c1"&gt;// go through the string and store all the unique letter counts &lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dict&lt;/span&gt; &lt;span class="o"&gt;=&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;s&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;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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;[&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;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;dict&lt;/span&gt;&lt;span class="p"&gt;[&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;i&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;[&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;i&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// make an array of our letter counts to iterate on &lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;letterCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; 
    &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;letterCount&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;

    &lt;span class="c1"&gt;// create variables to remember how long our longest palindrome is &lt;/span&gt;
    &lt;span class="c1"&gt;// as well as whether we have our one odd letter set &lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

    &lt;span class="c1"&gt;// start adding up letter sets    &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;count&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;letterCount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&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;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>challenge</category>
    </item>
    <item>
      <title>Leetcode Daily - Iterator for Combinator</title>
      <dc:creator>Andrew Hsu</dc:creator>
      <pubDate>Thu, 13 Aug 2020 21:38:01 +0000</pubDate>
      <link>https://dev.to/drewhsu86/leetcode-daily-iterator-for-combinator-e8g</link>
      <guid>https://dev.to/drewhsu86/leetcode-daily-iterator-for-combinator-e8g</guid>
      <description>&lt;h1&gt;
  
  
  Leetcode Daily - August 13, 2020
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Iterator for Combinator
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/550/week-2-august-8th-august-14th/3422/"&gt;Link to Leetcode Question&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lately I've been grinding Leetcode and decided to record some of my thoughts on this blog. This is both to help me look back on what I've worked on as well as help others see how one might think about the problems.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;However, since many people post their own solutions in the discussions section of Leetcode, I won't necessarily be posting the optimal solution.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Question
&lt;/h2&gt;

&lt;p&gt;(Copy Pasted From Leetcode)&lt;/p&gt;

&lt;p&gt;Design an Iterator class, which has:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A constructor that takes a string characters of sorted distinct lowercase English letters and a number combinationLength as arguments.&lt;/li&gt;
&lt;li&gt;A function next() that returns the next combination of length combinationLength in lexicographical order.&lt;/li&gt;
&lt;li&gt;A function hasNext() that returns True if and only if there exists a next combination.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CombinationIterator iterator = new CombinationIterator("abc", 2); // creates the iterator.

iterator.next(); // returns "ab"
iterator.hasNext(); // returns true
iterator.next(); // returns "ac"
iterator.hasNext(); // returns true
iterator.next(); // returns "bc"
iterator.hasNext(); // returns false
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Constraints:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1 &amp;lt;= combinationLength &amp;lt;= characters.length &amp;lt;= 15&lt;/li&gt;
&lt;li&gt;There will be at most 10^4 function calls per test.&lt;/li&gt;
&lt;li&gt;It's guaranteed that all calls of the function next are valid.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  My Approach(es)
&lt;/h2&gt;

&lt;p&gt;I won't go over all the code for all attempts but I will explain my approach(es) qualitatively.&lt;/p&gt;

&lt;h3&gt;
  
  
  Attempt 1 - DFS or BFS
&lt;/h3&gt;

&lt;h5&gt;
  
  
  (Submission - Accepted)
&lt;/h5&gt;

&lt;p&gt;I decided to use the following data structures for my class:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Store the characters as a string and combinationLength as a number &lt;/li&gt;
&lt;li&gt;Use the idea of a mask consisting of 0's and 1's. This is to determine which characters from the character string will be used to form the output string of .next() &lt;/li&gt;
&lt;li&gt;Also generate and store all possible masks when the constructor is run. The main reason for this is that I am familiar with using search/backtracking to create all the permutations of ones and zeros, but if I'm not sure how best to iterate from one given permutation to the next.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once the array of all possible masks is calculated, .next() and .hasNext() become pretty easy if we are storing the mask index. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;.next() iterates the mask index and then uses the mask to return a string.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;.hasNext() just check to see if the mask index is not the last index of the array.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Submitted Code in Javascript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;CombinationIterator&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;characters&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;combinationLength&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;characters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;characters&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;combinationLength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;combinationLength&lt;/span&gt; 
    &lt;span class="c1"&gt;// should we generate all combinations now? or when the methods are called? &lt;/span&gt;
    &lt;span class="c1"&gt;// for testing purposes let's generate them now &lt;/span&gt;
    &lt;span class="c1"&gt;// string mask of characters &lt;/span&gt;

    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mask&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

    &lt;span class="c1"&gt;// to keep lexographical order we need bfs &lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;charLength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;characters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&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;stack&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;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// if length is 6, add it to this.mask &lt;/span&gt;
        &lt;span class="c1"&gt;// else, add the possible combinations to the stack &lt;/span&gt;
        &lt;span class="c1"&gt;// prefer adding 1's first up to combinationLength &lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;currCombo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;shift&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;currCombo&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;===&lt;/span&gt; &lt;span class="nx"&gt;charLength&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mask&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currCombo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&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;currCombo&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;lt;&lt;/span&gt; &lt;span class="nx"&gt;charLength&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// regex match /&amp;lt;char&amp;gt;/g returns array, so length of that &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;currCombo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/1/g&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;length&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;combinationLength&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currCombo&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&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;currCombo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/0/g&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;length&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;charLength&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;combinationLength&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currCombo&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;0&lt;/span&gt;&lt;span class="dl"&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;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;maskInd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;initial masks: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mask&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * @return {string}
 */&lt;/span&gt;
&lt;span class="nx"&gt;CombinationIterator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&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="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;maskInd&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt; 
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;currMask&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mask&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;maskInd&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&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;currMask&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;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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currMask&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;characters&lt;/span&gt;&lt;span class="p"&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="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; 
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * @return {boolean}
 */&lt;/span&gt;
&lt;span class="nx"&gt;CombinationIterator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hasNext&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="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// mask cannot iterate if there are no more combinations &lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;maskInd&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mask&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;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Discussion and Conclusions
&lt;/h2&gt;

&lt;p&gt;I want to explore the approach of iterating the next combination based on the previous combination mask. This is because when the constructor is run, it is doing all the computational work up front, and finding all the permutations. &lt;/p&gt;

&lt;p&gt;There are C(n, k) (n choose k) combinations, where n is the length of characters and k is combinationLength. And the height of the tree formed by my search is k (because there are k characters in a valid string) so the breadth-first-search scales at O( k*C(n,k) ).&lt;/p&gt;

&lt;p&gt;If we use the formula for calculating combinations, we get:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt; k * C(n,k) = k *  n!/(k! * (n-k)!) 
            = n!/((k-1)! * (n-k)!)                  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can reduce the time complexity of the constructor if we just generate the initial mask, and developed a formula to determine the next mask based on the previous. &lt;/p&gt;

&lt;p&gt;I wrote the following pseudocode in my comments but did not settle on a way to consistently determine the next mask value. I realized that at some point, the ones that have been moved from the left all the way to the right would have to be reset back to a position on the left. However, the next 1 on the left would've been shifted once to the right.&lt;/p&gt;

&lt;p&gt;Example for characters.length = 6 and combinationLength = 3:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    111000 110100 110010 110001 
    reset to 101100 ... 101001
    reset to 100110 ... 100101
    reset to 100011 ... 100011
    reset to 011100 ... 011001
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;For my next steps I will explore writing an algorithm that can determine the next mask value from the previous. Since it follows a logical progression it should be possible, but I need to come up with some valid conditional statements for determining when to "reset" some of the 1's from the right back to the left. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>challenge</category>
    </item>
    <item>
      <title>Leetcode Daily - H-Index</title>
      <dc:creator>Andrew Hsu</dc:creator>
      <pubDate>Tue, 11 Aug 2020 17:04:59 +0000</pubDate>
      <link>https://dev.to/drewhsu86/leetcode-daily-h-index-2nch</link>
      <guid>https://dev.to/drewhsu86/leetcode-daily-h-index-2nch</guid>
      <description>&lt;h1&gt;
  
  
  Leetcode Daily - August 11, 2020
&lt;/h1&gt;

&lt;h2&gt;
  
  
  H-index
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/550/week-2-august-8th-august-14th/3420/"&gt;Link to Leetcode Question&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lately I've been grinding Leetcode and decided to record some of my thoughts on this blog. This is both to help me look back on what I've worked on as well as help others see how one might think about the problems.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;However, since many people post their own solutions in the discussions section of Leetcode, I won't necessarily be posting the optimal solution.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Question
&lt;/h2&gt;

&lt;p&gt;(Copy Pasted From Leetcode)&lt;/p&gt;

&lt;p&gt;Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.&lt;/p&gt;

&lt;p&gt;According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each."&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: citations = [3,0,6,1,5]
Output: 3 
Explanation: [3,0,6,1,5] means the researcher has 5 papers in total and each of them had 
             received 3, 0, 6, 1, 5 citations respectively. 
             Since the researcher has 3 papers with at least 3 citations each and the remaining 
             two with no more than 3 citations each, her h-index is 3.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; If there are several possible values for h, the maximum one is taken as the h-index.&lt;/p&gt;

&lt;h2&gt;
  
  
  My Approach(es)
&lt;/h2&gt;

&lt;p&gt;I won't go over all the code for all attempts but I will explain my approach(es) qualitatively.&lt;/p&gt;

&lt;h3&gt;
  
  
  Attempt 1 - Check Possible H-Index Values
&lt;/h3&gt;

&lt;h5&gt;
  
  
  (Submission - Accepted)
&lt;/h5&gt;

&lt;p&gt;To start out this question, I decided to think about how I would solve this for simple arrays as a human. I took a minute to understand and clarify the definition of h-index. &lt;/p&gt;

&lt;p&gt;"A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each." &lt;/p&gt;

&lt;p&gt;I made the following notes: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The h-index is a type of maximum value which satisfies the stated conditions.&lt;/li&gt;
&lt;li&gt;The h-index depends on counting the number of papers which satisfies a condition. &lt;/li&gt;
&lt;li&gt;The condition is simply related to the number in the array (number of citations).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I decided that for my first attempt I could complete it with an O(n&lt;sup&gt;2&lt;/sup&gt;) solution. First we set the h-index to be the maximum possible value, which is the number of papers published. Then we iterate down and check if it meets the h-index conditions.&lt;/p&gt;

&lt;p&gt;Submitted Code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;hIndex&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;citations&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;tryH&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;citations&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="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tryH&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&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;counter&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="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;citations&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;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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;citations&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;tryH&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;counter&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;tryH&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;tryH&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;tryH&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&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;h3&gt;
  
  
  Attempt 2 - Remove one iterative loop from O(n&lt;sup&gt;2&lt;/sup&gt;) solution
&lt;/h3&gt;

&lt;h5&gt;
  
  
  (Submission - Accepted)
&lt;/h5&gt;

&lt;p&gt;I have to admit here that while I realized I needed to optimize on the O(n&lt;sup&gt;2&lt;/sup&gt;) solution, I also realized that I had seen this problem before. &lt;/p&gt;

&lt;p&gt;I had seen this problem in the introduction section of the book: &lt;a href="https://www.amazon.com/Elements-Programming-Interviews-Python-Insiders/dp/1537713949"&gt;Elements of Programming Interviews in Python&lt;/a&gt;, where they detail the thought process and more optimal solution to the h-index problem. &lt;/p&gt;

&lt;p&gt;I use the same concept they explain in the book, which is to sort the array first. This is because, if you sort the array from high to low, then if citations[i] &amp;gt;= i+1 then all the previous values in citations are also greater than or equal to i+1, which is the number of papers (array index but counting starting from 1 instead of 0).&lt;/p&gt;

&lt;p&gt;Assuming the sort is O(nlog(n)), now the iterative element of the algorithm is O(n) so the total time complexity is O(nlog(n)).&lt;/p&gt;

&lt;p&gt;Submitted Code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;hIndex&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;citations&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;citations&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;a&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;counter&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="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;citations&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;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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;citations&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&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;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;counter&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;counter&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;h2&gt;
  
  
  Discussion and Conclusions
&lt;/h2&gt;

&lt;p&gt;This is a very interesting example for me because it touches on a lot of challenges newer coders might run into on technical interview questions. The first one is to clarify and understand the question correctly. The second is to understand the approach and how to optimize it. And finally, it is personally interesting to me because I got the Elements of Programming Interviews book when I started studying coding challenges, and it took me a long time to comprehend and try programming the questions. But after almost one year of studying I feel so much more comfortable with questions like these that I want to remember what my initial feeling of being challenged was like so I won't be as intimidated by harder questions in the future.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Leetcode Daily - Excel Sheet Column Number</title>
      <dc:creator>Andrew Hsu</dc:creator>
      <pubDate>Mon, 10 Aug 2020 15:17:01 +0000</pubDate>
      <link>https://dev.to/drewhsu86/leetcode-daily-excel-sheet-column-number-bce</link>
      <guid>https://dev.to/drewhsu86/leetcode-daily-excel-sheet-column-number-bce</guid>
      <description>&lt;h1&gt;
  
  
  Leetcode Daily - August 10, 2020
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Excel Sheet Column Number
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/550/week-2-august-8th-august-14th/3419/"&gt;Link to Leetcode Question&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lately I've been grinding Leetcode and decided to record some of my thoughts on this blog. This is both to help me look back on what I've worked on as well as help others see how one might think about the problems.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;However, since many people post their own solutions in the discussions section of Leetcode, I won't necessarily be posting the optimal solution.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Question
&lt;/h2&gt;

&lt;p&gt;(Copy Pasted From Leetcode)&lt;/p&gt;

&lt;p&gt;Given a column title as appear in an Excel sheet, return its corresponding column number.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    A -&amp;gt; 1
    B -&amp;gt; 2
    C -&amp;gt; 3
    ...
    Z -&amp;gt; 26
    AA -&amp;gt; 27
    AB -&amp;gt; 28 
    ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Example 1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: "A"
Output: 1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Example 2:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: "AB"
Output: 28
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Example 3:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: "ZY"
Output: 701
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Constraints:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1 &amp;lt;= s.length &amp;lt;= 7&lt;/li&gt;
&lt;li&gt;s consists only of uppercase English letters.&lt;/li&gt;
&lt;li&gt;s is between "A" and "FXSHRXW".&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  My Approach(es)
&lt;/h2&gt;

&lt;p&gt;I won't go over all the code for all attempts but I will explain my approach(es) qualitatively.&lt;/p&gt;

&lt;h3&gt;
  
  
  Attempt 1 - Treat the string as a base 26 number
&lt;/h3&gt;

&lt;h5&gt;
  
  
  (Submission - Accepted)
&lt;/h5&gt;

&lt;p&gt;After trying out some examples by hand, I realized that this column naming system is basically a base 26 number. The notable difference is that instead of having a zero digit, we start at 1 with A and end at 26 with Z. Then, 27 resets to AA, which is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt; 27 = 1*26 + 1*1 
 27 = A*(26^1) + A*(26^0)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Similarly, ZY, which is 701, can be broken down as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt; 701 = 26*26 + 25*1 
 701 = Z*(26^1) + Y*(26^0)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Even without the zero digit, we can be fairly confident in our conversion system. The numbers start counting at 26 to the zeroth power, just like how other number bases start at the zeroth power. &lt;/p&gt;

&lt;p&gt;With this we can write our Javascript code, which starts at the right side of the string and starts iterating the powers of 26. I used a dictionary to convert the individual letters to numbers.&lt;/p&gt;

&lt;p&gt;Submitted Code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var titleToNumber = function(s) {
    // s is a string, but basically converts to a number in base 26 
    // also instead of zero we have 26 
    const dict = {
        A: 1, B: 2, C: 3, D: 4, E: 5, F: 6, G: 7, H: 8, I: 9, J: 10, K: 11, L: 12, M: 13, N: 14,
        O: 15, P: 16, Q: 17, R: 18, S: 19, T: 20, U: 21, V: 22, W: 23, X: 24, Y: 25, Z: 26
    }
    let number = 0;
    let power = 0;
    for (let i = s.length-1; i &amp;gt;= 0; i--) {
        number += Math.pow(26, power)*dict[s[i]];
        power ++;
    }
    return number;
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Attempt 1A - Still Treating the string as a base 26 number
&lt;/h3&gt;

&lt;h5&gt;
  
  
  (Submission - Accepted)
&lt;/h5&gt;

&lt;p&gt;I just wanted to try writing this but reading the string's digits from left to right instead of right to left. Instead of adding the correct power of 26, this iterative method takes the previous number and multiplies all of it by 26 (because it's all numbers from left to right so far), and then adds the specific digit.&lt;/p&gt;

&lt;p&gt;Submitted Code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var titleToNumber = function(s) {
    // s is a string, but basically converts to a number in base 26 
    // also instead of zero we have 26 
    const dict = {
        A: 1, B: 2, C: 3, D: 4, E: 5, F: 6, G: 7, H: 8, I: 9, J: 10, K: 11, L: 12, M: 13, N: 14,
        O: 15, P: 16, Q: 17, R: 18, S: 19, T: 20, U: 21, V: 22, W: 23, X: 24, Y: 25, Z: 26
    }
    let number = 0;

    for (let i = 0; i &amp;lt; s.length; i++) {
        number = number*26 + dict[s[i]];
    }
    return number;
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Discussion and Conclusions
&lt;/h2&gt;

&lt;p&gt;There is not much I want to say about this question. Once the naming/numbering system is understood, the conversion can be done as a numerical base 26 conversion, with the added step of parsing the string. Even though I'm sure there are ways to optimize the code, I believe this is sufficient understanding of the problem. &lt;/p&gt;

&lt;p&gt;The time complexity is O(n) for n equal to the length of string s, but the length of s is constrained to be less than or equal to 7.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>challenge</category>
    </item>
    <item>
      <title>Leetcode Daily - Rotting Oranges</title>
      <dc:creator>Andrew Hsu</dc:creator>
      <pubDate>Mon, 10 Aug 2020 05:41:25 +0000</pubDate>
      <link>https://dev.to/drewhsu86/leetcode-daily-rotting-oranges-108d</link>
      <guid>https://dev.to/drewhsu86/leetcode-daily-rotting-oranges-108d</guid>
      <description>&lt;h1&gt;
  
  
  Leetcode Daily - August 9, 2020
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Rotting Oranges
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/550/week-2-august-8th-august-14th/3418/"&gt;Link to Leetcode Question&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lately I've been grinding Leetcode and decided to record some of my thoughts on this blog. This is both to help me look back on what I've worked on as well as help others see how one might think about the problems.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;However, since many people post their own solutions in the discussions section of Leetcode, I won't necessarily be posting the optimal solution.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Question
&lt;/h2&gt;

&lt;p&gt;(Copy Pasted From Leetcode)&lt;/p&gt;

&lt;p&gt;In a given grid, each cell can have one of three values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the value 0 representing an empty cell;&lt;/li&gt;
&lt;li&gt;the value 1 representing a fresh orange;&lt;/li&gt;
&lt;li&gt;the value 2 representing a rotten orange.
Every minute, any fresh orange that is adjacent (4-directionally) to a rotten orange becomes rotten.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Return the minimum number of minutes that must elapse until no cell has a fresh orange.  If this is impossible, return -1 instead.&lt;/p&gt;

&lt;p&gt;Example 1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: [[2,1,1],[1,1,0],[0,1,1]]
Output: 4
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Example 2:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: [[2,1,1],[0,1,1],[1,0,1]]
Output: -1
Explanation:  The orange in the bottom left corner (row 2, column 0) is never rotten, because rotting only happens 4-directionally.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Example 3:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: [[0,2]]
Output: 0
Explanation:  Since there are already no fresh oranges at minute 0, the answer is just 0.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 &amp;lt;= grid.length &amp;lt;= 10
1 &amp;lt;= grid[0].length &amp;lt;= 10
grid[i][j] is only 0, 1, or 2.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  My Approach(es)
&lt;/h2&gt;

&lt;p&gt;I won't go over all the code for all attempts but I will explain my approach(es) qualitatively.&lt;/p&gt;

&lt;h3&gt;
  
  
  Attempt 1 - Brute Force (iterate out the grids)
&lt;/h3&gt;

&lt;h5&gt;
  
  
  (Submission - Accepted)
&lt;/h5&gt;

&lt;p&gt;My basic thought process focused on whether I could determine if the rotting oranges would reach all the fresh oranges on the grid. I realized that if the board didn't change between one minute and the next, that it wouldn't change at all anymore, because rotten oranges only spread to adjacent fresh oranges.&lt;/p&gt;

&lt;p&gt;My brute force approach simply iterates the grid minute by minute by checking all the fresh oranges for adjacency to rotten oranges and then changing them from 1 to 2. I decided to use a while loop with a counter that counts the number of minutes elapsed (number of iterations).&lt;/p&gt;

&lt;p&gt;I used a while loop but I realized there were two ending conditions: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The counter amount should be returned if the grid has no fresh oranges left. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The value -1 should return instead if the grid does not change from iteration N to iteration N+1 (but there are still fresh oranges left).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The only other complication is making copies of the grid so that I could check if the new grid (after rotten orange spread has been calculated) is the same as the current grid (before the rotten oranges spread for that minute). In Javascript, assigning the arrays equal to each other would make them reference the same array, so they would always end up equal. I wrote some helper functions in my code but end up with a pretty cumbersome implementation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;orangesRotting&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;grid&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;currGrid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;copyGrid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;grid&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;minutes&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="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// check currentGrid for fresh (1)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;hasFresh2D&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currGrid&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;minutes&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;newGrid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;copyGrid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currGrid&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;// process the new Grid for rottenness &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;newGrid&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;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="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;j&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;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;newGrid&lt;/span&gt;&lt;span class="p"&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;length&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="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="c1"&gt;// check 4 directions of a 1 for 2's, if true change it to a 2 &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;newGrid&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&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="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&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;i&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;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;currGrid&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="mi"&gt;1&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="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="nx"&gt;newGrid&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&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="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&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;j&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;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;currGrid&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&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="nx"&gt;newGrid&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&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="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&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;i&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;&lt;/span&gt; &lt;span class="nx"&gt;newGrid&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;currGrid&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="mi"&gt;1&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="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="nx"&gt;newGrid&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&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="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&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;j&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;&lt;/span&gt; &lt;span class="nx"&gt;newGrid&lt;/span&gt;&lt;span class="p"&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;length&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;currGrid&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&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="nx"&gt;newGrid&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&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="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="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// check if no fresh oranges get rotten &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;allEqual2D&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currGrid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;newGrid&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// cannot get more oranges rotten &lt;/span&gt;
            &lt;span class="c1"&gt;// because no change &lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="nx"&gt;minutes&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt; 
        &lt;span class="nx"&gt;currGrid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;copyGrid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newGrid&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;copyGrid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arrs1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;result&lt;/span&gt; &lt;span class="o"&gt;=&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;arrs1&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;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="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arrs1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;slice&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;result&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;allEqual2D&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arrs1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;arrs2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// assume the lengths are equal, just want to check values &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;arrs1&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;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="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;j&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;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;arrs1&lt;/span&gt;&lt;span class="p"&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;length&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="p"&gt;)&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;arrs1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&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="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;arrs2&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&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="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&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="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&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;hasFresh2D&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arrs1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&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;arrs1&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;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="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;j&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;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;arrs1&lt;/span&gt;&lt;span class="p"&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;length&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="p"&gt;)&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;arrs1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&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="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&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="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Discussion and Conclusions
&lt;/h2&gt;

&lt;p&gt;Some of my notable thoughts about my implementation can be summarized as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Can I use a different data structure to store the grids instead of a 2D array?&lt;/li&gt;
&lt;li&gt;Are there better indicators or ways of calculating the ending conditions? For example, Using a search algorithm starting from every rotten orange to every fresh orange. The search algorithms would also calculate the manhattan grid distances and the maximum value would be returned unless theres are fresh oranges that cannot be reached (if at least one exists, return -1). &lt;/li&gt;
&lt;li&gt;I believe the algorithm I used is O(n&lt;sup&gt;4&lt;/sup&gt;), if n is the length or width of the grid. That is because I check the entire grid once per iteration (O(n&lt;sup&gt;2&lt;/sup&gt;), and for a rotten orange to spread from one corner to a fresh orange in the other it whose maximum path (if zigzagging) can take around half the boxes of the grid (and the size of the grid is O(n&lt;sup&gt;2&lt;/sup&gt;). But luckily, n is constrained to 10, and n&lt;sup&gt;4&lt;/sup&gt; is 10,000, so investigating better algorithms for dealing with grids could pay off.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Leetcode Daily - Path Sum III</title>
      <dc:creator>Andrew Hsu</dc:creator>
      <pubDate>Sun, 09 Aug 2020 02:10:12 +0000</pubDate>
      <link>https://dev.to/drewhsu86/leetcode-daily-path-sum-iii-2nbb</link>
      <guid>https://dev.to/drewhsu86/leetcode-daily-path-sum-iii-2nbb</guid>
      <description>&lt;h1&gt;
  
  
  Leetcode Daily - August 8, 2020
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Path Sum III
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/550/week-2-august-8th-august-14th/3417/"&gt;Link to Leetcode Question&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lately I've been grinding Leetcode and decided to record some of my thoughts on this blog. This is both to help me look back on what I've worked on as well as help others see how one might think about the problems.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;However, since many people post their own solutions in the discussions section of Leetcode, I won't necessarily be posting the optimal solution.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Question
&lt;/h2&gt;

&lt;p&gt;(Copy Pasted From Leetcode)&lt;/p&gt;

&lt;p&gt;You are given a binary tree in which each node contains an integer value.&lt;/p&gt;

&lt;p&gt;Find the number of paths that sum to a given value.&lt;/p&gt;

&lt;p&gt;The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).&lt;/p&gt;

&lt;p&gt;The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8

      10
     /  \
    5   -3
   / \    \
  3   2   11
 / \   \
3  -2   1

Return 3. The paths that sum to 8 are:

1.  5 -&amp;gt; 3
2.  5 -&amp;gt; 2 -&amp;gt; 1
3. -3 -&amp;gt; 11
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  My Approach(es)
&lt;/h2&gt;

&lt;p&gt;I won't go over all the code for all attempts but I will explain my approach(es) qualitatively.&lt;/p&gt;

&lt;h3&gt;
  
  
  Attempt 1 - DFS or BFS
&lt;/h3&gt;

&lt;h5&gt;
  
  
  (Submission - Accepted)
&lt;/h5&gt;

&lt;p&gt;After looking at the expected answer of the example, I was a little worried. For a given long path, I thought about the complexity of finding all the combination of paths and then their sums. For example, from root to the bottom left is &lt;code&gt;10 -&amp;gt; 5 -&amp;gt; 3 -&amp;gt; 3&lt;/code&gt;. There are 4 + 3 + 2 + 1 = 10 paths, because there is one path of length 4, two of length 3, three of length 2 and four of length 1. &lt;/p&gt;

&lt;p&gt;However, I thought about how to record these path sums and realized that recording the whole path then checking the sum of all combinations was a bit cumbersome. If a binary tree has k levels, the path would require storage of O(k), and the time complexity becomes O(k^2) combinations per check.&lt;/p&gt;

&lt;p&gt;I decided to store the sums of all paths leading to the current node. Each node would have to do a calculation of adding its own value to this array, but also push its value to the array to represent the path starting and ending at itself. If a binary has k levels, then this array's storage and the adding operation's time complexity should both be O(k).&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;pathSum&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;root&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// dfs or bfs while storing a sums array &lt;/span&gt;
    &lt;span class="c1"&gt;// when it reaches a certain node, add val to all in the sums array then push val &lt;/span&gt;
    &lt;span class="c1"&gt;// finally, if any paths are equal to sum then counter ++ &lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;counter&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;stack&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="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;sums&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="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&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;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&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;currNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// process sums array &lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newSums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;currNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sums&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;s&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;currNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;newSums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;newSums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&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;s&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nx"&gt;counter&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="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;currNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;({...&lt;/span&gt;&lt;span class="nx"&gt;currNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;sums&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;newSums&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;currNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;({...&lt;/span&gt;&lt;span class="nx"&gt;currNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;sums&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;newSums&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;counter&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Discussion and Conclusions
&lt;/h2&gt;

&lt;p&gt;I've done a lot of binary tree problems on Leetcode now and was able to complete a majority of them using depth and breadth first search. In this case, I haven't thought of a good idea to map this binary tree into another data structure, but instead how to calculate instances of the path sum while traversing the existing data structure. I will try to explore and keep in mind other ways to approach this problem.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>challenge</category>
    </item>
    <item>
      <title>Leetcode Daily - Vertical Order Traversal Of Binary Tree</title>
      <dc:creator>Andrew Hsu</dc:creator>
      <pubDate>Fri, 07 Aug 2020 21:40:47 +0000</pubDate>
      <link>https://dev.to/drewhsu86/leetcode-daily-vertical-order-traversal-of-binary-tree-3d8j</link>
      <guid>https://dev.to/drewhsu86/leetcode-daily-vertical-order-traversal-of-binary-tree-3d8j</guid>
      <description>&lt;h1&gt;
  
  
  Leetcode Daily - August 7, 2020
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Vertical Order Traversal Of Binary Tree
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/549/week-1-august-1st-august-7th/3415/"&gt;Link to Leetcode Question&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lately I've been grinding Leetcode and decided to record some of my thoughts on this blog. This is both to help me look back on what I've worked on as well as help others see how one might think about the problems.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;However, since many people post their own solutions in the discussions section of Leetcode, I won't necessarily be posting the optimal solution.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Question
&lt;/h2&gt;

&lt;p&gt;(Copy Pasted From Leetcode)&lt;/p&gt;

&lt;p&gt;Given a binary tree, return the vertical order traversal of its nodes values.&lt;/p&gt;

&lt;p&gt;For each node at position (X, Y), its left and right children respectively will be at positions (X-1, Y-1) and (X+1, Y-1).&lt;/p&gt;

&lt;p&gt;Running a vertical line from X = -infinity to X = +infinity, whenever the vertical line touches some nodes, we report the values of the nodes in order from top to bottom (decreasing Y coordinates).&lt;/p&gt;

&lt;p&gt;If two nodes have the same position, then the value of the node that is reported first is the value that is smaller.&lt;/p&gt;

&lt;p&gt;Return an list of non-empty reports in order of X coordinate.  Every report will have a list of values of nodes.&lt;/p&gt;

&lt;p&gt;Example 1:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UXifho3n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://assets.leetcode.com/uploads/2019/01/31/1236_example_1.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UXifho3n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://assets.leetcode.com/uploads/2019/01/31/1236_example_1.PNG" alt="Example 1 Binary Tree"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: [3,9,20,null,null,15,7]
Output: [[9],[3,15],[20],[7]]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;br&gt;
Without loss of generality, we can assume the root node is at position (0, 0):&lt;br&gt;
Then, the node with value 9 occurs at position (-1, -1);&lt;br&gt;
The nodes with values 3 and 15 occur at positions (0, 0) and (0, -2);&lt;/p&gt;

&lt;p&gt;The node with value 20 occurs at position (1, -1);&lt;br&gt;
The node with value 7 occurs at position (2, -2).&lt;/p&gt;

&lt;p&gt;Example 2:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3SaW0Ivx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://assets.leetcode.com/uploads/2019/01/31/tree2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3SaW0Ivx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://assets.leetcode.com/uploads/2019/01/31/tree2.png" alt="Example 2 Binary Tree"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: [1,2,3,4,5,6,7]
Output: [[4],[2],[1,5,6],[3],[7]]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;br&gt;
The node with value 5 and the node with value 6 have the same position according to the given scheme.&lt;br&gt;
However, in the report "[1,5,6]", the node value of 5 comes first since 5 is smaller than 6.&lt;/p&gt;

&lt;p&gt;Notes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The tree will have between 1 and 1000 nodes.&lt;/li&gt;
&lt;li&gt;Each node's value will be between 0 and 1000.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  My Approach(es)
&lt;/h2&gt;

&lt;p&gt;I won't go over all the code for all attempts but I will explain my approach(es) qualitatively.&lt;/p&gt;
&lt;h3&gt;
  
  
  Attempt 1 - DFS or BFS
&lt;/h3&gt;
&lt;h5&gt;
  
  
  (Submission - Accepted)
&lt;/h5&gt;

&lt;p&gt;Based on the detailed instructions, I understand that each node will have its own X and Y coordinate (relative to the root node's). With this understanding, I used depth first search to search the binary tree and add each node to a data structure. I used a Javascript object, which has hash table characteristics, to hold data about each node. &lt;/p&gt;

&lt;p&gt;Since we need to return a data structure which sorts the nodes by X value, I decided to sort them by the X value when I add them to the container. &lt;/p&gt;

&lt;p&gt;I wanted my container to look something like this after it's been filled in (using Example 1's values):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;xCoordMap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;-1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="na"&gt;val&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}],&lt;/span&gt;
   &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;0&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="na"&gt;val&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;val&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="na"&gt;val&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}],&lt;/span&gt;
   &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="na"&gt;val&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;y&lt;/span&gt;&lt;span class="p"&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Originally I tried to use breadth first search because that search algorithm searches through all the nodes of the same Y level consecutively. However, when there is a tie in Y value, the question wants the lower node value to be placed first. So I ended up recording the Y values in order to detect ties, and then sorted them first by highest Y value and then by lowest value (if the Y values are tied).&lt;/p&gt;

&lt;p&gt;Submitted Javascript code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;verticalTraversal&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;root&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// dfs or bfs but calculate the coordinates while running it &lt;/span&gt;
    &lt;span class="c1"&gt;// using bfs we won't have to use the Y values to sort (?)&lt;/span&gt;
    &lt;span class="c1"&gt;// left goes to X-1, Y-1, right goes to X+1, Y-1 &lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[{...&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;y&lt;/span&gt;&lt;span class="p"&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;xCoordMap&lt;/span&gt; &lt;span class="o"&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;addNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&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;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// if the key, 'x', already exists, push it &lt;/span&gt;
        &lt;span class="c1"&gt;// if it doesn't, make a new array &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;xCoordMap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;xCoordMap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;xCoordMap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&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;val&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&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="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stack&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;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&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;currNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="nx"&gt;addNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;currNode&lt;/span&gt;&lt;span class="p"&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;currNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;y&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;currNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;({...&lt;/span&gt;&lt;span class="nx"&gt;currNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;currNode&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;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;currNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;});&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;currNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;({...&lt;/span&gt;&lt;span class="nx"&gt;currNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;currNode&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;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;currNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&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="c1"&gt;// we have an object with numbered keys and arrays of values &lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sortedKeys&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;xCoordMap&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&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;vertArrays&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sortedKeys&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;key&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="c1"&gt;// sort the array then return it with only vals, not x and y &lt;/span&gt;
       &lt;span class="c1"&gt;// sort by y first, then by value if y's are the same &lt;/span&gt;
       &lt;span class="nx"&gt;xCoordMap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&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;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;y&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;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
           &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&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;xCoordMap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&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;obj&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&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;vertArrays&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;h2&gt;
  
  
  Discussion and Conclusions
&lt;/h2&gt;

&lt;p&gt;I really focused on putting node data into a data structure and sorting it by the X value right when it is added. I felt that this is faster than any method which searches first and then sorts later. Especially because I am using a hash table to store the nodes with the same X value.&lt;/p&gt;

&lt;p&gt;However, I could've done some more thinking on sorting the arrays of my node container by the Y value and node value upon adding, instead of before returning. I believe I could've done it in O(n) if I tried to put new nodes in the right spot when adding them, instead of the O(nlog(n)) sorting that I did to return the solution.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>challenge</category>
    </item>
    <item>
      <title>Leetcode Daily - Find all duplicates in an array</title>
      <dc:creator>Andrew Hsu</dc:creator>
      <pubDate>Thu, 06 Aug 2020 17:42:51 +0000</pubDate>
      <link>https://dev.to/drewhsu86/leetcode-daily-find-all-duplicates-in-an-array-23m8</link>
      <guid>https://dev.to/drewhsu86/leetcode-daily-find-all-duplicates-in-an-array-23m8</guid>
      <description>&lt;h1&gt;
  
  
  Leetcode Daily - August 6, 2020
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Find All Duplicates In An Array
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/549/week-1-august-1st-august-7th/3414/"&gt;Link to Leetcode Question&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lately I've been grinding Leetcode and decided to record some of my thoughts on this blog. This is both to help me look back on what I've worked on as well as help others see how one might think about the problems.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;However, since many people post their own solutions in the discussions section of Leetcode, I won't necessarily be posting the optimal solution.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Question
&lt;/h2&gt;

&lt;p&gt;(Copy Pasted From Leetcode)&lt;/p&gt;

&lt;p&gt;Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.&lt;/p&gt;

&lt;p&gt;Find all the elements that appear twice in this array.&lt;/p&gt;

&lt;p&gt;Could you do it without extra space and in O(n) runtime?&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input:
[4,3,2,7,8,2,3,1]

Output:
[2,3]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  My Approach(es)
&lt;/h2&gt;

&lt;p&gt;I won't go over all the code for all attempts but I will explain my approach(es) qualitatively.&lt;/p&gt;

&lt;h3&gt;
  
  
  Attempt 1 - store seen numbers in a hash table
&lt;/h3&gt;

&lt;h5&gt;
  
  
  (Submission - Accepted)
&lt;/h5&gt;

&lt;p&gt;I thought about using techniques such as sorting the array, with time complexity of O(nlog(n)), but decided to utilize hash tables which take virtually O(1) to retrieve values if given a key.&lt;/p&gt;

&lt;p&gt;Since we are looking for duplicates, we only need to traverse the array once (in O(n) time) and check if we have seen a number before. If we haven't seen it, we add it to our hash table. If we have seen it, we add it to an array holding numbers we've identified as duplicates. Then we can return the array of duplicates to complete the function.&lt;/p&gt;

&lt;p&gt;Javascript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;findDuplicates&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="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// use a hash table to store numbers we've seen &lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;hash&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="c1"&gt;// now go through the array once &lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;dupes&lt;/span&gt; &lt;span class="o"&gt;=&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;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;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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hash&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;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;()])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// if it is a duplicate, add it to dupes&lt;/span&gt;
            &lt;span class="nx"&gt;dupes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&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;i&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// if it is not a duplicate, add it to hash &lt;/span&gt;
            &lt;span class="nx"&gt;hash&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;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;()]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&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;dupes&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Attempt 2 - try and not use extra space for O(n) solution
&lt;/h3&gt;

&lt;h5&gt;
  
  
  (Submission - Accepted)
&lt;/h5&gt;

&lt;p&gt;I tried to take advantage of the fact that the numbers are only integers between 1 and the length of the array, N. Therefore, it can map one to one with the indices of the array unless there are duplicates. &lt;/p&gt;

&lt;p&gt;For example, the number 1 belongs in index 0, so the number k belongs in index k - 1.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;I first create a container to hold duplicates. &lt;/li&gt;
&lt;li&gt;I then iterate through the array. 

&lt;ul&gt;
&lt;li&gt;If the number i is in index i-1, I leave it alone &lt;/li&gt;
&lt;li&gt;If the number i is not in index i-1:&lt;/li&gt;
&lt;li&gt;I check index i-1:

&lt;ul&gt;
&lt;li&gt;If index i-1 holds number i: this number i is a duplicate&lt;/li&gt;
&lt;li&gt;If index i-1 doesn't hold number i, I swap the numbers so that i is now in index i-1&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;After the swap I check the current index again using the same criteria. I continue to swap until current index either:

&lt;ul&gt;
&lt;li&gt;Holds the right number &lt;/li&gt;
&lt;li&gt;Is a dupe because index i-1 holds i&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Finally, continue to next index and repeat i. and ii.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="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;findDuplicates&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="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// try to match the numbers to index = nums[i]-1&lt;/span&gt;

    &lt;span class="c1"&gt;// now go through the array once &lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;dupes&lt;/span&gt; &lt;span class="o"&gt;=&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;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;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="k"&gt;if&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;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!==&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;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// is the number in the right place? &lt;/span&gt;
            &lt;span class="c1"&gt;// for example: 1 belongs in index 0&lt;/span&gt;
            &lt;span class="c1"&gt;// if so, ignore. if not, try to place all &lt;/span&gt;
            &lt;span class="c1"&gt;// numbers in a chain correctly&lt;/span&gt;

            &lt;span class="c1"&gt;// nums[i] belongs in index nums[i]-1&lt;/span&gt;
            &lt;span class="c1"&gt;// try to put it in index nums[i]-1 &lt;/span&gt;
            &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newIndex&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;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&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;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;newIndex&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;i&lt;/span&gt;&lt;span class="p"&gt;])&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="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;dupes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;includes&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;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;dupes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&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;i&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="k"&gt;else&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;temp&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;i&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;i&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;newIndex&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;newIndex&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;temp&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="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;dupes&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I may not be allowed to store the duplicates in an extra array if I wanted to meet the challenge of using no extra storage space as well as having O(n) time complexity. However, I would iterate through the array again and splice out any numbers that were not in their assigned positions (number k would be in index k-1) then return the spliced array.&lt;/p&gt;

&lt;h2&gt;
  
  
  Discussion and Conclusions
&lt;/h2&gt;

&lt;p&gt;Using hash tables (in the case of Javascript, Objects) to quickly access data is a powerful tool that I was first introduced to in the first problem I ever tried on Leetcode, &lt;a href="https://leetcode.com/problems/two-sum/"&gt;Two Sums&lt;/a&gt;. By using the hash table instead of a nested iterative loop, we can frequently reduce O(n&lt;sup&gt;2&lt;/sup&gt;) tasks to O(n).&lt;/p&gt;

&lt;p&gt;I still need to think about how to do this problem without using extra space. I tried to use the fact that all numbers integers from 1 to the length of the array, N. My implementation spent time during the for loop (O(n) operation) to reassign values to where they would be, and swapping places with the number in the correct position if it's not a duplicate. I think this approach is still O(n) because there aren't an infinite numbers to swap, since you would stop swapping if the number were in the right position. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>challenge</category>
    </item>
    <item>
      <title>Leetcode Daily - Power of Four</title>
      <dc:creator>Andrew Hsu</dc:creator>
      <pubDate>Wed, 05 Aug 2020 19:59:03 +0000</pubDate>
      <link>https://dev.to/drewhsu86/leetcode-daily-power-of-four-kp9</link>
      <guid>https://dev.to/drewhsu86/leetcode-daily-power-of-four-kp9</guid>
      <description>&lt;h1&gt;
  
  
  Leetcode Daily - August 4, 2020
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Power of Four
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/549/week-1-august-1st-august-7th/3412/"&gt;Link to the Leetcode Question&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lately I've been grinding Leetcode and decided to record some of my thoughts on this blog. This is both to help me look back on what I've worked on as well as help others see how one might think about the problems.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;However, since many people post their own solutions in the discussions section of Leetcode, I won't necessarily be posting the optimal solution.&lt;/strong&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Question
&lt;/h2&gt;

&lt;p&gt;(Copy Pasted from Leetcode) &lt;/p&gt;

&lt;p&gt;Given an integer (signed 32 bits), write a function to check whether it is a power of 4.&lt;/p&gt;

&lt;p&gt;Example 1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: 16
Output: true
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Example 2:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: 5
Output: false
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Follow up:&lt;/strong&gt; Could you solve it without loops/recursion?&lt;/p&gt;

&lt;h2&gt;
  
  
  My Approach(es)
&lt;/h2&gt;

&lt;p&gt;I won't go over all the code for all attempts but I will explain my approach(es) qualitatively.&lt;/p&gt;

&lt;h3&gt;
  
  
  Attempt 1 - while loop
&lt;/h3&gt;

&lt;h5&gt;
  
  
  (Did not submit, only pseudocoded)
&lt;/h5&gt;

&lt;p&gt;I believe most people understand how to iteratively solve this problem. The two possible ways to use the iterative method are: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Via multiplication
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="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;isPowerOfFour&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&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;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&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;counter&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="mi"&gt;4&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;counter&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; 
 &lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Via division
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="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;isPowerOfFour&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&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;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;num&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;counter&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;/=&lt;/span&gt; &lt;span class="mi"&gt;4&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;counter&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; 
 &lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;These methods are O(log(n)) because the number of iterations would basically be the power of 4 that the number is closest to. &lt;/p&gt;

&lt;h3&gt;
  
  
  Attempt 2 - Math.log
&lt;/h3&gt;

&lt;h5&gt;
  
  
  (Submission - Accepted)
&lt;/h5&gt;

&lt;p&gt;For those that are not familiar with the logarithm mathematical function, please refer to &lt;a href="https://dev.to/drewhsu86/simple-introduction-to-logarithms-29na"&gt;my post about it, here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Basically, if &lt;strong&gt;4&lt;sup&gt;N&lt;/sup&gt; = num&lt;/strong&gt;, then &lt;strong&gt;log4(num) = N&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We can also use the &lt;a href="https://www.purplemath.com/modules/logrules5.htm"&gt;mathematical theorem that converts logarithms to a common basis&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;logB(A) = logx(A) / logx(B)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;where &lt;strong&gt;x&lt;/strong&gt; is a common basis. We will simply use base 10 since most log functions are base 10. &lt;/p&gt;

&lt;p&gt;By understanding that powers of 4 will only have &lt;strong&gt;log4(num) = N&lt;/strong&gt; where &lt;strong&gt;N&lt;/strong&gt; are whole numbers, we can write a very simple function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;isPowerOfFour&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&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="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&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;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After submitting this solution I noticed the run time was a bit long, 140ms. I think it might be that Math.log uses an iterative method to calculate the log accurately but that's required for this solution so the method I found (discussed in the Conclusions Section could be a more computationally efficient alternative.&lt;/p&gt;

&lt;h2&gt;
  
  
  Discussion and Conclusions &lt;a&gt;
&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;I felt very satisfied with solution, but looked for other interesting solutions. &lt;a href="https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/549/week-1-august-1st-august-7th/3412/discuss/772269/Python-O(1)-oneliner-solution-explained"&gt;Here is one&lt;/a&gt; that uses some programming knowledge as well as mathematical knowledge. &lt;/p&gt;

&lt;p&gt;First of all, since the question is asking about a power of 4, which is a power of 2, it has a significant property in binary, which is that it is a &lt;strong&gt;one&lt;/strong&gt; with all &lt;strong&gt;zeros&lt;/strong&gt; to the right of it. Not only that, it must be an even number of zeros since the odd powers of 2, such as 8 = 2&lt;sup&gt;3&lt;/sup&gt;, would have an odd number of zeros (3 zeros for the number 8).&lt;/p&gt;

&lt;p&gt;This solution also takes advantage of the fact that the programmer can know the maximum size of the number in bits, which allows them to use a bit mask and therefore not have to write iteration in a higher level language. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>challenge</category>
    </item>
    <item>
      <title>Leetcode Daily - Add and Search Data Structure</title>
      <dc:creator>Andrew Hsu</dc:creator>
      <pubDate>Wed, 05 Aug 2020 19:28:41 +0000</pubDate>
      <link>https://dev.to/drewhsu86/leetcode-daily-add-and-search-data-structure-5448</link>
      <guid>https://dev.to/drewhsu86/leetcode-daily-add-and-search-data-structure-5448</guid>
      <description>&lt;h1&gt;
  
  
  Leetcode Daily - August 5, 2020
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Add and Search Data Structure
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/549/week-1-august-1st-august-7th/3413/"&gt;Link to the Leetcode Question&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lately I've been grinding Leetcode and decided to record some of my thoughts on this blog. This is both to help me look back on what I've worked on as well as help others see how one might think about the problems. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;However, since many people post their own solutions in the discussions section of Leetcode, I won't necessarily be posting the optimal solution.&lt;/strong&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Question
&lt;/h2&gt;

&lt;p&gt;(Copy Pasted From Leetcode)&lt;/p&gt;

&lt;p&gt;Design a data structure that supports the following two operations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void addWord(word)
bool search(word)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;addWord("bad")
addWord("dad")
addWord("mad")
search("pad") -&amp;gt; false
search("bad") -&amp;gt; true
search(".ad") -&amp;gt; true
search("b..") -&amp;gt; true
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt;&lt;br&gt;
You may assume that all words are consist of lowercase letters a-z.&lt;/p&gt;

&lt;h2&gt;
  
  
  My Approach(es)
&lt;/h2&gt;

&lt;p&gt;I won't go over all the code for all attempts but I will explain my approach(es) qualitatively. &lt;/p&gt;

&lt;h3&gt;
  
  
  Attempt 1 - Store words in an array
&lt;/h3&gt;

&lt;h5&gt;
  
  
  (Submission - Time Exceeded)
&lt;/h5&gt;

&lt;p&gt;I added wordList Array to the WordDictionary class. Then the &lt;strong&gt;add&lt;/strong&gt; method simply pushes the word to the Array. I even checked if the Array already included the word to make sure I didn't have duplicates. &lt;/p&gt;

&lt;p&gt;This made the &lt;strong&gt;search&lt;/strong&gt; method pretty challenging. If an exact word is given, I could simply use Array.includes. But for optional letters indicated by the &lt;strong&gt;'.'&lt;/strong&gt;, I had to first iterate through an array then iterate through the letters of the word. There is probably a smarter way to do this, for example, with regex, but I ended up getting 'time exceeded' on my submission. &lt;/p&gt;

&lt;h3&gt;
  
  
  Attempt 2 - Changing data structure to a Hash table
&lt;/h3&gt;

&lt;h5&gt;
  
  
  (Submission - Accepted)
&lt;/h5&gt;

&lt;p&gt;Since Hash tables are virtually O(1) for data retrieval I decided I would stop using the Array and use a Hash table. &lt;/p&gt;

&lt;p&gt;However, simply using the word as the key would not make it any easier to retrieve words that had to match optional letters, &lt;strong&gt;'.'&lt;/strong&gt;. I decided to refactor my data structure to make searching easier, but adding would be slightly harder. &lt;/p&gt;

&lt;p&gt;My data structure is a hash table that has the following key value pairs: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;key - length of word (maybe prepended with 'len' or another string)&lt;/li&gt;
&lt;li&gt;value - Array of words&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;My add function checks if the key named &lt;code&gt;len${word.length}&lt;/code&gt; exists, and if it does, it pushes the word to the Array held by that key. If the key does not exist, it is created with the value &lt;code&gt;[word]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;My search function checks if the key named &lt;code&gt;len${word.length}&lt;/code&gt; exists, and if it does then it iterates through the array and tries to match the letters of the searched word. This is actually very similar to the original array implementation, but the array searched is potentially much smaller, unless all or most of the words in the dictionary are the same length.&lt;/p&gt;

&lt;p&gt;This ended up being my accepted submission.&lt;/p&gt;

&lt;h2&gt;
  
  
  Discussion and Conclusion
&lt;/h2&gt;

&lt;p&gt;I still have to do some studying on the optimal implementation of this problem. I feel that if I designed a better data structure then perhaps there could be a more efficient &lt;strong&gt;search&lt;/strong&gt; algorithm. But of course, there could potentially be added complexity to the &lt;strong&gt;add&lt;/strong&gt; algorithm.&lt;/p&gt;

&lt;p&gt;In my current solution, &lt;strong&gt;add&lt;/strong&gt; should still be O(1) if you ignore any complexities of using the Array.push method. And, &lt;strong&gt;search&lt;/strong&gt; should still be O(n), because even though we broke down our storage data structure by word length, the amount of words stored and therefore the iterative search time still grows proportionally to the number of words added. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>challenge</category>
    </item>
    <item>
      <title>Simple Introduction to Logarithms for Complexity Analysis</title>
      <dc:creator>Andrew Hsu</dc:creator>
      <pubDate>Wed, 27 May 2020 18:41:24 +0000</pubDate>
      <link>https://dev.to/drewhsu86/simple-introduction-to-logarithms-29na</link>
      <guid>https://dev.to/drewhsu86/simple-introduction-to-logarithms-29na</guid>
      <description>&lt;h1&gt;
  
  
  Simple Introduction to Logarithms for Complexity Analysis
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;What is it?&lt;/li&gt;
&lt;li&gt;How it Relates to Complexity Analysis&lt;/li&gt;
&lt;li&gt;Example: Binary Search&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;I just wanted to write this post to help introduce (very SIMPLY) the concept of logarithms, especially because many programmers do not come from a math background. This post is not meant to get into all the possible things logarithms are good for, but just to understand what it IS and show how it is relevant in an algorithm's time (and space) complexity analysis.&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is it?
&lt;/h2&gt;

&lt;p&gt;Let's start from the basics. To introduce logarithms, we need to first introduce the idea of exponents, or a number to a certain 'power'.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;2&lt;sup&gt;5&lt;/sup&gt; = 32&lt;/p&gt;

&lt;p&gt;The superscript (smaller, higher up number) indicates the number of times you multiply the base number (regular font and position number) with itself. Therefore, we can calculate 2 x 2 x 2 x 2 x 2 = 32.&lt;/p&gt;

&lt;p&gt;This can be useful for many things, such as for determining how much information can be stored digitally (see &lt;a href="https://en.wikipedia.org/wiki/Bit" rel="noopener noreferrer"&gt;bits&lt;/a&gt; for an introduction) or in computing compounding interest in finance.&lt;/p&gt;

&lt;p&gt;But just as we can build up the product of 2's, we may also want to know how many times we multiplied 2 together. For example, we already know that 32 is 2 multiplied together 5 times.&lt;/p&gt;

&lt;p&gt;We use the following notation for the logarithm:&lt;/p&gt;

&lt;p&gt;log2(32) = 5&lt;/p&gt;

&lt;p&gt;The subscript (smaller, lower number) is the base, which is still 2 in this example. The number we want to take the logarithm of is 32 (we want to find out how many times 2 multiplied together to get this number). The result is 5 because we started with this example, so we already know the answer.&lt;/p&gt;

&lt;p&gt;Exponentiation and logarithms are actually inverse functions. Assuming both operations use the same base value (2, or the number being multiplied), exponentiation takes a number and multiplies the base that many number of times. Logarithms, starting with that final number, find out how many times it was multiplied (the exponent).&lt;/p&gt;

&lt;p&gt;This means that we can feed our exponential into a logarithm and get the exponent used.&lt;/p&gt;

&lt;p&gt;log2(2&lt;sup&gt;x&lt;/sup&gt;) = x&lt;/p&gt;

&lt;p&gt;Basically, the logarithm should be thought of as the exponent detector or calculator. (Assuming we start with the same base; very important!)&lt;/p&gt;

&lt;h4&gt;
  
  
  Notes
&lt;/h4&gt;

&lt;p&gt;A couple of notes about logarithms before we continue:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Most of the time, we just see 'log', such as on a calculator. This is most likely log10, which is base 10.&lt;/li&gt;
&lt;li&gt;Logarithms have properties that allow conversion between different bases: &lt;a href="https://www.purplemath.com/modules/logrules5.htm" rel="noopener noreferrer"&gt;Formula to convert logarithm bases&lt;/a&gt;. This is really useful if you need to calculate log in a different base but only have a log (base 10) function (in your programming language or on a calculator).&lt;/li&gt;
&lt;li&gt;Sometimes you may see 'ln' or natural log. This is a logarithm in base &lt;strong&gt;e&lt;/strong&gt;, or &lt;a href="https://en.wikipedia.org/wiki/E_(mathematical_constant)" rel="noopener noreferrer"&gt;Euler's number&lt;/a&gt;. This is usually irrelevant for programmers and developers unless you are working with something related to mathematics but now you know what the ln button is on a graphing calculator.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How it Relates to Complexity Analysis
&lt;/h2&gt;

&lt;p&gt;Bored yet?&lt;/p&gt;

&lt;p&gt;Let's get to something more relevant. We know that there are many different ways algorithms can scale in time (and space) complexity, such as O(n), O(n&lt;sup&gt;2&lt;/sup&gt;), O(nlog(n)), etc.&lt;/p&gt;

&lt;p&gt;You may have seen a graph (with two curves) like the one below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FjoK0UXM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FjoK0UXM.png" alt="2^x and log2(x)"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I created this graph using &lt;a href="https://www.geogebra.org/graphing?lang=en" rel="noopener noreferrer"&gt;GeoGebra Graphing Calculator&lt;/a&gt;, a site where you can draw graphs using math formulae.&lt;/p&gt;

&lt;p&gt;These curves may already be familiar with you. The higher, puke green curve that increases to infinity is 2&lt;sup&gt;x&lt;/sup&gt;. The lower, dark teal curve is log2(x). In both cases, x is simply the value on the horizontal axis of the graph.&lt;/p&gt;

&lt;p&gt;Because exponentiation and logarithms are inverse functions, and we are using base 2 for both curves, we can observe a symmetry along the a diagonal line (whose slope is 1).&lt;/p&gt;

&lt;p&gt;From what we know about complexity analysis and big O notation, the exponential curve is really bad (increases towards infinity very fast) while the logarithm is very good (the slope tends to descend rather than ascend like the exponential). &lt;/p&gt;

&lt;p&gt;Now that we know what the logarithm is, and why it has that descending (but not fully reaching horizontal) slope, let's take a look at a case where we find logarithms in commonly seen algorithms.&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Example: Binary Search
&lt;/h2&gt;

&lt;p&gt;You may have seen binary search already but here is a summary:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Let's say you are given a sorted array of integers (ascending order). One way to find if a target value is held in the array is to look through every array index in order. But a more efficient way (especially for longer arrays) is to use binary search. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the array is sorted, you can check if the median index holds the target value. But if the median index's value is lower than the target, you know that you only need to search the upper half of the array. Similarly, if the median index's value is higher, you only need to search the lower half. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This can be repeated until base cases are reached, such as when the sub-array being searched is only a length of 1.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I won't discuss how to solve or implement binary search in any more detail here. I want to use the binary search algorithm to show how we can determine that (spoiler alert!) the time complexity is O(log(n)) where n is the length of the array being sorted.&lt;/p&gt;

&lt;p&gt;Let's use our trusty number 32 from the previous section. We already know that this is 2&lt;sup&gt;5&lt;/sup&gt;. We also know that when we implement binary search, we keep finding the median index (or the midpoint) between our starting and ending indices. This is because we want to rule out half of the search area each time (if we know the target is higher or lower, we know which side NOT to search).&lt;/p&gt;

&lt;p&gt;Since dividing by 2 is the inverse of multiplying by 2, this plays into our hands because exponentiation is simply multiplying a bunch of times. If our input array has 32 numbers to look through, we don't want to look through all 32. &lt;/p&gt;

&lt;p&gt;Let's see what we are actually binary searching through if we have an array of length 32 (indices 0 - 31).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Iteration 1: 

&lt;ul&gt;
&lt;li&gt;We check index 15 (average of 31 and 0 is 15.5, and we take the floor).&lt;/li&gt;
&lt;li&gt;If our target number is found on array[15], we can return out of the function. Else, we binary search the upper or lower half of the array.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Iteration 2:

&lt;ul&gt;
&lt;li&gt;Since the sub-array is only length 16, we can take the median of 0 and 15, which is 7 (floor of 7.5).&lt;/li&gt;
&lt;li&gt;If target found, return. Else, binary search an array of length 8.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Iteration 3: 

&lt;ul&gt;
&lt;li&gt;Array length 8&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Iteration 4: 

&lt;ul&gt;
&lt;li&gt;Array length 4&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Iteration 5: 

&lt;ul&gt;
&lt;li&gt;Array length 2&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Iteration 6:

&lt;ul&gt;
&lt;li&gt;Array length 1&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;"6 iterations!" you may say, thinking that I am actually not that great at math. But when you think about the powers of 2 and its correlation to the sub-array lengths, you will see that 1 is actually 2&lt;sup&gt;0&lt;/sup&gt;. And counting 0-5 actually gives you 6 items.&lt;/p&gt;

&lt;p&gt;Additionally, the exact amount isn't the most important thing in complexity analysis, but rather the scaling factor as the size of data increases. For example, 2&lt;sup&gt;10&lt;/sup&gt; is 1024. If you had an array of around 1000 numbers, your binary search would probably take 11 iterations in the worst case (0-10 is 11 items). But when you start comparing 11 iterations to the 1000 iterations an O(n) linear algorithm would take, the benefits start to become apparent.&lt;/p&gt;

&lt;p&gt;Also, even though most numbers are not exactly divisible by 2, as long as you are roughly dividing the array in half all the smaller pieces are either ignored (we know the target isn't in a certain half of the array) or break down to arrays of length 2 and 1 anyway. So the performance would be comparable to the next power of 2 not lower than the array length.&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;While not every programmer needs to know a ton of math, logarithms are seen frequently enough that I feel it should be introduced. They have many mathematical properties but most are not necessary to memorize for a programmer.&lt;/p&gt;

&lt;p&gt;The behavior of logarithms, however, is important to understand in algorithms and their complexity analysis because the potential improvements in algorithm performance are so great, and can be thought of as low hanging fruit in many cases, such as for well known divide and conquer algorithms. &lt;/p&gt;

</description>
      <category>logarithm</category>
      <category>bigo</category>
    </item>
  </channel>
</rss>
