<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Problems on Colan Biemer</title><link>https://bi3mer.github.io/problems/</link><description>Recent content in Problems on Colan Biemer</description><generator>Hugo -- 0.137.1</generator><language>en-us</language><lastBuildDate>Sun, 21 Jun 2026 09:06:31 -0500</lastBuildDate><atom:link href="https://bi3mer.github.io/problems/index.xml" rel="self" type="application/rss+xml"/><item><title>LeetCode 2</title><link>https://bi3mer.github.io/posts/leetcode-2/</link><pubDate>Sun, 21 Jun 2026 09:06:31 -0500</pubDate><guid>https://bi3mer.github.io/posts/leetcode-2/</guid><description>&lt;p>&lt;a href="https://leetcode.com/problems/add-two-numbers/description/">LeetCode 2&lt;/a> asks the user to add two numbers represented as linked lists, where that linked list is reversed.&lt;/p>
&lt;pre tabindex="0">&lt;code>Example 1:
Input: l1 = 2-&amp;gt;4-&amp;gt;3, l2 = 5-&amp;gt;6-&amp;gt;4
Output: 7-&amp;gt;0-&amp;gt;8
&lt;/code>&lt;/pre>&lt;p>Example one shows why the linked list being reversed is important. &lt;code>2-&amp;gt;4-&amp;gt;3&lt;/code> represents &lt;code>342&lt;/code> NOT &lt;code>243&lt;/code>&lt;/p>
&lt;pre tabindex="0">&lt;code>Example 2:
Input: l1 = 0, l2 = 0
Output: 0
Example 3:
Input: l1 = 9-&amp;gt;9-&amp;gt;9-&amp;gt;9-&amp;gt;9-&amp;gt;9-&amp;gt;9, l2 = 9-&amp;gt;9-&amp;gt;9-&amp;gt;9
Output: 8-&amp;gt;9-&amp;gt;9-&amp;gt;9-&amp;gt;0-&amp;gt;0-&amp;gt;0-&amp;gt;1
&lt;/code>&lt;/pre>&lt;p>This becomes important because if you look at example 3, we have two input numbers of different magnitudes, but because they are reversed, we can add from the start of the list. We&amp;rsquo;ll do this a little later in the post.&lt;/p></description></item><item><title>LeetCode 1</title><link>https://bi3mer.github.io/posts/leetcode-1/</link><pubDate>Sat, 20 Jun 2026 12:11:03 -0500</pubDate><guid>https://bi3mer.github.io/posts/leetcode-1/</guid><description>&lt;p>As mentioned in &lt;a href="https://bi3mer.github.io/posts/leetcode-32/#conclusion">the last post,&lt;/a> we are going back to the start of the LeetCode problem set, and doing &lt;a href="https://leetcode.com/problems/two-sum/description/">LeetCode 1&lt;/a>. The problem is to find two elements in the input array that add up to a target value that is also input. The output, then, is the indices of those two elements:&lt;/p>
&lt;pre tabindex="0">&lt;code>Example 1:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Example 2:
Input: nums = [3,2,4], target = 6
Output: [1,2]
Example 3:
Input: nums = [3,3], target = 6
Output: [0,1]
&lt;/code>&lt;/pre>&lt;p>We are also given several constraints, the most important of which is that there is only one valid answer. Meaning, an input of &lt;code>nums=[1,1,1]&lt;/code> and &lt;code>target=2&lt;/code> is impossible because that means there would be multiple valid ouputs: &lt;code>[[0,1], [0,2], [1,2]]&lt;/code>.&lt;/p></description></item><item><title>LeetCode 32</title><link>https://bi3mer.github.io/posts/leetcode-32/</link><pubDate>Fri, 19 Jun 2026 07:30:25 -0500</pubDate><guid>https://bi3mer.github.io/posts/leetcode-32/</guid><description>&lt;p>We are now on &lt;a href="https://leetcode.com/problems/longest-valid-parentheses/">LeetCode 32&lt;/a>. It is titled, &amp;ldquo;Longest Valid Parentheses,&amp;rdquo; and the problem definition is wonderfully sparse: &amp;ldquo;Given a string containing just the characters &amp;lsquo;(&amp;rsquo; and &amp;lsquo;)&amp;rsquo;, return the length of the longest valid (well-formed) parentheses substring.&amp;rdquo;&lt;/p>
&lt;pre tabindex="0">&lt;code>Example 1:
Input: s = &amp;#34;(()&amp;#34;
Output: 2
Example 2:
Input: s = &amp;#34;)()())&amp;#34;
Output: 4
Example 3:
Input: s = &amp;#34;&amp;#34;
Output: 0
&lt;/code>&lt;/pre>&lt;p>Oh! This problem is listed as&amp;hellip; &lt;span style="color:red">HARD&lt;/span>!!!&lt;/p>
&lt;h1 id="solution-1-single-pass-stack">Solution 1: Single Pass Stack&lt;/h1>
&lt;p>The worst solution would have been &lt;code>O(n³)&lt;/code> where &lt;code>n=len(s)&lt;/code>, and I was tempted, but I didn&amp;rsquo;t bother. After doing so many leetcode problems&amp;mdash;as if 15 problems were a lot when I know others have done hundreds, maybe thousands of these hellish things&amp;mdash;I decided that I would rather get the problem done quickly. Especially a &amp;ldquo;hard&amp;rdquo; problem.&lt;/p></description></item><item><title>LeetCode 31</title><link>https://bi3mer.github.io/posts/leetcode-31/</link><pubDate>Wed, 17 Jun 2026 06:30:45 -0500</pubDate><guid>https://bi3mer.github.io/posts/leetcode-31/</guid><description>&lt;p>&lt;a href="https://leetcode.com/problems/next-permutation/description/">Problem 31&lt;/a> is &lt;span style="color:orange">medium&lt;/span> difficulty. The title is &amp;ldquo;Next Permutation,&amp;rdquo; which partially relates to &lt;a href="../leetcode-30/">yesterday&amp;rsquo;s problem&lt;/a> where I tried to use permutations, but the goal is different. To see, let&amp;rsquo;s look at the examples that LeetCode gave:&lt;/p>
&lt;pre tabindex="0">&lt;code>Example 1:
Input: nums = [1,2,3]
Output: [1,3,2]
Example 2:
Input: nums = [3,2,1]
Output: [1,2,3]
Example 3:
Input: nums = [1,1,5]
Output: [1,5,1]
&lt;/code>&lt;/pre>&lt;p>The goal, as the problem&amp;rsquo;s title suggest, is to generate the next permutation. Not just any permutation, though. That would be too easy. They want the &amp;ldquo;next&amp;rdquo; one, and they defined next by, &amp;ldquo;The next permutation of an array of integers is the next lexicographically greater permutation of its integer.&amp;rdquo;&lt;/p></description></item><item><title>LeetCode 30</title><link>https://bi3mer.github.io/posts/leetcode-30/</link><pubDate>Tue, 16 Jun 2026 09:24:41 -0500</pubDate><guid>https://bi3mer.github.io/posts/leetcode-30/</guid><description>&lt;p>We are finally back to a &lt;span style="color:red">HARD&lt;/span> problem with &lt;a href="https://leetcode.com/problems/substring-with-concatenation-of-all-words/description/">LeetCode 30.&lt;/a> As input, we get one string and one array of strings. The expected output is an array of indices.&lt;/p>
&lt;pre tabindex="0">&lt;code>Example 1:
Input: s = &amp;#34;barfoothefoobarman&amp;#34;, words = [&amp;#34;foo&amp;#34;,&amp;#34;bar&amp;#34;]
Output: [0,9]
Example 2:
Input: s = &amp;#34;wordgoodgoodgoodbestword&amp;#34;, words = [&amp;#34;word&amp;#34;,&amp;#34;good&amp;#34;,&amp;#34;best&amp;#34;,&amp;#34;word&amp;#34;]
Output: []
Example 3:
Input: s = &amp;#34;barfoofoobarthefoobarman&amp;#34;, words = [&amp;#34;bar&amp;#34;,&amp;#34;foo&amp;#34;,&amp;#34;the&amp;#34;]
Output: [6,9,12]
&lt;/code>&lt;/pre>&lt;p>The output array is expected to have indices into &lt;code>s&lt;/code>. Looking at Example 1, there are two indices: 0 and 9:&lt;/p></description></item><item><title>LeetCode 29</title><link>https://bi3mer.github.io/posts/leetcode-29/</link><pubDate>Mon, 15 Jun 2026 09:22:12 -0500</pubDate><guid>https://bi3mer.github.io/posts/leetcode-29/</guid><description>&lt;p>&lt;a href="https://leetcode.com/problems/divide-two-integers/description/">LeetCode 29&lt;/a> asks for you to implement integer division without using division, mod, or multiplication. Naturally, the first thing I did was use division.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-go" data-lang="go">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">func&lt;/span> &lt;span style="color:#a6e22e">divide&lt;/span>(&lt;span style="color:#a6e22e">dividend&lt;/span> &lt;span style="color:#66d9ef">int&lt;/span>, &lt;span style="color:#a6e22e">divisor&lt;/span> &lt;span style="color:#66d9ef">int&lt;/span>) &lt;span style="color:#66d9ef">int&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> &lt;span style="color:#a6e22e">dividend&lt;/span> &lt;span style="color:#f92672">/&lt;/span> &lt;span style="color:#a6e22e">divisor&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>This, though, failed!&lt;/p>
&lt;pre tabindex="0">&lt;code>input: -2147483648 / -1
Output: 2147483648
Expected Output: 2147483647
&lt;/code>&lt;/pre>&lt;p>That led me to the conclusion that this is silly and a waste of time.&lt;sup id="fnref:1">&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref">1&lt;/a>&lt;/sup> So I didn&amp;rsquo;t bother with the bit manipulation solutions I know are out there. Instead, I made a quick fix for division to make Go&amp;rsquo;s division of integers fit what LeetCode wanted.&lt;/p></description></item><item><title>LeetCode 28</title><link>https://bi3mer.github.io/posts/leetcode-28/</link><pubDate>Sun, 14 Jun 2026 07:58:24 -0500</pubDate><guid>https://bi3mer.github.io/posts/leetcode-28/</guid><description>&lt;p>&lt;a href="https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/">LeetCode 28&lt;/a> is another very easy problem. You are given two strings, and you have to see if the second string exists in the first string. If so, return the index of the start of that string. If it can&amp;rsquo;t be found, return -1.&lt;/p>
&lt;h1 id="solution-1-stringsindex">Solution 1: &lt;code>strings.Index&lt;/code>&lt;/h1>
&lt;p>This kind of problem is so common in programming, that the majority of standard libraries include it. In C, you can use &lt;code>strstr&lt;/code>. In Go, you can use &lt;code>strings.Index()&lt;/code>.&lt;/p></description></item><item><title>LeetCode 27</title><link>https://bi3mer.github.io/posts/leetcode-27/</link><pubDate>Sat, 13 Jun 2026 09:59:33 -0500</pubDate><guid>https://bi3mer.github.io/posts/leetcode-27/</guid><description>&lt;p>&lt;a href="https://leetcode.com/problems/remove-element/description/">LeetCode 27&lt;/a> is, like &lt;a href="../leetcode-26/">the last problem&lt;/a>, an &lt;span style="color:green">easy&lt;/span> problem. The problem itself is to remove elements from an array that equal &lt;code>v&lt;/code>. Here is an example:&lt;/p>
&lt;pre tabindex="0">&lt;code>Input: nums = [1,2,3,1,2], val = 1
Output: nums = [2,3,2,_,_], size = 3
&lt;/code>&lt;/pre>&lt;p>If you read the &lt;a href="../leetcode-26/">post for problem 26&lt;/a>, then I think you&amp;rsquo;ll know the solution.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-go" data-lang="go">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">func&lt;/span> &lt;span style="color:#a6e22e">removeElement&lt;/span>(&lt;span style="color:#a6e22e">nums&lt;/span> []&lt;span style="color:#66d9ef">int&lt;/span>, &lt;span style="color:#a6e22e">val&lt;/span> &lt;span style="color:#66d9ef">int&lt;/span>) &lt;span style="color:#66d9ef">int&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">write_index&lt;/span> &lt;span style="color:#f92672">:=&lt;/span> &lt;span style="color:#ae81ff">0&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">for&lt;/span> &lt;span style="color:#a6e22e">_&lt;/span>, &lt;span style="color:#a6e22e">v&lt;/span> &lt;span style="color:#f92672">:=&lt;/span> &lt;span style="color:#66d9ef">range&lt;/span> &lt;span style="color:#a6e22e">nums&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">if&lt;/span> &lt;span style="color:#a6e22e">val&lt;/span> &lt;span style="color:#f92672">!=&lt;/span> &lt;span style="color:#a6e22e">v&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">nums&lt;/span>[&lt;span style="color:#a6e22e">write_index&lt;/span>] = &lt;span style="color:#a6e22e">v&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">write_index&lt;/span>&lt;span style="color:#f92672">++&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> &lt;span style="color:#a6e22e">write_index&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;code>write_index&lt;/code> tracks where the next kept element goes, so each value that isn&amp;rsquo;t &lt;code>val&lt;/code> gets compacted toward the front while everything else is skipped. If the code doesn&amp;rsquo;t make sense, then I recommend that you read the last post.&lt;/p></description></item><item><title>LeetCode 26</title><link>https://bi3mer.github.io/posts/leetcode-26/</link><pubDate>Fri, 12 Jun 2026 09:24:02 -0500</pubDate><guid>https://bi3mer.github.io/posts/leetcode-26/</guid><description>&lt;p>&lt;a href="https://leetcode.com/problems/remove-duplicates-from-sorted-array/">Problem 26&lt;/a> lives up to its label: &lt;span style="color:green">easy&lt;/span>. You are given a sorted array, and you have to remove duplicates from it. It also asks that you include the number of unique elements in the array. Here are some samples.&lt;/p>
&lt;pre tabindex="0">&lt;code>Input: [1,1,2]
Output: 2, nums = [1,2,_]
&lt;/code>&lt;/pre>&lt;p>The first solution I came up with was to find duplicates and remove them from the array:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-go" data-lang="go">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">func&lt;/span> &lt;span style="color:#a6e22e">removeDuplicates&lt;/span>(&lt;span style="color:#a6e22e">nums&lt;/span> []&lt;span style="color:#66d9ef">int&lt;/span>) &lt;span style="color:#66d9ef">int&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">for&lt;/span> &lt;span style="color:#a6e22e">i&lt;/span> &lt;span style="color:#f92672">:=&lt;/span> &lt;span style="color:#ae81ff">0&lt;/span>; &lt;span style="color:#a6e22e">i&lt;/span> &amp;lt; len(&lt;span style="color:#a6e22e">nums&lt;/span>) &lt;span style="color:#f92672">-&lt;/span> &lt;span style="color:#ae81ff">1&lt;/span>; &lt;span style="color:#a6e22e">i&lt;/span>&lt;span style="color:#f92672">++&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">j&lt;/span> &lt;span style="color:#f92672">:=&lt;/span> &lt;span style="color:#a6e22e">i&lt;/span> &lt;span style="color:#f92672">+&lt;/span> &lt;span style="color:#ae81ff">1&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">if&lt;/span> &lt;span style="color:#a6e22e">nums&lt;/span>[&lt;span style="color:#a6e22e">i&lt;/span>] &lt;span style="color:#f92672">==&lt;/span> &lt;span style="color:#a6e22e">nums&lt;/span>[&lt;span style="color:#a6e22e">j&lt;/span>] {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">j&lt;/span>&lt;span style="color:#f92672">++&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">for&lt;/span> ; &lt;span style="color:#a6e22e">j&lt;/span> &amp;lt; len(&lt;span style="color:#a6e22e">nums&lt;/span>) &lt;span style="color:#f92672">&amp;amp;&amp;amp;&lt;/span> &lt;span style="color:#a6e22e">nums&lt;/span>[&lt;span style="color:#a6e22e">i&lt;/span>] &lt;span style="color:#f92672">==&lt;/span> &lt;span style="color:#a6e22e">nums&lt;/span>[&lt;span style="color:#a6e22e">j&lt;/span>]; &lt;span style="color:#a6e22e">j&lt;/span>&lt;span style="color:#f92672">++&lt;/span> { }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">nums&lt;/span> = &lt;span style="color:#a6e22e">slices&lt;/span>.&lt;span style="color:#a6e22e">Delete&lt;/span>(&lt;span style="color:#a6e22e">nums&lt;/span>, &lt;span style="color:#a6e22e">i&lt;/span>&lt;span style="color:#f92672">+&lt;/span>&lt;span style="color:#ae81ff">1&lt;/span>,&lt;span style="color:#a6e22e">j&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> len(&lt;span style="color:#a6e22e">nums&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>This, though, is &lt;code>O(n²)&lt;/code>. The reason why is not the two &lt;code>for&lt;/code> loops, it is the &lt;code>slices.Delete&lt;/code>, which will shift all the elements from the right over to the left. We can avoid this cost and get the solution down to just &lt;code>O(n)&lt;/code> by not shifting.&lt;/p></description></item><item><title>LeetCode 25</title><link>https://bi3mer.github.io/posts/leetcode-25/</link><pubDate>Thu, 11 Jun 2026 09:15:19 -0500</pubDate><guid>https://bi3mer.github.io/posts/leetcode-25/</guid><description>&lt;p>Welcome back. We are going to be solving a dreaded &lt;span style="color:red">HARD&lt;/span> problem. Specifically, we are going to do &lt;a href="https://leetcode.com/problems/reverse-nodes-in-k-group/description/">problem 25&lt;/a>. What I like about it is that it directly builds on &lt;a href="../leetcode-24/">problem 24, the problem I solved yesterday.&lt;/a> As a reminder, &lt;a href="https://leetcode.com/problems/swap-nodes-in-pairs/">problem 24&lt;/a> asked you to swap nodes in pairs. This problem is the same idea, except instead of pairs, we have to swap &lt;code>k&lt;/code> nodes. Meaning, problem 24 was a specific instance of this problem where &lt;code>k=2&lt;/code>. To help, here are two examples:&lt;/p></description></item><item><title>LeetCode 24</title><link>https://bi3mer.github.io/posts/leetcode-24/</link><pubDate>Wed, 10 Jun 2026 09:25:33 -0500</pubDate><guid>https://bi3mer.github.io/posts/leetcode-24/</guid><description>&lt;p>&lt;a href="https://leetcode.com/problems/swap-nodes-in-pairs/">LeetCode 24&lt;/a> is a problem where you have to swap pairs of nodes in a linked list. I think the examples are more informative than the text, so here are the examples but I did modify them to use linked list notation:&lt;/p>
&lt;pre tabindex="0">&lt;code>Example 1:
Input: 1 -&amp;gt; 2 -&amp;gt; 3 -&amp;gt; 4
Output: 2 -&amp;gt; 1 -&amp;gt; 4 -&amp;gt; 3
Example 2:
Input: nil
Output: nil
Example 3:
Input: 1
Output: 1
Example 4:
Input: 1 -&amp;gt; 2 -&amp;gt; 3
Output: 2 -&amp;gt; 1 -&amp;gt; 3
&lt;/code>&lt;/pre>&lt;p>This post will be shorter than the others in the series. Despite being listed as &lt;span style="color:orange">medium&lt;/span> difficulty, there wasn&amp;rsquo;t anything that I found interesting.&lt;/p></description></item><item><title>LeetCode 23</title><link>https://bi3mer.github.io/posts/leetcode-23/</link><pubDate>Tue, 09 Jun 2026 10:46:22 -0500</pubDate><guid>https://bi3mer.github.io/posts/leetcode-23/</guid><description>&lt;p>&lt;a href="https://leetcode.com/problems/merge-k-sorted-lists/">LeetCode 23&lt;/a> is the first problem in the series that covers a &lt;span style="color:red">HARD&lt;/span> problem. Should be challenging, right? Well, let&amp;rsquo;s see.&lt;/p>
&lt;p>To start, I need to define the problem. You get a list of linked lists that are sorted. You have to merge them all into one big linked list, that is also sorted. That&amp;rsquo;s it.&lt;/p>
&lt;h1 id="solution-1-screw-linked-lists">Solution 1: Screw Linked Lists&lt;/h1>
&lt;p>I don&amp;rsquo;t have anything against linked lists. They&amp;rsquo;re cool. However, sorting an array is a solved problem. So, if everything was in just one big slice, then we&amp;rsquo;d be done.&lt;/p></description></item><item><title>LeetCode 22</title><link>https://bi3mer.github.io/posts/leetcode-22/</link><pubDate>Mon, 08 Jun 2026 10:10:27 -0500</pubDate><guid>https://bi3mer.github.io/posts/leetcode-22/</guid><description>&lt;p>Welcome back. This time we are going to work on &lt;a href="https://leetcode.com/problems/generate-parentheses/">problem 22&lt;/a>. The idea is simple. We need to generate every possible valid string of open and closed parenthesis for an integer &lt;code>n&lt;/code>. The example they give is for &lt;code>n=1&lt;/code> and &lt;code>n=3&lt;/code>:&lt;/p>
&lt;pre tabindex="0">&lt;code>Input: n = 1
Output: [&amp;#34;()&amp;#34;]
Input: n = 3
Output: [&amp;#34;((()))&amp;#34;,&amp;#34;(()())&amp;#34;,&amp;#34;(())()&amp;#34;,&amp;#34;()(())&amp;#34;,&amp;#34;()()()&amp;#34;]
&lt;/code>&lt;/pre>&lt;h1 id="failed-attempt">Failed Attempt&lt;/h1>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-go" data-lang="go">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">func&lt;/span> &lt;span style="color:#a6e22e">generateParenthesis&lt;/span>(&lt;span style="color:#a6e22e">n&lt;/span> &lt;span style="color:#66d9ef">int&lt;/span>) []&lt;span style="color:#66d9ef">string&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">var&lt;/span> &lt;span style="color:#a6e22e">strings&lt;/span> []&lt;span style="color:#66d9ef">string&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">switch&lt;/span> &lt;span style="color:#a6e22e">n&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">case&lt;/span> &lt;span style="color:#ae81ff">0&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">// do nothing
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">&lt;/span> &lt;span style="color:#66d9ef">case&lt;/span> &lt;span style="color:#ae81ff">1&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">strings&lt;/span> = append(&lt;span style="color:#a6e22e">strings&lt;/span>, &lt;span style="color:#e6db74">&amp;#34;()&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">default&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">old_strings&lt;/span> &lt;span style="color:#f92672">:=&lt;/span> &lt;span style="color:#a6e22e">generateParenthesis&lt;/span>(&lt;span style="color:#a6e22e">n&lt;/span> &lt;span style="color:#f92672">-&lt;/span> &lt;span style="color:#ae81ff">1&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">for&lt;/span> &lt;span style="color:#a6e22e">_&lt;/span>, &lt;span style="color:#a6e22e">o&lt;/span> &lt;span style="color:#f92672">:=&lt;/span> &lt;span style="color:#66d9ef">range&lt;/span> &lt;span style="color:#a6e22e">old_strings&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">strings&lt;/span> = append(&lt;span style="color:#a6e22e">strings&lt;/span>, &lt;span style="color:#e6db74">&amp;#34;()&amp;#34;&lt;/span> &lt;span style="color:#f92672">+&lt;/span> &lt;span style="color:#a6e22e">o&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">strings&lt;/span> = append(&lt;span style="color:#a6e22e">strings&lt;/span>, &lt;span style="color:#e6db74">&amp;#34;(&amp;#34;&lt;/span> &lt;span style="color:#f92672">+&lt;/span> &lt;span style="color:#a6e22e">o&lt;/span> &lt;span style="color:#f92672">+&lt;/span> &lt;span style="color:#e6db74">&amp;#34;)&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> &lt;span style="color:#a6e22e">strings&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>This was my first solution, but it failed. The reason why can be shown with &lt;code>n=3&lt;/code>, where it can&amp;rsquo;t generate the string &amp;ldquo;(())()&amp;rdquo;. This may lead you to think that you should add one more line in the &lt;code>for&lt;/code> loop: &lt;code>strings = append(strings, o + &amp;quot;()&amp;quot;)&lt;/code>. This, though, will cause duplicate strings, which breaks the rules. So, after that we could go the de-duplication route like we did for &lt;a href="https://bi3mer.github.io/posts/leetcode-18/">#18&lt;/a>, but I&amp;rsquo;d rather not. So, let&amp;rsquo;s abandon this solution and go to the smarter approach.&lt;/p></description></item><item><title>LeetCode 21</title><link>https://bi3mer.github.io/posts/leetcode-21/</link><pubDate>Sun, 07 Jun 2026 09:41:05 -0500</pubDate><guid>https://bi3mer.github.io/posts/leetcode-21/</guid><description>&lt;p>Unfortunately, &lt;a href="https://leetcode.com/problems/merge-two-sorted-lists/">LeetCode 21&lt;/a> had no interesting wrinkles to explore.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-go" data-lang="go">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">func&lt;/span> &lt;span style="color:#a6e22e">mergeTwoLists&lt;/span>(&lt;span style="color:#a6e22e">list1&lt;/span> &lt;span style="color:#f92672">*&lt;/span>&lt;span style="color:#a6e22e">ListNode&lt;/span>, &lt;span style="color:#a6e22e">list2&lt;/span> &lt;span style="color:#f92672">*&lt;/span>&lt;span style="color:#a6e22e">ListNode&lt;/span>) &lt;span style="color:#f92672">*&lt;/span>&lt;span style="color:#a6e22e">ListNode&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">if&lt;/span> &lt;span style="color:#a6e22e">list1&lt;/span> &lt;span style="color:#f92672">==&lt;/span> &lt;span style="color:#66d9ef">nil&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> &lt;span style="color:#a6e22e">list2&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> } &lt;span style="color:#66d9ef">else&lt;/span> &lt;span style="color:#66d9ef">if&lt;/span> &lt;span style="color:#a6e22e">list2&lt;/span> &lt;span style="color:#f92672">==&lt;/span> &lt;span style="color:#66d9ef">nil&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> &lt;span style="color:#a6e22e">list1&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">var&lt;/span> &lt;span style="color:#a6e22e">head&lt;/span> &lt;span style="color:#f92672">*&lt;/span>&lt;span style="color:#a6e22e">ListNode&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">if&lt;/span> &lt;span style="color:#a6e22e">list1&lt;/span>.&lt;span style="color:#a6e22e">Val&lt;/span> &lt;span style="color:#f92672">&amp;lt;=&lt;/span> &lt;span style="color:#a6e22e">list2&lt;/span>.&lt;span style="color:#a6e22e">Val&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">head&lt;/span> = &lt;span style="color:#a6e22e">list1&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">list1&lt;/span> = &lt;span style="color:#a6e22e">list1&lt;/span>.&lt;span style="color:#a6e22e">Next&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> } &lt;span style="color:#66d9ef">else&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">head&lt;/span> = &lt;span style="color:#a6e22e">list2&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">list2&lt;/span> = &lt;span style="color:#a6e22e">list2&lt;/span>.&lt;span style="color:#a6e22e">Next&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">cur&lt;/span> &lt;span style="color:#f92672">:=&lt;/span> &lt;span style="color:#a6e22e">head&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">for&lt;/span> &lt;span style="color:#a6e22e">list1&lt;/span> &lt;span style="color:#f92672">!=&lt;/span> &lt;span style="color:#66d9ef">nil&lt;/span> &lt;span style="color:#f92672">&amp;amp;&amp;amp;&lt;/span> &lt;span style="color:#a6e22e">list2&lt;/span> &lt;span style="color:#f92672">!=&lt;/span> &lt;span style="color:#66d9ef">nil&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">if&lt;/span> &lt;span style="color:#a6e22e">list1&lt;/span>.&lt;span style="color:#a6e22e">Val&lt;/span> &lt;span style="color:#f92672">&amp;lt;=&lt;/span> &lt;span style="color:#a6e22e">list2&lt;/span>.&lt;span style="color:#a6e22e">Val&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">cur&lt;/span>.&lt;span style="color:#a6e22e">Next&lt;/span> = &lt;span style="color:#a6e22e">list1&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">list1&lt;/span> = &lt;span style="color:#a6e22e">list1&lt;/span>.&lt;span style="color:#a6e22e">Next&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> } &lt;span style="color:#66d9ef">else&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">cur&lt;/span>.&lt;span style="color:#a6e22e">Next&lt;/span> = &lt;span style="color:#a6e22e">list2&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">list2&lt;/span> = &lt;span style="color:#a6e22e">list2&lt;/span>.&lt;span style="color:#a6e22e">Next&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">cur&lt;/span> = &lt;span style="color:#a6e22e">cur&lt;/span>.&lt;span style="color:#a6e22e">Next&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">if&lt;/span> &lt;span style="color:#a6e22e">list1&lt;/span> &lt;span style="color:#f92672">==&lt;/span> &lt;span style="color:#66d9ef">nil&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">cur&lt;/span>.&lt;span style="color:#a6e22e">Next&lt;/span> = &lt;span style="color:#a6e22e">list2&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> } &lt;span style="color:#66d9ef">else&lt;/span> &lt;span style="color:#66d9ef">if&lt;/span> &lt;span style="color:#a6e22e">list2&lt;/span> &lt;span style="color:#f92672">==&lt;/span> &lt;span style="color:#66d9ef">nil&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">cur&lt;/span>.&lt;span style="color:#a6e22e">Next&lt;/span> = &lt;span style="color:#a6e22e">list1&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> &lt;span style="color:#a6e22e">head&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>There is one way to reduce the amount of code, which is to create a dummy variable like so:&lt;/p></description></item><item><title>LeetCode 20</title><link>https://bi3mer.github.io/posts/leetcode-20/</link><pubDate>Sat, 06 Jun 2026 12:00:54 -0500</pubDate><guid>https://bi3mer.github.io/posts/leetcode-20/</guid><description>&lt;p>&lt;a href="https://leetcode.com/problems/valid-parentheses/">LeetCode 20&lt;/a> gives you a string made up of the six bracket characters &lt;code>()[]{}&lt;/code> and asks whether they are properly matched and nested. &amp;ldquo;Properly&amp;rdquo; means every opener has a corresponding closer of the same type, and they close in the right order. So &lt;code>()[]{}&lt;/code> is valid, &lt;code>([])&lt;/code> is valid, but &lt;code>([)]&lt;/code> is not.&lt;/p>
&lt;p>Anywho, I&amp;rsquo;ll start with a dumb implementation for fun and then move on to the better but less exciting version.&lt;/p></description></item><item><title>LeetCode 19</title><link>https://bi3mer.github.io/posts/leetcode-19/</link><pubDate>Fri, 05 Jun 2026 09:37:33 -0500</pubDate><guid>https://bi3mer.github.io/posts/leetcode-19/</guid><description>&lt;p>&lt;a href="https://leetcode.com/problems/remove-nth-node-from-end-of-list/">LeetCode 19&lt;/a> asks the solver to remove a node from a linked list. It does have a trick, though. Rather than the typical &amp;ldquo;remove the nth element&amp;rdquo; it gives you an index based on the end of the list, &amp;ldquo;remove the nth element from the end of the list.&amp;rdquo;&lt;/p>
&lt;p>That framing is the whole problem. In a singly linked list you can only move forward, so &amp;ldquo;nth from the end&amp;rdquo; isn&amp;rsquo;t something you can read off directly the way you would in an array. Every solution below is really just a different way of figuring out which node sits before the one you want to drop, since removing a node means pointing its predecessor&amp;rsquo;s &lt;code>Next&lt;/code> past it.&lt;/p></description></item><item><title>LeetCode 18</title><link>https://bi3mer.github.io/posts/leetcode-18/</link><pubDate>Thu, 04 Jun 2026 08:00:41 -0400</pubDate><guid>https://bi3mer.github.io/posts/leetcode-18/</guid><description>&lt;p>&lt;a href="https://leetcode.com/problems/4sum/description/">LeetCode 18, &lt;em>4Sum&lt;/em>&lt;/a> asks for every &lt;em>unique&lt;/em> quadruplet &lt;code>[a, b, c, d]&lt;/code> in an array &lt;code>nums&lt;/code> that sums to a &lt;code>target&lt;/code>. For example, given &lt;code>nums = [1, 0, -1, 0, -2, 2]&lt;/code> and &lt;code>target = 0&lt;/code>, the answer is &lt;code>[[-2, -1, 1, 2], [-2, 0, 0, 2], [-1, 0, 0, 1]]&lt;/code>. The word &lt;em>unique&lt;/em> is the whole game here. It is easy to find quadruplets that sum to the target; it is annoying to make sure you never report the same one twice.&lt;/p></description></item><item><title>LeetCode 17</title><link>https://bi3mer.github.io/posts/leetcode-17/</link><pubDate>Wed, 03 Jun 2026 08:49:11 +0000</pubDate><guid>https://bi3mer.github.io/posts/leetcode-17/</guid><description>&lt;h1 id="from-array-confusion-to-a-mixed-radix-decoder-solving-leetcode-17-in-go">From Array Confusion to a Mixed-Radix Decoder: Solving LeetCode 17 in Go&lt;/h1>
&lt;p>&lt;a href="https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/">LeetCode 17, &lt;em>Letter Combinations of a Phone Number&lt;/em>&lt;/a> is simple: given a string of digits like &lt;code>&amp;quot;23&amp;quot;&lt;/code>, return every combination of letters those digits could spell on an old phone keypad. For example, &lt;code>2&lt;/code> maps to &lt;code>['a', 'b', 'c']&lt;/code> and if given the input &lt;code>22&lt;/code>, we would expect the output: &lt;code>[&amp;quot;aa&amp;quot;, &amp;quot;ab&amp;quot;, &amp;quot;ac&amp;quot;, &amp;quot;ba&amp;quot;, &amp;quot;bb&amp;quot;, &amp;quot;bc&amp;quot;, &amp;quot;ca&amp;quot;, &amp;quot;cb&amp;quot;, &amp;quot;cc&amp;quot;]&lt;/code>.&lt;/p></description></item></channel></rss>