<?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: Deepak Venkatachalam</title>
    <description>The latest articles on DEV Community by Deepak Venkatachalam (@deepakvenkat).</description>
    <link>https://dev.to/deepakvenkat</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%2F37840%2Fcc77cefb-756c-441c-bc72-b834046e63ad.jpg</url>
      <title>DEV Community: Deepak Venkatachalam</title>
      <link>https://dev.to/deepakvenkat</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/deepakvenkat"/>
    <language>en</language>
    <item>
      <title>Programming Exercise: Frequency Sort</title>
      <dc:creator>Deepak Venkatachalam</dc:creator>
      <pubDate>Fri, 12 Jun 2020 23:34:10 +0000</pubDate>
      <link>https://dev.to/deepakvenkat/programming-exercise-frequency-sort-3k08</link>
      <guid>https://dev.to/deepakvenkat/programming-exercise-frequency-sort-3k08</guid>
      <description>&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;Recently, I decided to solve some of the problems in leetcode.com for fun and practicing my java which I have not used in a while. I decided to document my thought process as I solve these problems. You can find some of the other solutions in the series table above this section.&lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://leetcode.com/problems/sort-characters-by-frequency"&gt;Frequency Sort&lt;/a&gt;&lt;br&gt;
Given a string, return another string where the characters are sorted by the frequency of their appearance with the most repeating at the beginning. &lt;br&gt;
Eg:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: tree
Output: eert
Input: aabbbc
Output: bbbaac
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;Whenever I see a problem like this, where the output depends on the how each item in a list relate to another item in the list (here for e.g. how many times each element appears in the list) my mind immediately jumps to a two pass approach with a hashmap. &lt;/p&gt;

