<?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: Conectado</title>
    <description>The latest articles on DEV Community by Conectado (@conectado).</description>
    <link>https://dev.to/conectado</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%2F81447%2F863534b8-0030-41a7-999f-74d2b0cd18be.png</url>
      <title>DEV Community: Conectado</title>
      <link>https://dev.to/conectado</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/conectado"/>
    <language>en</language>
    <item>
      <title>Advent of code Day 2 Part 2 Complexity</title>
      <dc:creator>Conectado</dc:creator>
      <pubDate>Mon, 03 Dec 2018 12:38:47 +0000</pubDate>
      <link>https://dev.to/conectado/advent-of-code-day-2-part-2-complexity-556l</link>
      <guid>https://dev.to/conectado/advent-of-code-day-2-part-2-complexity-556l</guid>
      <description>

&lt;p&gt;I've been trying to solve the Advent of code day 2 part 2 exercise. The 'simplest' algorithm I've come up with is the following (Pseudo-code)&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;i &amp;lt;- 0
while i &amp;lt;= len(file) do:
    j &amp;lt;- i+1
    while j &amp;lt;= len(file) do:
        same = sameCharacters(file[i], file[j])
        if len(file[i]) == len(file[j]) == len(same+1) then:
            return same
        j++
   i++
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;file&lt;/code&gt; contains the whole exercise input, &lt;code&gt;file[i]&lt;/code&gt; returns line number &lt;code&gt;i&lt;/code&gt; of the input and &lt;code&gt;sameCharacters&lt;/code&gt; returns the common characters between two words compared position by position as per explained by the exercise.&lt;/p&gt;

&lt;p&gt;So, if the maximum length of a word is &lt;code&gt;k&lt;/code&gt;and the number of lines in the file is &lt;code&gt;n&lt;/code&gt;this solution would have complexity &lt;code&gt;O(n^2*k)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I've been trying to come up with a solution with a better worst-case complexity. I've thought on making a tree out of the words in the file, so that its root would point to the first letter of each word, then each node would point to the next letter and so on, the leafs would point to the index of the corresponding word in the file, so an array like &lt;code&gt;[aaa, abb, baa]&lt;/code&gt; would become&lt;/p&gt;

&lt;p&gt;root&lt;br&gt;
 /   \&lt;br&gt;
 a    b&lt;br&gt;
/\    |&lt;br&gt;
a b   a&lt;br&gt;
| |   |&lt;br&gt;
a b   a&lt;br&gt;
| |   |&lt;br&gt;
0 1   2&lt;/p&gt;

&lt;p&gt;Creating this tree would take &lt;code&gt;O(n*k)&lt;/code&gt; but then, although I believe that this could improve performance, one would still need to search the whole tree for each word in the worst case scenario and the complexity would stay &lt;code&gt;O(n^2*k)&lt;/code&gt;. I've not come up with anything better yet, anyone has any idea?&lt;/p&gt;


</description>
      <category>adventofcode</category>
      <category>complexity</category>
    </item>
  </channel>
</rss>