&lt;p&gt;This is not because hashmaps are my favorite data structure (I don't have a favorite data structure), but rather the ease of lookup of hashmaps. So usually my mind thinks how I can store the data in a hashmap in the first pass, so that I can look it up in the second pass thereby avoiding a nested loop. &lt;/p&gt;

&lt;p&gt;This problem was no different and I started out the same way. But when I started to write down what the hashmap I needed I realized I might need two hashmaps. One to represent how many counts of a characters there are, and another to represent the different counts itself. (There might be a better way of doing it, but this is what I came up with in the first try)&lt;/p&gt;

&lt;p&gt;So my two hashmaps for the second example above looked like this at the end of the first pass:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// For each character a string with their frequency&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;aa&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="nx"&gt;bbb&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// For each count a list of characters with the count&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="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="nx"&gt;c&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;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="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;:&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now there is some repetition in the second map, but that was ok since I didn't want to add additional complexity of lookup in the first pass.&lt;/p&gt;

&lt;p&gt;From here it was a matter of sorting the counts and then forming a string from the other map.&lt;/p&gt;

&lt;p&gt;This yielded the following code: &lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;h3&gt;
  
  
  Complexity
&lt;/h3&gt;

&lt;p&gt;Since there are a few different steps involved in here lets see the complexity of each step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First pass - constructing the hashmap - &lt;code&gt;O(n)&lt;/code&gt; where n is the length of the string.&lt;/li&gt;
&lt;li&gt;Sorting the counts - &lt;code&gt;O(k.log k)&lt;/code&gt; where k is the number of unique counts.&lt;/li&gt;
&lt;li&gt;Second pass - constructing the result - 
3.1 &lt;code&gt;O(n)&lt;/code&gt; - if each character appeared only once.
3.2 &lt;code&gt;O(k + n)&lt;/code&gt; - if there are &lt;code&gt;k&lt;/code&gt; counts each with more than one item. But since k &amp;lt; n, that is really &lt;code&gt;O(n)&lt;/code&gt; &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So the most expensive operation here is the sort. &lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>leetcode</category>
    </item>
    <item>
      <title>Leetcode Problem: Valid Parenthesis</title>
      <dc:creator>Deepak Venkatachalam</dc:creator>
      <pubDate>Thu, 04 Jun 2020 23:03:35 +0000</pubDate>
      <link>https://dev.to/deepakvenkat/leetcode-problem-valid-parenthesis-3e41</link>
      <guid>https://dev.to/deepakvenkat/leetcode-problem-valid-parenthesis-3e41</guid>
      <description>&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;Recently, I decided to solve some of the problems in leetcode.com for fun and practicing my java which I have not used in a while. I decided to document my thought process as I solve these problems. You can find some of the other solutions in the series table above this section.&lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://leetcode.com/problems/valid-parentheses"&gt;Valid Parentheses&lt;/a&gt;&lt;br&gt;
Given a string of parentheses determine if the string is valid. The string is valid if and only if, the opening and the closing parentheses match.&lt;/p&gt;

&lt;p&gt;For e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{([])} - valid
{{{ - Invalid
()() - Valid
({) - Invalid
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;I know the answer to this problem from my college intro to Algo class. The trick here is to use a &lt;a href="https://en.wikibooks.org/wiki/Data_Structures/Stacks_and_Queues"&gt;Stack&lt;/a&gt; data structure. One of the first benefits I remember of stacks was to use stacks to verify certain kinds of grammar which is what this question is about. Verifying a grammar of parentheses. &lt;/p&gt;

&lt;p&gt;The pseudo algorithm is: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Walk through every character in the array.&lt;/li&gt;
&lt;li&gt;If the character is a opening parentheses push into the array.&lt;/li&gt;
&lt;li&gt;If the character is a closing parentheses pop the top element in the array.&lt;/li&gt;
&lt;li&gt;If it is the corresponding opening parentheses, move on ; else return false (fail quick)&lt;/li&gt;
&lt;li&gt;If all elements have been processed, then the string is valid if the stack is empty.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Coding the solution
&lt;/h3&gt;

&lt;p&gt;The following is the first version of the solution that I wrote: &lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;After I verified that this works, I wanted to clean up a bit. A easy win would be to flip the inner if loop so that I don't have to keep two maps. Also that would take care of invalid (non parentheses) characters. So I got here: &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h4&gt;
  
  
  Sidebar
&lt;/h4&gt;

&lt;p&gt;One other difference in the above two solution is that I have a special case for empty string in the second case. The reason for is two-fold: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First of course is fail early. No need to try and work on string when we already know the result.&lt;/li&gt;
&lt;li&gt;Second is something I learned today (#til). In java if you split an empty string by an empty string, it results in an array of length 1 with an empty string as the only element. 😮&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here is an e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;jshell&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;split&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="err"&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="nc"&gt;String&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="o"&gt;{&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
      <category>java</category>
      <category>leetcode</category>
      <category>programming</category>
    </item>
    <item>
      <title>Leetcode Problem: Group Anagrams</title>
      <dc:creator>Deepak Venkatachalam</dc:creator>
      <pubDate>Sun, 31 May 2020 16:57:11 +0000</pubDate>
      <link>https://dev.to/deepakvenkat/leetcode-problem-group-anagrams-1nd5</link>
      <guid>https://dev.to/deepakvenkat/leetcode-problem-group-anagrams-1nd5</guid>
      <description>&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;Recently, I decided to solve some of the problems in leetcode.com for fun and practicing my java which I have not used in a while. I decided to document my thought process as I solve these problems. You can find some of the other solutions in the series table above this section.&lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://leetcode.com/problems/group-anagrams/"&gt;Group Anagrams&lt;/a&gt;&lt;br&gt;
Given a list of strings, return a list where all anagrams are grouped together.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: [ate, cat, tac, eat, mat, tea, tam, bat]
Output: [[ate, eat, tea] [cat, tac], [mat, tam], [bat]]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Note
&lt;/h3&gt;

&lt;p&gt;A word is considered an &lt;a href="https://en.wikipedia.org/wiki/Anagram"&gt;anagram&lt;/a&gt; of another if it can be formed by rearranging all the letters of the first word.&lt;/p&gt;
&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Finding an anagram
&lt;/h3&gt;

&lt;p&gt;There are a couple of ways to finding if a word is an anagram of another. &lt;/p&gt;
&lt;h4&gt;
  
  
  Sorted String
&lt;/h4&gt;

&lt;p&gt;If you have two string &lt;code&gt;bac&lt;/code&gt; and &lt;code&gt;cab&lt;/code&gt; if you sort them, they both result in the same string &lt;code&gt;abc&lt;/code&gt;. Hence they are anagrams of each other. &lt;/p&gt;
&lt;h4&gt;
  
  
  Count of characters
&lt;/h4&gt;

&lt;p&gt;With the same two strings &lt;code&gt;bac&lt;/code&gt; and &lt;code&gt;cab&lt;/code&gt; if you can count the number of characters in each one of the like &lt;code&gt;{ a: 1, b: 1, c: 1 }&lt;/code&gt; and if these match, then they are anagrams of each other. &lt;/p&gt;
&lt;h3&gt;
  
  
  Grouping Anagrams
&lt;/h3&gt;

&lt;p&gt;My first thought was that counting characters might not be straightforward. So I went with the sorted string approach. &lt;/p&gt;

&lt;p&gt;The rough algorithm was: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Initialize an empty map which will store &lt;code&gt;SortedString -&amp;gt; List of original Strings&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;For every string in the list,&lt;/p&gt;

&lt;p&gt;2.1 Sort the string&lt;/p&gt;

&lt;p&gt;2.2 If there is an entry in the map for the string add current string to the list.&lt;/p&gt;

&lt;p&gt;2.3 Create a new list with this element and add it to map. &lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Return a list of all values in the map.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This yields the following code: &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;h4&gt;
  
  
  Complexity
&lt;/h4&gt;

&lt;p&gt;The complexity of this solution is &lt;code&gt;O(n.m.logm)&lt;/code&gt; where is &lt;em&gt;n&lt;/em&gt; is the number of strings in the list and &lt;em&gt;m&lt;/em&gt; is the average length of the strings in the list. &lt;/p&gt;

&lt;h3&gt;
  
  
  Note
&lt;/h3&gt;

&lt;p&gt;After solving this, I went through the solutions in leetcode, and found that you can use the character count solution to get a better &lt;code&gt;O(n.m)&lt;/code&gt; solution. I will attempt that next. &lt;/p&gt;

</description>
      <category>java</category>
      <category>leetcode</category>
      <category>programming</category>
    </item>
    <item>
      <title>Leetcode Problem: Three sum</title>
      <dc:creator>Deepak Venkatachalam</dc:creator>
      <pubDate>Thu, 28 May 2020 01:42:38 +0000</pubDate>
      <link>https://dev.to/deepakvenkat/leetcode-problem-three-sum-589c</link>
      <guid>https://dev.to/deepakvenkat/leetcode-problem-three-sum-589c</guid>
      <description>&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;Recently, I decided to solve some of the problems in leetcode.com for fun and practicing my java which I have not used in a while. I decided to document my thought process as I solve these problems. You can find some of the other solutions in my profile page.&lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://leetcode.com/problems/3sum/solution/"&gt;3 Sum Problem&lt;/a&gt;&lt;br&gt;
Given a list of numbers and a sum k, return all unique triplets  such that&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Note
&lt;/h3&gt;

&lt;p&gt;This is a subset of the n-sum problem and a level higher in difficulty compared to often asked 2 sum problem. I have personally asked 2 sum problem multiple times in interview but have never gotten to solving the three sum problem. &lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;h3&gt;
  
  
  2 Sum
&lt;/h3&gt;

&lt;p&gt;From previous experience, I know that the fastest way to solve the 2 sum problem is by maintaining a hashmap.&lt;/p&gt;

&lt;p&gt;The rough algorithm for a list of size &lt;code&gt;n&lt;/code&gt; with a sum &lt;code&gt;k&lt;/code&gt; is as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Iterate through the list.&lt;/li&gt;
&lt;li&gt;In each iteration check if the hashmap contains the key &lt;code&gt;k - currentElement&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;if it does you have found a tuple&lt;/li&gt;
&lt;li&gt;else place the currentElement in the hashmap. &lt;/li&gt;
&lt;/ul&gt;


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

&lt;p&gt;This solution yields a &lt;code&gt;O(n)&lt;/code&gt; solution where n is the number fo elements in the list.&lt;/p&gt;

&lt;h3&gt;
  
  
  3 Sum as an extension to 2 sum
&lt;/h3&gt;

&lt;p&gt;My first thought to solve this problem, was to think of extending the hashmap solution from 2sum. &lt;/p&gt;

&lt;p&gt;My first idea was to see if I could maybe use a nested hashmap. So, somehow in each step, I would check every other element. But then I realized I would have to check every other element twice, once for the top level set of keys and once for the second nested level. While I am not sure if this would work (I didn't try implementing this) it would at least involve a n^3 solution. &lt;/p&gt;

&lt;p&gt;An optimization which might be useful is to use the 2 sum problem. So the modified algorithm could be: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For every element in the list&lt;/li&gt;
&lt;li&gt;Get the value of &lt;code&gt;k - currentElement&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;Call twoSum with the value from above step and the rest of the list. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Given twoSum is &lt;code&gt;O(n)&lt;/code&gt; and it is called &lt;code&gt;n&lt;/code&gt; times, this solutions would be &lt;code&gt;O(n^2)&lt;/code&gt; &lt;br&gt;
This is clearly better than my first approach so I decided to implement it. &lt;/p&gt;
&lt;h2&gt;
  
  
  Implementation
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;I wrote a quick twoSum method and then added a third parameter to it &lt;code&gt;skippedIndex&lt;/code&gt; so that I can skip the current element. There is probably a better way to pass around the rest of the list.&lt;/li&gt;
&lt;li&gt;My first run was successful, but then I hit a snag. I was getting duplicate triplets. &lt;/li&gt;
&lt;li&gt;To get around this, I used a &lt;code&gt;Set&lt;/code&gt; instead of an array (more on Java's Set &lt;a href="https://docs.oracle.com/javase/tutorial/collections/interfaces/set.html"&gt;here&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;There was still an issue. If my two sum code returned two set of triplets &lt;code&gt;1, 2, 3&lt;/code&gt; and &lt;code&gt;3, 2, 1&lt;/code&gt; the Set interface in java treated this as two different elements. To get around this problem, I simply sorted the triplet before inserting it into the set. This ensures uniqueness of the triplets. See sidebar below on how this works.&lt;/li&gt;
&lt;li&gt;You can find my full solution &lt;a href="https://github.com/deepakvenkat/LeetCodeExamples/blob/master/src/com/company/ThreeSum.java"&gt;here&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Sidebar
&lt;/h3&gt;

&lt;p&gt;Here is a quick example of Sets and how you can add sorted lists to a set to maintain uniqueness.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;jshell&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HashSet&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;();&lt;/span&gt;
&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;==&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;[]&lt;/span&gt;

&lt;span class="n"&gt;jshell&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;of&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;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;==&amp;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;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;jshell&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;of&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&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;1&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;==&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&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;1&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;jshell&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt; &lt;span class="o"&gt;==&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;

&lt;span class="n"&gt;jshell&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;
&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;==&amp;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;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;]]&lt;/span&gt;

&lt;span class="n"&gt;jshell&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="o"&gt;==&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;

&lt;span class="n"&gt;jshell&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;
&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;==&amp;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;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;],&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&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;1&lt;/span&gt;&lt;span class="o"&gt;]]&lt;/span&gt;

&lt;span class="n"&gt;jshell&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HashSet&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;();&lt;/span&gt;
&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;==&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;[]&lt;/span&gt;

&lt;span class="n"&gt;jshell&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;11&lt;/span&gt; &lt;span class="o"&gt;==&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;

&lt;span class="n"&gt;jshell&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Collections&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sort&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;jshell&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;13&lt;/span&gt; &lt;span class="o"&gt;==&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;

&lt;span class="n"&gt;jshell&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;
&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;==&amp;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;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;]]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
      <category>java</category>
      <category>leetcode</category>
      <category>programming</category>
    </item>
    <item>
      <title>Leetcode: Integer to Roman</title>
      <dc:creator>Deepak Venkatachalam</dc:creator>
      <pubDate>Mon, 25 May 2020 20:59:38 +0000</pubDate>
      <link>https://dev.to/deepakvenkat/leetcode-integer-to-roman-465m</link>
      <guid>https://dev.to/deepakvenkat/leetcode-integer-to-roman-465m</guid>
      <description>&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;Recently, I decided to solve some of the problems in leetcode.com for fun and practicing my java which I have not used in a while. I decided to document my thought process as I solve these problems. Any feedback on improving the solution is welcome. This is not really meant to be instructive but rather just a journal for my own thought process. &lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://leetcode.com/problems/integer-to-roman/"&gt;Integer to Roman&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So the problem is straightforward. Given an integer value, return the roman numeral version of it. (In this specific problem, the input range was limited between 1 and 3999)&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution whiteboarding
&lt;/h2&gt;

&lt;p&gt;I had previously solved the roman numeral to integer problem and similar to that solution, my first idea was to create a map with all possible &lt;code&gt;values -&amp;gt; symbol&lt;/code&gt; combination. &lt;/p&gt;

&lt;p&gt;This yielded this :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    1000: M,
    500: D,
    100: C
   //  … so on
}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  First idea : Division (incorrect?)
&lt;/h3&gt;

&lt;p&gt;Having solved number based problems before, my first thought was to use a division based solution. I thought I could keep a static &lt;code&gt;highestDivisor&lt;/code&gt; &lt;br&gt;
value and then somehow step through the values by dividing by the highest divisor possible for a number (for e.g. for 432 the highest divisor would be 100)&lt;/p&gt;
&lt;h3&gt;
  
  
  Roadblock 1: 5xx and special cases
&lt;/h3&gt;

&lt;p&gt;The first roadblock I hit was that there was 5xx (5, 50, so on) so dividing by 10 might not be a very good way? Also do I do about the special cases?&lt;/p&gt;

&lt;p&gt;To address the second problem first, I added the special cases to the map.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    1000: M,
    900: CM,
    500: D,
    400: CD,
    100: C
   //  … so on
}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But I still had the issue with division.&lt;/p&gt;

&lt;h2&gt;
  
  
  Second idea
&lt;/h2&gt;

&lt;p&gt;So I thought my initial assumption to try and parse the numbers were right. But division might not be the best approach. Maybe, I could try just subtraction? &lt;/p&gt;

&lt;p&gt;I had created the map from highest to lowest values. So, the solution I came up with was the subtract (when I was done, I checked the solutions tab of leetcode the first solution was similar to this) &lt;/p&gt;

&lt;p&gt;So the steps would be : &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Starting with the highest value in the map see if subtracting the number gives a positive number&lt;/li&gt;
&lt;li&gt;If I can't move to the next smaller value. &lt;/li&gt;
&lt;li&gt;When I find the value which can be subtracted from the number, concat the corresponding symbol and keep subtracting until you can't subtract anymore. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This yielded the following java method (You can find my whole solution &lt;a href="https://github.com/deepakvenkat/LeetCodeExamples/blob/master/src/com/company/RomanNumbers.java"&gt;here&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;keys&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;valueSymbolMap&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;keySet&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;keysI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;keys&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toArray&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;keys&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="o"&gt;()]);&lt;/span&gt;
&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&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="o"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&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;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;keysI&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;keysI&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&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="o"&gt;)&lt;/span&gt; &lt;span class="o"&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;result&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;concat&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;valueSymbolMap&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;keysI&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]));&lt;/span&gt;
            &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="n"&gt;keysI&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&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;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Implementation Notes:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;I used a &lt;code&gt;LinkedHashMap&lt;/code&gt; to make sure I get the keyset in the same order as the element added always.&lt;/li&gt;
&lt;li&gt;I can probably do without the double &lt;code&gt;while&lt;/code&gt; loop. &lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;I wonder if there is a more elegant solution to this without some of the hardcoded values but I couldn't think of any. &lt;/p&gt;

</description>
      <category>java</category>
      <category>leetcode</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
