JSON Feed Validator

Check whether your feed is valid. For more information about JSON Feed, see the specification.

{
  "version": "https://jsonfeed.org/version/1.1",
  "title": "Friedrich Ewald",
  "description": "Blog of a software engineer. Follow me along as I explore and write about Python, Ruby, and web development and random pieces of the internet. Happy to chat!",
  "language": "en",
  "home_page_url": "https://fewald.net",
  "feed_url": "https://fewald.net/feed.json",
  "items": [
    {
      "id": "https://fewald.net/2024/01/31/silicon-valley.html",
      "url": "https://fewald.net/2024/01/31/silicon-valley.html",
      "title": "Silicon Valley Photos",
      "content_html": "<p>Marcus Kazmierczak has great photos of the <a href=\"https://mkaz.com/silicon-valley/\">Silicon Valley</a> on his homepage. Definitely worth checking them out.</p>",
      "date_published": "2024-01-31T17:28:00+00:00"
    },
    {
      "id": "https://fewald.net/2023/12/19/rain.html",
      "url": "https://fewald.net/2023/12/19/rain.html",
      "title": "2023 Rain",
      "content_html": "<p>Rain started in the Bay Area yesterday.</p>",
      "date_published": "2023-12-19T18:00:00+00:00"
    },
    {
      "id": "https://fewald.net/ruby/rails/2023/08/13/ruby-testing-server-part-two.html",
      "url": "https://fewald.net/ruby/rails/2023/08/13/ruby-testing-server-part-two.html",
      "title": "Ruby Testing Server Part Two",
      "content_html": "<p>Following from my post from 2022 about testing Rails application with a static server, I wanted to mock an API during a test. The reason for this is that I like to keep the external dependencies to a minimum.</p><!--more--><p>Webrick supports <code class=\"language-plaintext highlighter-rouge\">mount_proc</code> which lets me create endpoints on the fly. Since my <code class=\"language-plaintext highlighter-rouge\">fixture_server</code> provides access to the <code class=\"language-plaintext highlighter-rouge\">server</code> object, it’s as simple as this:</p><figure class=\"highlight\"><pre><code class=\"language-ruby\" data-lang=\"ruby\"><span class=\"n\">fixture_server</span> <span class=\"k\">do</span> <span class=\"o\">|</span><span class=\"n\">server</span><span class=\"p\">,</span> <span class=\"n\">port</span><span class=\"o\">|</span>  <span class=\"n\">server</span><span class=\"p\">.</span><span class=\"nf\">mount_proc</span> <span class=\"s1\">'/extract_author'</span> <span class=\"k\">do</span> <span class=\"o\">|</span><span class=\"n\">req</span><span class=\"p\">,</span> <span class=\"n\">res</span><span class=\"o\">|</span>    <span class=\"n\">res</span><span class=\"p\">.</span><span class=\"nf\">body</span> <span class=\"o\">=</span> <span class=\"p\">{</span> <span class=\"ss\">authors: </span><span class=\"sx\">%w[Alice Bob]</span> <span class=\"p\">}.</span><span class=\"nf\">to_json</span>  <span class=\"k\">end</span>  <span class=\"n\">nlp</span> <span class=\"o\">=</span> <span class=\"no\">Nlp</span><span class=\"p\">.</span><span class=\"nf\">new</span><span class=\"p\">(</span><span class=\"s2\">\"http://localhost:</span><span class=\"si\">#{</span><span class=\"n\">port</span><span class=\"si\">}</span><span class=\"s2\">\"</span><span class=\"p\">)</span>  <span class=\"n\">extracted_authors</span> <span class=\"o\">=</span> <span class=\"n\">nlp</span><span class=\"p\">.</span><span class=\"nf\">extract_author</span><span class=\"p\">(</span><span class=\"s2\">\"Alice, Bob\"</span><span class=\"p\">)</span>  <span class=\"n\">assert_not_nil</span> <span class=\"n\">extracted_authors</span>  <span class=\"n\">assert_equal</span> <span class=\"sx\">%w[Alice Bob]</span><span class=\"p\">,</span> <span class=\"n\">extracted_authors</span><span class=\"k\">end</span></code></pre></figure>",
      "date_published": "2023-08-13T17:44:00+00:00"
    },
    {
      "id": "https://fewald.net/2023/07/04/july-4-2023.html",
      "url": "https://fewald.net/2023/07/04/july-4-2023.html",
      "title": "July 4 2023",
      "content_html": "<p><a href=\"/assets/images/golden-gate-bridge.jpeg\"><picture><source srcset=\"/generated/assets/images/golden-gate-bridge-200-3b912ce1e.avif 200w, /generated/assets/images/golden-gate-bridge-400-3b912ce1e.avif 400w, /generated/assets/images/golden-gate-bridge-800-3b912ce1e.avif 800w, /generated/assets/images/golden-gate-bridge-1200-3b912ce1e.avif 1200w\" type=\"image/avif\"><source srcset=\"/generated/assets/images/golden-gate-bridge-200-50cc4d734.webp 200w, /generated/assets/images/golden-gate-bridge-400-50cc4d734.webp 400w, /generated/assets/images/golden-gate-bridge-800-50cc4d734.webp 800w, /generated/assets/images/golden-gate-bridge-1200-50cc4d734.webp 1200w\" type=\"image/webp\"><source srcset=\"/generated/assets/images/golden-gate-bridge-200-c5bfe6f92.jpeg 200w, /generated/assets/images/golden-gate-bridge-400-c5bfe6f92.jpeg 400w, /generated/assets/images/golden-gate-bridge-800-c5bfe6f92.jpeg 800w, /generated/assets/images/golden-gate-bridge-1200-c5bfe6f92.jpeg 1200w\" type=\"image/jpeg\"><img src=\"/generated/assets/images/golden-gate-bridge-800-c5bfe6f92.jpeg\" alt=\"Golden Gate Bridge in San Francisco, CA on 4th of July weekend\" width=\"1280\" height=\"960\"></picture></a>Happy 4th of July from San Francisco!</p>",
      "date_published": "2023-07-04T14:17:00+00:00"
    },
    {
      "id": "https://fewald.net/misc/2023/05/21/bread-recipe.html",
      "url": "https://fewald.net/misc/2023/05/21/bread-recipe.html",
      "title": "Bread recipe",
      "content_html": "<p>I made my first bread today from scratch. The recipe is very simple:</p><ol>  <li>430 grams flour (King Arthurs bread flour)</li>  <li>2 teaspoons coarse salt</li>  <li>1 bag (6-7 grams) of instant yeast</li>  <li>1 1/2 cups of water</li></ol><p>Mix everything gently together in a bowl until the dough becomes sticky. Then proof at 95 Fahrenheit for 1 hour until the dough doubles in size.</p><p>Preheat the oven to <strong>475 Fahrenheit</strong> and bake for <strong>20 minutes</strong>.</p><p><a href=\"/assets/images/bread-in-oven.jpeg\"><picture><source srcset=\"/generated/assets/images/bread-in-oven-200-dda192b67.avif 200w, /generated/assets/images/bread-in-oven-400-dda192b67.avif 400w, /generated/assets/images/bread-in-oven-800-dda192b67.avif 800w, /generated/assets/images/bread-in-oven-1200-dda192b67.avif 1200w\" type=\"image/avif\"><source srcset=\"/generated/assets/images/bread-in-oven-200-41d0cb29a.webp 200w, /generated/assets/images/bread-in-oven-400-41d0cb29a.webp 400w, /generated/assets/images/bread-in-oven-800-41d0cb29a.webp 800w, /generated/assets/images/bread-in-oven-1200-41d0cb29a.webp 1200w\" type=\"image/webp\"><source srcset=\"/generated/assets/images/bread-in-oven-200-8f01ee7aa.jpeg 200w, /generated/assets/images/bread-in-oven-400-8f01ee7aa.jpeg 400w, /generated/assets/images/bread-in-oven-800-8f01ee7aa.jpeg 800w, /generated/assets/images/bread-in-oven-1200-8f01ee7aa.jpeg 1200w\" type=\"image/jpeg\"><img src=\"/generated/assets/images/bread-in-oven-800-8f01ee7aa.jpeg\" alt=\"Bread in the oven\" width=\"1280\" height=\"854\"></picture></a></p><p>The results were good for a first attempt, but I will try to proof for 2 hours next to achieve fluffier results.</p><p><a href=\"/assets/images/bread-cut.jpeg\"><picture><source srcset=\"/generated/assets/images/bread-cut-200-e05407fce.avif 200w, /generated/assets/images/bread-cut-400-e05407fce.avif 400w, /generated/assets/images/bread-cut-800-e05407fce.avif 800w, /generated/assets/images/bread-cut-1200-e05407fce.avif 1200w\" type=\"image/avif\"><source srcset=\"/generated/assets/images/bread-cut-200-4e4a1f47e.webp 200w, /generated/assets/images/bread-cut-400-4e4a1f47e.webp 400w, /generated/assets/images/bread-cut-800-4e4a1f47e.webp 800w, /generated/assets/images/bread-cut-1200-4e4a1f47e.webp 1200w\" type=\"image/webp\"><source srcset=\"/generated/assets/images/bread-cut-200-6fbab8b36.jpeg 200w, /generated/assets/images/bread-cut-400-6fbab8b36.jpeg 400w, /generated/assets/images/bread-cut-800-6fbab8b36.jpeg 800w, /generated/assets/images/bread-cut-1200-6fbab8b36.jpeg 1200w\" type=\"image/jpeg\"><img src=\"/generated/assets/images/bread-cut-800-6fbab8b36.jpeg\" alt=\"Bread after cutting\" width=\"1280\" height=\"854\"></picture></a></p>",
      "date_published": "2023-05-21T01:30:00+00:00"
    },
    {
      "id": "https://fewald.net/development/python/2023/04/18/memory-profiling-memray.html",
      "url": "https://fewald.net/development/python/2023/04/18/memory-profiling-memray.html",
      "title": "Memory profiling with Memray",
      "content_html": "<p>I am currently working on a small Python project with a relatively large memory footprint. I wanted toanalyze the usage in detail and realized that I wasn’t aware of any good Python memory profiler. Aftersome searching I found the builtin package <a href=\"https://docs.python.org/3/library/tracemalloc.html\">tracemalloc</a>(Trace memory allocation). Especially interesting to me is the capability to take snapshots at differentpoints in time and then compare the delta between those snaphots. The following snippet illustrates this.</p><figure class=\"highlight\"><pre><code class=\"language-python\" data-lang=\"python\"><span class=\"kn\">import</span> <span class=\"n\">numpy</span><span class=\"kn\">import</span> <span class=\"n\">tracemalloc</span><span class=\"n\">tracemalloc</span><span class=\"p\">.</span><span class=\"nf\">start</span><span class=\"p\">()</span><span class=\"n\">snapshot_before</span> <span class=\"o\">=</span> <span class=\"n\">tracemalloc</span><span class=\"p\">.</span><span class=\"nf\">take_snapshot</span><span class=\"p\">()</span><span class=\"n\">array</span> <span class=\"o\">=</span> <span class=\"n\">numpy</span><span class=\"p\">.</span><span class=\"nf\">zeros</span><span class=\"p\">((</span><span class=\"mi\">100</span><span class=\"p\">,</span> <span class=\"mi\">100</span><span class=\"p\">))</span><span class=\"n\">array2</span> <span class=\"o\">=</span> <span class=\"n\">numpy</span><span class=\"p\">.</span><span class=\"n\">random</span><span class=\"p\">.</span><span class=\"nf\">rand</span><span class=\"p\">(</span><span class=\"mi\">1_000_000</span><span class=\"p\">,</span> <span class=\"mi\">1_000</span><span class=\"p\">)</span><span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"n\">array2</span><span class=\"p\">.</span><span class=\"n\">dtype</span><span class=\"p\">)</span><span class=\"n\">array3</span> <span class=\"o\">=</span> <span class=\"n\">array2</span><span class=\"p\">.</span><span class=\"nf\">astype</span><span class=\"p\">(</span><span class=\"n\">numpy</span><span class=\"p\">.</span><span class=\"n\">float16</span><span class=\"p\">)</span><span class=\"n\">snapshot_after</span> <span class=\"o\">=</span> <span class=\"n\">tracemalloc</span><span class=\"p\">.</span><span class=\"nf\">take_snapshot</span><span class=\"p\">()</span><span class=\"n\">stats</span> <span class=\"o\">=</span> <span class=\"n\">snapshot_after</span><span class=\"p\">.</span><span class=\"nf\">compare_to</span><span class=\"p\">(</span><span class=\"n\">snapshot_before</span><span class=\"p\">,</span> <span class=\"sh\">'</span><span class=\"s\">lineno</span><span class=\"sh\">'</span><span class=\"p\">)</span><span class=\"k\">for</span> <span class=\"n\">s</span> <span class=\"ow\">in</span> <span class=\"n\">stats</span><span class=\"p\">[:</span><span class=\"mi\">10</span><span class=\"p\">]:</span>    <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"n\">s</span><span class=\"p\">)</span><span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"n\">tracemalloc</span><span class=\"p\">.</span><span class=\"nf\">get_traced_memory</span><span class=\"p\">())</span></code></pre></figure><p>This is fine for smaller code bases where I have full control over the source. It requires however alot of editing and manual filtering of frames and taking snapshots at the right time.</p><p>Another interesting solution is Bloombergs <a href=\"https://github.com/bloomberg/memray\">memray</a>. This allows to run a whole Python application and save the snapshots into a file. From there memray can either create a table or a flamegraph to see the memory consumption over time.</p><p>Memray can be used like this:</p><figure class=\"highlight\"><pre><code class=\"language-shell\" data-lang=\"shell\">pip3 <span class=\"nb\">install </span>memray<span class=\"c\"># Run application and save snapshots</span>memray run my_app.py<span class=\"c\"># Display snapshots</span>memray flamegraph memray-test.py.94623.bin</code></pre></figure><p>This generates a HTML file which can be opened in any browser.</p>",
      "date_published": "2023-04-18T17:00:00+00:00"
    },
    {
      "id": "https://fewald.net/development/2023/01/25/updating-jekyll-to-ruby-3-2-0.html",
      "url": "https://fewald.net/development/2023/01/25/updating-jekyll-to-ruby-3-2-0.html",
      "title": "Updating Jekyll to Ruby 3.2.0",
      "content_html": "<p>I recently updated this blog to <a href=\"https://www.ruby-lang.org/en/news/2022/12/25/ruby-3-2-0-released/\">Ruby 3.2.0</a> which was released on Dec 25, 2022.</p><p>I expected this to be a minor update since I came from Ruby 3.1.2. However, I ran into some problems that I had to solve.</p><!--more--><h2 id=\"outdated-api\">Outdated API</h2><p>It turns out that there is one important change in the Ruby standard library. <code class=\"language-plaintext highlighter-rouge\">File.exists?</code> was renamed to <code class=\"language-plaintext highlighter-rouge\">File.exist?</code> with the same functionality. Some third-party plugins that I use have not yet included this change. I was able to extend the <code class=\"language-plaintext highlighter-rouge\">File</code> class like so:</p><figure class=\"highlight\"><pre><code class=\"language-ruby\" data-lang=\"ruby\"><span class=\"k\">class</span> <span class=\"nc\">File</span>  <span class=\"k\">def</span> <span class=\"nc\">self</span><span class=\"o\">.</span><span class=\"nf\">exists?</span><span class=\"p\">(</span><span class=\"o\">*</span><span class=\"n\">args</span><span class=\"p\">)</span>    <span class=\"n\">exist?</span><span class=\"p\">(</span><span class=\"o\">*</span><span class=\"n\">args</span><span class=\"p\">)</span>  <span class=\"k\">end</span><span class=\"k\">end</span></code></pre></figure><p>I added this file as in the <code class=\"language-plaintext highlighter-rouge\">_plugins</code> folder so that Jekyll would import it automatically.</p><h1 id=\"updating-jekyll\">Updating Jekyll</h1><p>The version of Jekyll that I used was not compatible with Ruby 3.2.0. I had to update my <code class=\"language-plaintext highlighter-rouge\">Gemfile</code> to the following and then run <code class=\"language-plaintext highlighter-rouge\">bundle update</code>. This will apply updates to Jekyll automatically in the future.</p><figure class=\"highlight\"><pre><code class=\"language-gem\" data-lang=\"gem\"># ...ruby \"3.2.0\"# ...gem \"jekyll\", \"~&gt; 4\"</code></pre></figure><h2 id=\"sass-compiler\">SASS Compiler</h2><p>With the Jekyll update came also an update to the SASS compiler.My <code class=\"language-plaintext highlighter-rouge\">css</code> folder contains one file: <code class=\"language-plaintext highlighter-rouge\">main.scss</code>. This file has just some import statements that it uses to assemble a larger CSS file. I got an error message that instead of the <code class=\"language-plaintext highlighter-rouge\">;</code> at the end of each line, curly braces are expected. I spent quite some time to try to figure out what is going on, until I saw the changelog on the Jekyll homepage. It turns out that the <code class=\"language-plaintext highlighter-rouge\">main.scss</code> cannot have the same name as the imported <code class=\"language-plaintext highlighter-rouge\">_main.scss</code>. After renaming it to <code class=\"language-plaintext highlighter-rouge\">_base.scss</code>, it worked. See the last line in the snippet below for an example.</p><figure class=\"highlight\"><pre><code class=\"language-sass\" data-lang=\"sass\"><span class=\"nt\">---</span><span class=\"nn\">#</span> <span class=\"nt\">Front</span> <span class=\"nt\">matter</span> <span class=\"nt\">is</span> <span class=\"nt\">needed</span> <span class=\"nt\">here</span><span class=\"nt\">---</span><span class=\"k\">@charset</span> <span class=\"s2\">\"utf-8\"</span><span class=\"o\">;</span><span class=\"k\">@use</span> <span class=\"s2\">\"syntax\"</span><span class=\"o\">;</span><span class=\"k\">@use</span> <span class=\"s2\">\"poole\"</span><span class=\"o\">;</span><span class=\"k\">@use</span> <span class=\"s2\">\"lanyon\"</span><span class=\"o\">;</span><span class=\"k\">@use</span> <span class=\"s2\">\"base\"</span><span class=\"o\">;</span></code></pre></figure><p>After those steps, everything worked again and this site is now generated with Ruby 3.2.0!</p>",
      "date_published": "2023-01-25T16:35:00+00:00"
    },
    {
      "id": "https://fewald.net/home-improvement/2022/12/18/electric-closet.html",
      "url": "https://fewald.net/home-improvement/2022/12/18/electric-closet.html",
      "title": "Electric Closet",
      "content_html": "<p>I never liked having the router in full display in the living room. I routed the fiber connection into a closet in our hallway with the plan on making this a “tech closet”. I had to route electricity to the closet and install some shelves. The goal was to have a space for the router, my printer (that I only use ocassionally), some other hardware like a Raspberry Pi and to be able to charge a Dyson vacuum cleaner.</p><p><a href=\"/assets/images/electric-closet/full-closet.jpeg\"><picture><source srcset=\"/generated/assets/images/electric-closet/full-closet-200-5bf80a708.avif 200w, /generated/assets/images/electric-closet/full-closet-400-5bf80a708.avif 400w, /generated/assets/images/electric-closet/full-closet-800-5bf80a708.avif 800w, /generated/assets/images/electric-closet/full-closet-1200-5bf80a708.avif 1200w\" type=\"image/avif\"><source srcset=\"/generated/assets/images/electric-closet/full-closet-200-58d2892b7.webp 200w, /generated/assets/images/electric-closet/full-closet-400-58d2892b7.webp 400w, /generated/assets/images/electric-closet/full-closet-800-58d2892b7.webp 800w, /generated/assets/images/electric-closet/full-closet-1200-58d2892b7.webp 1200w\" type=\"image/webp\"><source srcset=\"/generated/assets/images/electric-closet/full-closet-200-f4fc86ca4.jpeg 200w, /generated/assets/images/electric-closet/full-closet-400-f4fc86ca4.jpeg 400w, /generated/assets/images/electric-closet/full-closet-800-f4fc86ca4.jpeg 800w, /generated/assets/images/electric-closet/full-closet-1200-f4fc86ca4.jpeg 1200w\" type=\"image/jpeg\"><img src=\"/generated/assets/images/electric-closet/full-closet-800-f4fc86ca4.jpeg\" width=\"2107\" height=\"3160\"></picture></a>The (almost) finished closet. The cover plate for the outlet was not available at the time.</p><p><a href=\"/assets/images/electric-closet/wago-wiring-outlets.jpeg\"><picture><source srcset=\"/generated/assets/images/electric-closet/wago-wiring-outlets-200-acfa365f5.avif 200w, /generated/assets/images/electric-closet/wago-wiring-outlets-400-acfa365f5.avif 400w, /generated/assets/images/electric-closet/wago-wiring-outlets-800-acfa365f5.avif 800w, /generated/assets/images/electric-closet/wago-wiring-outlets-1200-acfa365f5.avif 1200w\" type=\"image/avif\"><source srcset=\"/generated/assets/images/electric-closet/wago-wiring-outlets-200-9b9fab8e7.webp 200w, /generated/assets/images/electric-closet/wago-wiring-outlets-400-9b9fab8e7.webp 400w, /generated/assets/images/electric-closet/wago-wiring-outlets-800-9b9fab8e7.webp 800w, /generated/assets/images/electric-closet/wago-wiring-outlets-1200-9b9fab8e7.webp 1200w\" type=\"image/webp\"><source srcset=\"/generated/assets/images/electric-closet/wago-wiring-outlets-200-dc152eaf9.jpeg 200w, /generated/assets/images/electric-closet/wago-wiring-outlets-400-dc152eaf9.jpeg 400w, /generated/assets/images/electric-closet/wago-wiring-outlets-800-dc152eaf9.jpeg 800w, /generated/assets/images/electric-closet/wago-wiring-outlets-1200-dc152eaf9.jpeg 1200w\" type=\"image/jpeg\"><img src=\"/generated/assets/images/electric-closet/wago-wiring-outlets-800-dc152eaf9.jpeg\" width=\"2842\" height=\"1895\"></picture></a>I connected the outlets with Wago connectors before installing them. This avoided having to install the outlets in an awkward position and sped the whole process up significantly.</p><p><a href=\"/assets/images/electric-closet/detail-below.jpeg\"><picture><source srcset=\"/generated/assets/images/electric-closet/detail-below-200-d48f07cf8.avif 200w, /generated/assets/images/electric-closet/detail-below-400-d48f07cf8.avif 400w, /generated/assets/images/electric-closet/detail-below-800-d48f07cf8.avif 800w, /generated/assets/images/electric-closet/detail-below-1200-d48f07cf8.avif 1200w\" type=\"image/avif\"><source srcset=\"/generated/assets/images/electric-closet/detail-below-200-8964a8e72.webp 200w, /generated/assets/images/electric-closet/detail-below-400-8964a8e72.webp 400w, /generated/assets/images/electric-closet/detail-below-800-8964a8e72.webp 800w, /generated/assets/images/electric-closet/detail-below-1200-8964a8e72.webp 1200w\" type=\"image/webp\"><source srcset=\"/generated/assets/images/electric-closet/detail-below-200-60812ee93.jpeg 200w, /generated/assets/images/electric-closet/detail-below-400-60812ee93.jpeg 400w, /generated/assets/images/electric-closet/detail-below-800-60812ee93.jpeg 800w, /generated/assets/images/electric-closet/detail-below-1200-60812ee93.jpeg 1200w\" type=\"image/jpeg\"><img src=\"/generated/assets/images/electric-closet/detail-below-800-60812ee93.jpeg\" alt=\"View from below\" width=\"4032\" height=\"3024\"></picture></a>The view from below. I used normal wood screws to attach the boards to the wall. Time will tell if this is sufficient. In case this is not stable enough: Behind the drywall are some studs at exactly the same position.</p><p><a href=\"/assets/images/electric-closet/detail-left.jpeg\"><picture><source srcset=\"/generated/assets/images/electric-closet/detail-left-200-bd39f05db.avif 200w, /generated/assets/images/electric-closet/detail-left-400-bd39f05db.avif 400w, /generated/assets/images/electric-closet/detail-left-800-bd39f05db.avif 800w, /generated/assets/images/electric-closet/detail-left-1200-bd39f05db.avif 1200w\" type=\"image/avif\"><source srcset=\"/generated/assets/images/electric-closet/detail-left-200-10b0aec1a.webp 200w, /generated/assets/images/electric-closet/detail-left-400-10b0aec1a.webp 400w, /generated/assets/images/electric-closet/detail-left-800-10b0aec1a.webp 800w, /generated/assets/images/electric-closet/detail-left-1200-10b0aec1a.webp 1200w\" type=\"image/webp\"><source srcset=\"/generated/assets/images/electric-closet/detail-left-200-75c31f7c3.jpeg 200w, /generated/assets/images/electric-closet/detail-left-400-75c31f7c3.jpeg 400w, /generated/assets/images/electric-closet/detail-left-800-75c31f7c3.jpeg 800w, /generated/assets/images/electric-closet/detail-left-1200-75c31f7c3.jpeg 1200w\" type=\"image/jpeg\"><img src=\"/generated/assets/images/electric-closet/detail-left-800-75c31f7c3.jpeg\" alt=\"View in the left corner from below\" width=\"3755\" height=\"2816\"></picture></a></p><p><a href=\"/assets/images/electric-closet/detail-divided.jpeg\"><picture><source srcset=\"/generated/assets/images/electric-closet/detail-divided-200-a39645a84.avif 200w, /generated/assets/images/electric-closet/detail-divided-400-a39645a84.avif 400w, /generated/assets/images/electric-closet/detail-divided-800-a39645a84.avif 800w, /generated/assets/images/electric-closet/detail-divided-1200-a39645a84.avif 1200w\" type=\"image/avif\"><source srcset=\"/generated/assets/images/electric-closet/detail-divided-200-c801e5dc5.webp 200w, /generated/assets/images/electric-closet/detail-divided-400-c801e5dc5.webp 400w, /generated/assets/images/electric-closet/detail-divided-800-c801e5dc5.webp 800w, /generated/assets/images/electric-closet/detail-divided-1200-c801e5dc5.webp 1200w\" type=\"image/webp\"><source srcset=\"/generated/assets/images/electric-closet/detail-divided-200-850289c26.jpeg 200w, /generated/assets/images/electric-closet/detail-divided-400-850289c26.jpeg 400w, /generated/assets/images/electric-closet/detail-divided-800-850289c26.jpeg 800w, /generated/assets/images/electric-closet/detail-divided-1200-850289c26.jpeg 1200w\" type=\"image/jpeg\"><img src=\"/generated/assets/images/electric-closet/detail-divided-800-850289c26.jpeg\" alt=\"Smaller, divided boards\" width=\"4032\" height=\"3024\"></picture></a>I chose to have some board divided in the middle and not spanning the whole depth of the closet. This allows flexibility and also to store bigger items.</p>",
      "date_published": "2022-12-18T01:50:00+00:00"
    },
    {
      "id": "https://fewald.net/machine-learning/2022/12/11/chatgpt.html",
      "url": "https://fewald.net/machine-learning/2022/12/11/chatgpt.html",
      "title": "ChatGPT",
      "content_html": "<p>Who are you?</p><blockquote>  <p>I am Assistant, a large language model trained by OpenAI to assist with a variety of tasks. I am not a person, but rather a program that uses advanced machine learning algorithms to process and generate text. I am here to provide information and help answer your questions to the best of my ability. Do you have any questions I can help you with?</p></blockquote><p>Do you have any special skills?</p><blockquote>  <p>As a language model, my primary skill is generating and understanding natural language text. I am trained to assist with a variety of tasks, including answering questions, providing information, and engaging in conversation. I am constantly learning and improving my abilities through machine learning, so my capabilities are always expanding. Is there something specific you would like to know or discuss? I would be happy to help with any question or topic you have in mind.</p></blockquote><p>When being asked about ChatGPT, it doesn’t know itself. Likely because it was trained on data ending in 2021 which is before <a href=\"https://chat.openai.com\">ChatGPT</a> was published. I recommend trying it out.</p><h3 id=\"update-jan-25-2023\">Update Jan 25 2023</h3><p>Since a few days, ChatGPT has a premium plan. Anecdotaelly, I just went there and I got a message “ChatGPT is at capacity right now”. It seems like the free days are over.</p>",
      "date_published": "2022-12-11T16:25:00+00:00"
    },
    {
      "id": "https://fewald.net/development/2022/11/28/testing-email-sending.html",
      "url": "https://fewald.net/development/2022/11/28/testing-email-sending.html",
      "title": "Testing email sending",
      "content_html": "<p>I recently came across <a href=\"https://github.com/mailhog/MailHog\">MailHog</a>. I needed to test some email functionality locally. MailHog creates an SMTP that accepts any email to any address and shows this in web interface.</p>",
      "date_published": "2022-11-28T16:14:00+00:00"
    },
    {
      "id": "https://fewald.net/2022/11/18/heroku-price-increase.html",
      "url": "https://fewald.net/2022/11/18/heroku-price-increase.html",
      "title": "Heroku Price increase",
      "content_html": "<p>Heroku will increase <a href=\"https://www.heroku.com/pricing\">prices</a> and remove many free tiers.</p>",
      "date_published": "2022-11-18T18:44:00+00:00"
    },
    {
      "id": "https://fewald.net/development/2022/11/16/change-font-size-depending-on-post-length.html",
      "url": "https://fewald.net/development/2022/11/16/change-font-size-depending-on-post-length.html",
      "title": "Change font size depending on post length",
      "content_html": "<p>One of the things that I like about Twitter is that the font size changes depending on the length of the tweet. Yesterday, I tried to achieve the same thing for this Jekyll based blog and came up with the following solution.</p><p>First, I created a plugin (in the folder <code class=\"language-plaintext highlighter-rouge\">_plugins</code>) and registered it as <code class=\"language-plaintext highlighter-rouge\">Liquid</code> filter with the following code:</p><figure class=\"highlight\"><pre><code class=\"language-ruby\" data-lang=\"ruby\"><span class=\"k\">module</span> <span class=\"nn\">Jekyll</span>  <span class=\"k\">module</span> <span class=\"nn\">PostFilter</span>    <span class=\"k\">def</span> <span class=\"nf\">post_class</span><span class=\"p\">(</span><span class=\"n\">input</span><span class=\"p\">)</span>      <span class=\"c1\"># Catch invalid posts</span>      <span class=\"k\">return</span> <span class=\"s1\">'default'</span> <span class=\"k\">if</span> <span class=\"n\">input</span><span class=\"p\">.</span><span class=\"nf\">nil?</span>            <span class=\"k\">if</span> <span class=\"n\">input</span><span class=\"p\">.</span><span class=\"nf\">size</span> <span class=\"o\">&lt;</span> <span class=\"mi\">32</span>        <span class=\"k\">return</span> <span class=\"s1\">'large'</span>      <span class=\"k\">elsif</span> <span class=\"n\">input</span><span class=\"p\">.</span><span class=\"nf\">size</span> <span class=\"o\">&lt;</span> <span class=\"mi\">64</span>        <span class=\"k\">return</span> <span class=\"s1\">'medium'</span>      <span class=\"k\">else</span>        <span class=\"k\">return</span> <span class=\"s1\">'default'</span>      <span class=\"k\">end</span>    <span class=\"k\">end</span>  <span class=\"k\">end</span><span class=\"k\">end</span><span class=\"no\">Liquid</span><span class=\"o\">::</span><span class=\"no\">Template</span><span class=\"p\">.</span><span class=\"nf\">register_filter</span><span class=\"p\">(</span><span class=\"no\">Jekyll</span><span class=\"o\">::</span><span class=\"no\">PostFilter</span><span class=\"p\">)</span></code></pre></figure><p>As a next step I changed the <code class=\"language-plaintext highlighter-rouge\">post.html</code> template to this:</p><figure class=\"highlight\"><pre><code class=\"language-html\" data-lang=\"html\"><span class=\"nt\">&lt;p</span> <span class=\"na\">itemprop=</span><span class=\"s\">\"articleBody\"</span> <span class=\"na\">class=</span><span class=\"s\">\"{{content | post_class }}\"</span><span class=\"nt\">&gt;</span>  {{ content | remove: \"<span class=\"nt\">&lt;p&gt;</span>\" | remove: \"<span class=\"nt\">&lt;/p&gt;</span>\" }}<span class=\"nt\">&lt;/p&gt;</span></code></pre></figure><p>It is important to remove the <code class=\"language-plaintext highlighter-rouge\">&lt;p&gt;</code> tags because they cannot be nested in valid HTML and Jekyll adds their own tags to post content.</p><p>Finally, I created some SCSS to change the <code class=\"language-plaintext highlighter-rouge\">font-size</code> based on the CSS class.</p><figure class=\"highlight\"><pre><code class=\"language-scss\" data-lang=\"scss\"><span class=\"cm\">/* Post pages */</span><span class=\"nt\">div</span><span class=\"nc\">.post</span> <span class=\"p\">{</span>    <span class=\"nt\">p</span><span class=\"nc\">.default</span> <span class=\"p\">{</span>        <span class=\"nl\">font-size</span><span class=\"p\">:</span> <span class=\"m\">1em</span><span class=\"p\">;</span>    <span class=\"p\">}</span>    <span class=\"nt\">p</span><span class=\"nc\">.medium</span> <span class=\"p\">{</span>        <span class=\"nl\">font-size</span><span class=\"p\">:</span> <span class=\"m\">1</span><span class=\"mi\">.6em</span><span class=\"p\">;</span>    <span class=\"p\">}</span>    <span class=\"nt\">p</span><span class=\"nc\">.large</span> <span class=\"p\">{</span>        <span class=\"nl\">font-size</span><span class=\"p\">:</span> <span class=\"m\">1</span><span class=\"mi\">.8em</span><span class=\"p\">;</span>    <span class=\"p\">}</span><span class=\"p\">}</span></code></pre></figure>",
      "date_published": "2022-11-16T15:43:00+00:00"
    },
    {
      "id": "https://fewald.net/leetcode/2022/11/09/leetcode-house-robber-ii.html",
      "url": "https://fewald.net/leetcode/2022/11/09/leetcode-house-robber-ii.html",
      "title": "Leetcode: House robber II",
      "content_html": "<p>Similar to “House Robber”, but now we have to assume that the houses are aligned in a circle and the last house is a neighbor of the first house.</p><!--more--><p>We can solve it similarly but remove the first and last element and take the maximum of the result.</p><figure class=\"highlight\"><pre><code class=\"language-python\" data-lang=\"python\"><span class=\"k\">class</span> <span class=\"nc\">Solution</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">rob</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">nums</span><span class=\"p\">:</span> <span class=\"n\">List</span><span class=\"p\">[</span><span class=\"nb\">int</span><span class=\"p\">])</span> <span class=\"o\">-&gt;</span> <span class=\"nb\">int</span><span class=\"p\">:</span>    <span class=\"k\">if</span> <span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">nums</span><span class=\"p\">)</span> <span class=\"o\">&lt;</span> <span class=\"mi\">2</span><span class=\"p\">:</span>      <span class=\"k\">return</span> <span class=\"n\">nums</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span>    <span class=\"k\">return</span> <span class=\"nf\">max</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">.</span><span class=\"nf\">r</span><span class=\"p\">(</span><span class=\"n\">nums</span><span class=\"p\">[:</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">]),</span> <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"nf\">r</span><span class=\"p\">(</span><span class=\"n\">nums</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">:]))</span>  <span class=\"k\">def</span> <span class=\"nf\">r</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">nums</span><span class=\"p\">):</span>      <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"n\">nums</span><span class=\"p\">)</span>      <span class=\"k\">if</span> <span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">nums</span><span class=\"p\">)</span> <span class=\"o\">&lt;</span> <span class=\"mi\">2</span><span class=\"p\">:</span>        <span class=\"k\">return</span> <span class=\"n\">nums</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span>      <span class=\"k\">if</span> <span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">nums</span><span class=\"p\">)</span> <span class=\"o\">&lt;</span> <span class=\"mi\">3</span><span class=\"p\">:</span>        <span class=\"k\">return</span> <span class=\"nf\">max</span><span class=\"p\">(</span><span class=\"n\">nums</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">],</span> <span class=\"n\">nums</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">])</span>      <span class=\"n\">m</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"mi\">0</span> <span class=\"k\">for</span> <span class=\"n\">_</span> <span class=\"ow\">in</span> <span class=\"n\">nums</span><span class=\"p\">]</span>      <span class=\"n\">m</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"n\">nums</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span>      <span class=\"n\">m</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"n\">nums</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">]</span>      <span class=\"n\">m</span><span class=\"p\">[</span><span class=\"mi\">2</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"nf\">max</span><span class=\"p\">(</span><span class=\"n\">m</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span><span class=\"o\">+</span><span class=\"n\">nums</span><span class=\"p\">[</span><span class=\"mi\">2</span><span class=\"p\">],</span> <span class=\"n\">m</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">])</span>            <span class=\"k\">for</span> <span class=\"n\">pos</span> <span class=\"ow\">in</span> <span class=\"nf\">range</span><span class=\"p\">(</span><span class=\"mi\">3</span><span class=\"p\">,</span> <span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">nums</span><span class=\"p\">)):</span>        <span class=\"n\">m</span><span class=\"p\">[</span><span class=\"n\">pos</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"nf\">max</span><span class=\"p\">(</span><span class=\"n\">m</span><span class=\"p\">[</span><span class=\"n\">pos</span><span class=\"o\">-</span><span class=\"mi\">2</span><span class=\"p\">],</span> <span class=\"n\">m</span><span class=\"p\">[</span><span class=\"n\">pos</span><span class=\"o\">-</span><span class=\"mi\">3</span><span class=\"p\">])</span> <span class=\"o\">+</span> <span class=\"n\">nums</span><span class=\"p\">[</span><span class=\"n\">pos</span><span class=\"p\">]</span>            <span class=\"k\">return</span> <span class=\"nf\">max</span><span class=\"p\">(</span><span class=\"n\">m</span><span class=\"p\">[</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">],</span> <span class=\"n\">m</span><span class=\"p\">[</span><span class=\"o\">-</span><span class=\"mi\">2</span><span class=\"p\">])</span></code></pre></figure><blockquote>  <p>Runtime: 63 ms, faster than 22.78% of Python3 online submissions for House Robber II. Memory Usage: 13.9 MB, less than 70.83% of Python3 online submissions for House Robber II.</p></blockquote>",
      "date_published": "2022-11-09T20:00:00+00:00"
    },
    {
      "id": "https://fewald.net/leetcode/2022/11/08/leetcode-house-robber.html",
      "url": "https://fewald.net/leetcode/2022/11/08/leetcode-house-robber.html",
      "title": "Leetcode: House robber",
      "content_html": "<p>Given an array of <code class=\"language-plaintext highlighter-rouge\">nums</code> that represent the loot of a house robber, return the maximum amount of money a robber can steal. The only rule is that a robber can not steal from two neighboring houses.</p><p>When looking at this problem, we can see that we want to maximize the number of houses that we rob from. Consider the following array: <code class=\"language-plaintext highlighter-rouge\">[0,1,0,1]</code>. Our maximum profit is <code class=\"language-plaintext highlighter-rouge\">2</code>, because we can rob the house at index <code class=\"language-plaintext highlighter-rouge\">1</code> and index <code class=\"language-plaintext highlighter-rouge\">3</code>. If we were to rob at index <code class=\"language-plaintext highlighter-rouge\">0</code> and <code class=\"language-plaintext highlighter-rouge\">2</code>, we would only gain <code class=\"language-plaintext highlighter-rouge\">0</code> profit. We cannot however simply compare the even versus the odd indices because there could also be the following situation: <code class=\"language-plaintext highlighter-rouge\">[10,5,5,10]</code>. Here, it is advantageous to skip <code class=\"language-plaintext highlighter-rouge\">2</code> instead of just skipping <code class=\"language-plaintext highlighter-rouge\">1</code>. We need to look at every step if we want to visit the one after the next or the one after this. We don’t need to look if we want to visit the one after this, because then we could also simply have visited the one after the next.</p><!--more--><p>Generally speaking, we always need to look at the <code class=\"language-plaintext highlighter-rouge\">index+2</code> and at the <code class=\"language-plaintext highlighter-rouge\">index+3</code>. To solve this problem, we can look backwards at the array. At position <code class=\"language-plaintext highlighter-rouge\">0</code>, we can only rob <code class=\"language-plaintext highlighter-rouge\">0</code>. At position <code class=\"language-plaintext highlighter-rouge\">1</code> we can only rob <code class=\"language-plaintext highlighter-rouge\">1</code>. At position <code class=\"language-plaintext highlighter-rouge\">2</code> we can rob <code class=\"language-plaintext highlighter-rouge\">0</code> and <code class=\"language-plaintext highlighter-rouge\">2</code>. From there on, we can iterate over the array and calculate the maximum for each position and add the current loot. Finally, we can return the maximum of the two last positions of the array.</p><figure class=\"highlight\"><pre><code class=\"language-python\" data-lang=\"python\"><span class=\"kn\">from</span> <span class=\"n\">typing</span> <span class=\"kn\">import</span> <span class=\"n\">List</span><span class=\"k\">class</span> <span class=\"nc\">Solution</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">rob</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">nums</span><span class=\"p\">:</span> <span class=\"n\">List</span><span class=\"p\">[</span><span class=\"nb\">int</span><span class=\"p\">])</span> <span class=\"o\">-&gt;</span> <span class=\"nb\">int</span><span class=\"p\">:</span>    <span class=\"k\">if</span> <span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">nums</span><span class=\"p\">)</span> <span class=\"o\">&lt;</span> <span class=\"mi\">2</span><span class=\"p\">:</span>      <span class=\"k\">return</span> <span class=\"n\">nums</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span>    <span class=\"k\">if</span> <span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">nums</span><span class=\"p\">)</span> <span class=\"o\">&lt;</span> <span class=\"mi\">3</span><span class=\"p\">:</span>      <span class=\"k\">return</span> <span class=\"nf\">max</span><span class=\"p\">(</span><span class=\"n\">nums</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">],</span> <span class=\"n\">nums</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">])</span>    <span class=\"n\">m</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"mi\">0</span> <span class=\"k\">for</span> <span class=\"n\">_</span> <span class=\"ow\">in</span> <span class=\"n\">nums</span><span class=\"p\">]</span>    <span class=\"n\">m</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"n\">nums</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span>    <span class=\"n\">m</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"n\">nums</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">]</span>    <span class=\"n\">m</span><span class=\"p\">[</span><span class=\"mi\">2</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"n\">m</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span> <span class=\"o\">+</span> <span class=\"n\">nums</span><span class=\"p\">[</span><span class=\"mi\">2</span><span class=\"p\">]</span>    <span class=\"k\">for</span> <span class=\"n\">pos</span> <span class=\"ow\">in</span> <span class=\"nf\">range</span><span class=\"p\">(</span><span class=\"mi\">3</span><span class=\"p\">,</span> <span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">nums</span><span class=\"p\">)):</span>      <span class=\"n\">m</span><span class=\"p\">[</span><span class=\"n\">pos</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"nf\">max</span><span class=\"p\">(</span><span class=\"n\">m</span><span class=\"p\">[</span><span class=\"n\">pos</span><span class=\"o\">-</span><span class=\"mi\">2</span><span class=\"p\">],</span> <span class=\"n\">m</span><span class=\"p\">[</span><span class=\"n\">pos</span><span class=\"o\">-</span><span class=\"mi\">3</span><span class=\"p\">])</span> <span class=\"o\">+</span> <span class=\"n\">nums</span><span class=\"p\">[</span><span class=\"n\">pos</span><span class=\"p\">]</span>        <span class=\"k\">return</span> <span class=\"nf\">max</span><span class=\"p\">(</span><span class=\"n\">m</span><span class=\"p\">[</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">],</span> <span class=\"n\">m</span><span class=\"p\">[</span><span class=\"o\">-</span><span class=\"mi\">2</span><span class=\"p\">])</span>      <span class=\"k\">if</span> <span class=\"n\">__name__</span> <span class=\"o\">==</span> <span class=\"sh\">'</span><span class=\"s\">__main__</span><span class=\"sh\">'</span><span class=\"p\">:</span>  <span class=\"n\">s</span> <span class=\"o\">=</span> <span class=\"nc\">Solution</span><span class=\"p\">().</span><span class=\"n\">rob</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">([</span><span class=\"mi\">1</span><span class=\"p\">,</span><span class=\"mi\">2</span><span class=\"p\">,</span><span class=\"mi\">1</span><span class=\"p\">,</span><span class=\"mi\">1</span><span class=\"p\">]),</span> <span class=\"mi\">3</span><span class=\"p\">)</span></code></pre></figure><blockquote>  <p>Runtime: 39 ms, faster than 80.95% of Python3 online submissions for House Robber. Memory Usage: 14 MB, less than 19.55% of Python3 online submissions for House Robber.</p></blockquote>",
      "date_published": "2022-11-08T20:00:00+00:00"
    },
    {
      "id": "https://fewald.net/leetcode/2022/11/07/leetcode-palindrome-linked-list.html",
      "url": "https://fewald.net/leetcode/2022/11/07/leetcode-palindrome-linked-list.html",
      "title": "Leetcode: Palindrome linked list",
      "content_html": "<p>Given the <code class=\"language-plaintext highlighter-rouge\">head</code> of a linked list, determine if the linked list is a palindrome and return <code class=\"language-plaintext highlighter-rouge\">True</code> if it is, <code class=\"language-plaintext highlighter-rouge\">False</code> otherwise. For added difficulty, find an algorithm that runs in <code class=\"language-plaintext highlighter-rouge\">O(n)</code> and uses <code class=\"language-plaintext highlighter-rouge\">O(1)</code> space.</p><!--more--><p>The idea to solving this problem can be written as three distinct steps:</p><ol>  <li>Find the middle of the list</li>  <li>Reverse the second half of the list</li>  <li>Iterate over both lists simulatenously, comparing the elements</li></ol><p>Finding the middle can be achieved via slow/fast pointer. Reversing should be done iteratively because each recursion step costs memory. Finally we can set two pointers and compare each element. At the final step we need to check if we arrived at the second half or somewhere else.</p><figure class=\"highlight\"><pre><code class=\"language-python\" data-lang=\"python\"><span class=\"c1\"># Definition for singly-linked list.# class ListNode:#     def __init__(self, val=0, next=None):#         self.val = val#         self.next = next</span><span class=\"k\">class</span> <span class=\"nc\">Solution</span><span class=\"p\">:</span>    <span class=\"k\">def</span> <span class=\"nf\">isPalindrome</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">head</span><span class=\"p\">:</span> <span class=\"n\">Optional</span><span class=\"p\">[</span><span class=\"n\">ListNode</span><span class=\"p\">])</span> <span class=\"o\">-&gt;</span> <span class=\"nb\">bool</span><span class=\"p\">:</span>        <span class=\"n\">p1</span> <span class=\"o\">=</span> <span class=\"n\">head</span>        <span class=\"n\">p2</span> <span class=\"o\">=</span> <span class=\"n\">head</span>        <span class=\"k\">while</span> <span class=\"n\">p2</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"bp\">None</span> <span class=\"ow\">and</span> <span class=\"n\">p2</span><span class=\"p\">.</span><span class=\"nb\">next</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"bp\">None</span><span class=\"p\">:</span>            <span class=\"n\">p1</span> <span class=\"o\">=</span> <span class=\"n\">p1</span><span class=\"p\">.</span><span class=\"nb\">next</span>            <span class=\"n\">p2</span> <span class=\"o\">=</span> <span class=\"n\">p2</span><span class=\"p\">.</span><span class=\"nb\">next</span><span class=\"p\">.</span><span class=\"nb\">next</span>                <span class=\"n\">rev</span> <span class=\"o\">=</span> <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"nf\">reverse</span><span class=\"p\">(</span><span class=\"n\">p1</span><span class=\"p\">)</span>        <span class=\"k\">while</span> <span class=\"n\">head</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"bp\">None</span> <span class=\"ow\">and</span> <span class=\"n\">rev</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"bp\">None</span><span class=\"p\">:</span>            <span class=\"k\">if</span> <span class=\"n\">head</span><span class=\"p\">.</span><span class=\"n\">val</span> <span class=\"o\">!=</span> <span class=\"n\">rev</span><span class=\"p\">.</span><span class=\"n\">val</span><span class=\"p\">:</span>                <span class=\"k\">return</span> <span class=\"bp\">False</span>            <span class=\"n\">rev</span> <span class=\"o\">=</span> <span class=\"n\">rev</span><span class=\"p\">.</span><span class=\"nb\">next</span>            <span class=\"n\">head</span> <span class=\"o\">=</span> <span class=\"n\">head</span><span class=\"p\">.</span><span class=\"nb\">next</span>        <span class=\"k\">if</span> <span class=\"n\">head</span> <span class=\"o\">==</span> <span class=\"n\">p1</span><span class=\"p\">:</span>            <span class=\"n\">head</span> <span class=\"o\">=</span> <span class=\"bp\">None</span>        <span class=\"k\">return</span> <span class=\"n\">head</span> <span class=\"ow\">is</span> <span class=\"bp\">None</span> <span class=\"ow\">and</span> <span class=\"n\">rev</span> <span class=\"ow\">is</span> <span class=\"bp\">None</span>                <span class=\"k\">def</span> <span class=\"nf\">reverse</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">head</span><span class=\"p\">):</span>        <span class=\"n\">prev</span> <span class=\"o\">=</span> <span class=\"bp\">None</span>        <span class=\"n\">curr</span> <span class=\"o\">=</span> <span class=\"n\">head</span>        <span class=\"k\">while</span> <span class=\"n\">curr</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"bp\">None</span><span class=\"p\">:</span>            <span class=\"n\">n</span> <span class=\"o\">=</span> <span class=\"n\">curr</span><span class=\"p\">.</span><span class=\"nb\">next</span>            <span class=\"n\">curr</span><span class=\"p\">.</span><span class=\"nb\">next</span> <span class=\"o\">=</span> <span class=\"n\">prev</span>            <span class=\"n\">prev</span> <span class=\"o\">=</span> <span class=\"n\">curr</span>            <span class=\"n\">curr</span> <span class=\"o\">=</span> <span class=\"n\">n</span>        <span class=\"k\">return</span> <span class=\"n\">prev</span></code></pre></figure><blockquote>  <p>Runtime: 1729 ms, faster than 12.02% of Python3 online submissions for Palindrome Linked List. Memory Usage: 39.1 MB, less than 77.96% of Python3 online submissions for Palindrome Linked List.</p></blockquote>",
      "date_published": "2022-11-07T20:00:00+00:00"
    },
    {
      "id": "https://fewald.net/leetcode/2022/11/06/leetcode-intersection-of-two-arrays-ii.html",
      "url": "https://fewald.net/leetcode/2022/11/06/leetcode-intersection-of-two-arrays-ii.html",
      "title": "Leetcode: Intersection of two arrays II",
      "content_html": "<p>Given two arrays, <code class=\"language-plaintext highlighter-rouge\">nums1</code> and <code class=\"language-plaintext highlighter-rouge\">nums2</code>, return the intersection of those arrays in any order.</p><p>The easiest way I was able to come up with was to sort the arrays first and then look at the beginning of the arrays. If both are equal, add the element to the output and pop the first element from each array. Otherwise, pop the smaller element of both arrays. Repeat this until one of the arrays is empty at which point there can be no more intersecting elements.</p><!--more--><figure class=\"highlight\"><pre><code class=\"language-python\" data-lang=\"python\"><span class=\"k\">class</span> <span class=\"nc\">Solution</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">intersect</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">nums1</span><span class=\"p\">:</span> <span class=\"n\">List</span><span class=\"p\">[</span><span class=\"nb\">int</span><span class=\"p\">],</span> <span class=\"n\">nums2</span><span class=\"p\">:</span> <span class=\"n\">List</span><span class=\"p\">[</span><span class=\"nb\">int</span><span class=\"p\">])</span> <span class=\"o\">-&gt;</span> <span class=\"n\">List</span><span class=\"p\">[</span><span class=\"nb\">int</span><span class=\"p\">]:</span>    <span class=\"n\">nums1</span><span class=\"p\">.</span><span class=\"nf\">sort</span><span class=\"p\">()</span>    <span class=\"n\">nums2</span><span class=\"p\">.</span><span class=\"nf\">sort</span><span class=\"p\">()</span>    <span class=\"n\">out</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>    <span class=\"k\">while</span> <span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">nums1</span><span class=\"p\">)</span> <span class=\"o\">&gt;</span> <span class=\"mi\">0</span> <span class=\"ow\">and</span> <span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">nums2</span><span class=\"p\">)</span> <span class=\"o\">&gt;</span> <span class=\"mi\">0</span><span class=\"p\">:</span>      <span class=\"k\">if</span> <span class=\"n\">nums1</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span> <span class=\"o\">==</span> <span class=\"n\">nums2</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]:</span>        <span class=\"n\">out</span><span class=\"p\">.</span><span class=\"nf\">append</span><span class=\"p\">(</span><span class=\"n\">nums1</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">])</span>        <span class=\"n\">nums1</span> <span class=\"o\">=</span> <span class=\"n\">nums1</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">:]</span>        <span class=\"n\">nums2</span> <span class=\"o\">=</span> <span class=\"n\">nums2</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">:]</span>      <span class=\"k\">elif</span> <span class=\"n\">nums1</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span> <span class=\"o\">&lt;</span> <span class=\"n\">nums2</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]:</span>        <span class=\"n\">nums1</span> <span class=\"o\">=</span> <span class=\"n\">nums1</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">:]</span>      <span class=\"k\">else</span><span class=\"p\">:</span>        <span class=\"n\">nums2</span> <span class=\"o\">=</span> <span class=\"n\">nums2</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">:]</span>    <span class=\"k\">return</span> <span class=\"n\">out</span></code></pre></figure><blockquote>  <p>Runtime: 62 ms, faster than 80.36% of Python3 online submissions for Intersection of Two Arrays II. Memory Usage: 14.2 MB, less than 14.77% of Python3 online submissions for Intersection of Two Arrays II.</p></blockquote>",
      "date_published": "2022-11-06T20:00:00+00:00"
    },
    {
      "id": "https://fewald.net/leetcode/2022/11/05/leetcode-power-of-three.html",
      "url": "https://fewald.net/leetcode/2022/11/05/leetcode-power-of-three.html",
      "title": "Leetcode: Power of three",
      "content_html": "<p>Given a number <code class=\"language-plaintext highlighter-rouge\">n</code>, check if this number is a power of three, that is that it can be represented by <code class=\"language-plaintext highlighter-rouge\">3^x</code>.</p><p>A straightforward solution looks as follows. Multiply by <code class=\"language-plaintext highlighter-rouge\">3</code> until we reach <code class=\"language-plaintext highlighter-rouge\">n</code>. If we’re greater than <code class=\"language-plaintext highlighter-rouge\">n</code>, return <code class=\"language-plaintext highlighter-rouge\">False</code>, and <code class=\"language-plaintext highlighter-rouge\">True</code> otherwise.</p><figure class=\"highlight\"><pre><code class=\"language-python\" data-lang=\"python\"><span class=\"k\">class</span> <span class=\"nc\">Solution</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">isPowerOfThree</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">n</span><span class=\"p\">:</span> <span class=\"nb\">int</span><span class=\"p\">)</span> <span class=\"o\">-&gt;</span> <span class=\"nb\">bool</span><span class=\"p\">:</span>    <span class=\"k\">if</span> <span class=\"n\">n</span> <span class=\"o\">&lt;=</span> <span class=\"mi\">0</span><span class=\"p\">:</span>        <span class=\"k\">return</span> <span class=\"bp\">False</span>        <span class=\"n\">x</span> <span class=\"o\">=</span> <span class=\"mi\">1</span>    <span class=\"k\">while</span> <span class=\"n\">x</span> <span class=\"o\">&lt;</span> <span class=\"n\">n</span><span class=\"p\">:</span>        <span class=\"n\">x</span> <span class=\"o\">*=</span> <span class=\"mi\">3</span>    <span class=\"k\">return</span> <span class=\"n\">x</span> <span class=\"o\">==</span> <span class=\"n\">n</span></code></pre></figure><!--more--><p>A mathematical more elegant solution looks as follows. Create the largest number that is less than <code class=\"language-plaintext highlighter-rouge\">2^31</code>: <code class=\"language-plaintext highlighter-rouge\">3^19</code>. Then use the modulo operator to test if the number is divisible without a rest. If this is the case, <code class=\"language-plaintext highlighter-rouge\">x*n == 3^19</code>, therefore <code class=\"language-plaintext highlighter-rouge\">n</code> is a valid number.</p><figure class=\"highlight\"><pre><code class=\"language-python\" data-lang=\"python\"><span class=\"k\">class</span> <span class=\"nc\">Solution</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">isPowerOfThree</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">n</span><span class=\"p\">:</span> <span class=\"nb\">int</span><span class=\"p\">)</span> <span class=\"o\">-&gt;</span> <span class=\"nb\">bool</span><span class=\"p\">:</span>    <span class=\"k\">return</span> <span class=\"n\">n</span> <span class=\"o\">&gt;</span> <span class=\"mi\">0</span> <span class=\"ow\">and</span> <span class=\"nf\">pow</span><span class=\"p\">(</span><span class=\"mi\">3</span><span class=\"p\">,</span><span class=\"mi\">19</span><span class=\"p\">)</span> <span class=\"o\">%</span> <span class=\"n\">n</span> <span class=\"o\">==</span> <span class=\"mi\">0</span></code></pre></figure><blockquote>  <p>Runtime: 80 ms, faster than 95.46% of Python3 online submissions for Power of Three. Memory Usage: 13.9 MB, less than 16.48% of Python3 online submissions for Power of Three.</p></blockquote>",
      "date_published": "2022-11-05T20:00:00+00:00"
    },
    {
      "id": "https://fewald.net/leetcode/2022/11/04/leetcode-kth-smallest-element-in-matrix.html",
      "url": "https://fewald.net/leetcode/2022/11/04/leetcode-kth-smallest-element-in-matrix.html",
      "title": "Leetcode: Kth smallest element in matrix",
      "content_html": "<p>Given a <code class=\"language-plaintext highlighter-rouge\">n*n</code> matrix, find the <code class=\"language-plaintext highlighter-rouge\">kth</code> smallest element. Each row in the matrix is sorted in ascending order and independent of the other rows. Do not use <code class=\"language-plaintext highlighter-rouge\">O(n*n)</code> space when solving this. One example matrix looks like this:</p><figure class=\"highlight\"><pre><code class=\"language-plain\" data-lang=\"plain\">  1  5  9 10 11 13 12 13 15For k = 8 return 13 because 13 is the 8th smallest element.</code></pre></figure><p>If we could use all the space we wanted, we could just perform an n-way merge sort on every row at once. This would work because the rows itself are sorted already. Afterwards, we would need to pick the kth element from the list and would be done.</p><!--more--><p>The solution I came up with uses <code class=\"language-plaintext highlighter-rouge\">O(n)</code> space. That is one space for a pointer to each row and column. Initially we set all rows to the first column. Then we iterate over all rows and find the smallest element in this particular position. We advance the pointer to this element by 1. Afterwards we check if we left the bounds of this array. If we did, we remove the row as a possible candidate. Finally, we take the remaining pointers and write them in a list which we sort. From that sorted list, we pick the smallest value and return it.</p><figure class=\"highlight\"><pre><code class=\"language-python\" data-lang=\"python\"><span class=\"k\">class</span> <span class=\"nc\">Solution</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">kthSmallest</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">matrix</span><span class=\"p\">:</span> <span class=\"n\">List</span><span class=\"p\">[</span><span class=\"n\">List</span><span class=\"p\">[</span><span class=\"nb\">int</span><span class=\"p\">]],</span> <span class=\"n\">k</span><span class=\"p\">:</span> <span class=\"nb\">int</span><span class=\"p\">)</span> <span class=\"o\">-&gt;</span> <span class=\"nb\">int</span><span class=\"p\">:</span>    <span class=\"c1\"># Initialize pointer</span>    <span class=\"n\">ptr</span> <span class=\"o\">=</span> <span class=\"p\">[[</span><span class=\"n\">i</span><span class=\"p\">,</span><span class=\"mi\">0</span><span class=\"p\">]</span> <span class=\"k\">for</span> <span class=\"n\">i</span> <span class=\"ow\">in</span> <span class=\"nf\">range</span><span class=\"p\">(</span><span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">matrix</span><span class=\"p\">))]</span>    <span class=\"n\">min_val</span> <span class=\"o\">=</span> <span class=\"nf\">float</span><span class=\"p\">(</span><span class=\"sh\">'</span><span class=\"s\">inf</span><span class=\"sh\">'</span><span class=\"p\">)</span>    <span class=\"n\">min_row</span> <span class=\"o\">=</span> <span class=\"o\">-</span><span class=\"mi\">1</span>    <span class=\"k\">while</span> <span class=\"n\">k</span> <span class=\"o\">&gt;</span> <span class=\"mi\">1</span><span class=\"p\">:</span>      <span class=\"n\">min_val</span> <span class=\"o\">=</span> <span class=\"nf\">float</span><span class=\"p\">(</span><span class=\"sh\">'</span><span class=\"s\">inf</span><span class=\"sh\">'</span><span class=\"p\">)</span>      <span class=\"n\">min_row</span> <span class=\"o\">=</span> <span class=\"o\">-</span><span class=\"mi\">1</span>      <span class=\"c1\"># Find minimum row</span>      <span class=\"k\">for</span> <span class=\"n\">i</span><span class=\"p\">,</span> <span class=\"n\">coords</span> <span class=\"ow\">in</span> <span class=\"nf\">enumerate</span><span class=\"p\">(</span><span class=\"n\">ptr</span><span class=\"p\">):</span>        <span class=\"k\">if</span> <span class=\"n\">matrix</span><span class=\"p\">[</span><span class=\"n\">coords</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]][</span><span class=\"n\">coords</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">]]</span> <span class=\"o\">&lt;</span> <span class=\"n\">min_val</span><span class=\"p\">:</span>          <span class=\"n\">min_row</span> <span class=\"o\">=</span> <span class=\"n\">i</span>          <span class=\"n\">min_val</span> <span class=\"o\">=</span> <span class=\"n\">matrix</span><span class=\"p\">[</span><span class=\"n\">coords</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]][</span><span class=\"n\">coords</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">]]</span>            <span class=\"c1\"># Advance minimum row by 1</span>      <span class=\"n\">ptr</span><span class=\"p\">[</span><span class=\"n\">min_row</span><span class=\"p\">][</span><span class=\"mi\">1</span><span class=\"p\">]</span> <span class=\"o\">+=</span> <span class=\"mi\">1</span>      <span class=\"k\">if</span> <span class=\"n\">ptr</span><span class=\"p\">[</span><span class=\"n\">min_row</span><span class=\"p\">][</span><span class=\"mi\">1</span><span class=\"p\">]</span> <span class=\"o\">&gt;=</span> <span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">matrix</span><span class=\"p\">):</span>        <span class=\"k\">del</span> <span class=\"n\">ptr</span><span class=\"p\">[</span><span class=\"n\">min_row</span><span class=\"p\">]</span>      <span class=\"n\">k</span> <span class=\"o\">-=</span> <span class=\"mi\">1</span>        <span class=\"n\">vals</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">matrix</span><span class=\"p\">[</span><span class=\"n\">row</span><span class=\"p\">][</span><span class=\"n\">col</span><span class=\"p\">]</span> <span class=\"k\">for</span> <span class=\"n\">row</span><span class=\"p\">,</span> <span class=\"n\">col</span> <span class=\"ow\">in</span> <span class=\"n\">ptr</span><span class=\"p\">]</span>    <span class=\"n\">vals</span><span class=\"p\">.</span><span class=\"nf\">sort</span><span class=\"p\">()</span>    <span class=\"k\">return</span> <span class=\"n\">vals</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span><span class=\"k\">if</span> <span class=\"n\">__name__</span> <span class=\"o\">==</span> <span class=\"sh\">'</span><span class=\"s\">__main__</span><span class=\"sh\">'</span><span class=\"p\">:</span>  <span class=\"n\">s</span> <span class=\"o\">=</span> <span class=\"nc\">Solution</span><span class=\"p\">().</span><span class=\"n\">kthSmallest</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">([[</span><span class=\"mi\">1</span><span class=\"p\">,</span><span class=\"mi\">5</span><span class=\"p\">,</span><span class=\"mi\">9</span><span class=\"p\">],[</span><span class=\"mi\">10</span><span class=\"p\">,</span><span class=\"mi\">11</span><span class=\"p\">,</span><span class=\"mi\">13</span><span class=\"p\">],[</span><span class=\"mi\">12</span><span class=\"p\">,</span><span class=\"mi\">13</span><span class=\"p\">,</span><span class=\"mi\">15</span><span class=\"p\">]],</span> <span class=\"mi\">8</span><span class=\"p\">),</span> <span class=\"mi\">13</span><span class=\"p\">)</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">([[</span><span class=\"mi\">1</span><span class=\"p\">,</span><span class=\"mi\">5</span><span class=\"p\">,</span><span class=\"mi\">9</span><span class=\"p\">],[</span><span class=\"mi\">10</span><span class=\"p\">,</span><span class=\"mi\">11</span><span class=\"p\">,</span><span class=\"mi\">13</span><span class=\"p\">],[</span><span class=\"mi\">12</span><span class=\"p\">,</span><span class=\"mi\">13</span><span class=\"p\">,</span><span class=\"mi\">15</span><span class=\"p\">]],</span> <span class=\"mi\">1</span><span class=\"p\">),</span> <span class=\"mi\">1</span><span class=\"p\">)</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">([[</span><span class=\"o\">-</span><span class=\"mi\">5</span><span class=\"p\">]],</span> <span class=\"mi\">1</span><span class=\"p\">),</span> <span class=\"o\">-</span><span class=\"mi\">5</span><span class=\"p\">)</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">([[</span><span class=\"mi\">1</span><span class=\"p\">,</span><span class=\"mi\">2</span><span class=\"p\">],</span> <span class=\"p\">[</span><span class=\"mi\">3</span><span class=\"p\">,</span><span class=\"mi\">3</span><span class=\"p\">]],</span> <span class=\"mi\">2</span><span class=\"p\">),</span> <span class=\"mi\">2</span><span class=\"p\">)</span></code></pre></figure><blockquote>  <p>Runtime: 4712 ms, faster than 5.02% of Python3 online submissions for Kth Smallest Element in a Sorted Matrix. Memory Usage: 18.6 MB, less than 80.92% of Python3 online submissions for Kth Smallest Element in a Sorted Matrix.</p></blockquote>",
      "date_published": "2022-11-04T20:00:00+00:00"
    },
    {
      "id": "https://fewald.net/leetcode/2022/11/03/leetcode-copy-list-with-random-pointer.html",
      "url": "https://fewald.net/leetcode/2022/11/03/leetcode-copy-list-with-random-pointer.html",
      "title": "Leetcode: Copy list with random pointer",
      "content_html": "<p>Given a <code class=\"language-plaintext highlighter-rouge\">head</code> of a list with pointers to <code class=\"language-plaintext highlighter-rouge\">next</code> and a <code class=\"language-plaintext highlighter-rouge\">random</code> element, copy the list to a new list without any random pointers pointing to the old list.</p><figure class=\"highlight\"><pre><code class=\"language-python\" data-lang=\"python\"><span class=\"c1\"># Definition for a Node.</span><span class=\"k\">class</span> <span class=\"nc\">Node</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">__init__</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">x</span><span class=\"p\">:</span> <span class=\"nb\">int</span><span class=\"p\">,</span> <span class=\"nb\">next</span><span class=\"p\">:</span> <span class=\"sh\">'</span><span class=\"s\">Node</span><span class=\"sh\">'</span> <span class=\"o\">=</span> <span class=\"bp\">None</span><span class=\"p\">,</span> <span class=\"n\">random</span><span class=\"p\">:</span> <span class=\"sh\">'</span><span class=\"s\">Node</span><span class=\"sh\">'</span> <span class=\"o\">=</span> <span class=\"bp\">None</span><span class=\"p\">):</span>    <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">val</span> <span class=\"o\">=</span> <span class=\"nf\">int</span><span class=\"p\">(</span><span class=\"n\">x</span><span class=\"p\">)</span>    <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"nb\">next</span> <span class=\"o\">=</span> <span class=\"nb\">next</span>    <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">random</span> <span class=\"o\">=</span> <span class=\"n\">random</span></code></pre></figure><!--more--><p>One way to solve this is to copy first all the nodes into a new list via the <code class=\"language-plaintext highlighter-rouge\">next</code> links. Next, we create two pointers, one pointing at the original list and the other one pointing at the new list. We then iterate over the original list (while moving the second pointer as well) and check at each step if we hit our current node in the original list via <code class=\"language-plaintext highlighter-rouge\">random</code>. If we do, we make a connection in the new list as well. One specialty is that two <code class=\"language-plaintext highlighter-rouge\">random</code> pointers can point at the same element. For this reason, we cannot simply stop once we found the first pointer but have to continue until the end.</p><figure class=\"highlight\"><pre><code class=\"language-python\" data-lang=\"python\"><span class=\"kn\">from</span> <span class=\"n\">typing</span> <span class=\"kn\">import</span> <span class=\"n\">Optional</span><span class=\"c1\"># Definition for a Node.</span><span class=\"k\">class</span> <span class=\"nc\">Node</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">__init__</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">x</span><span class=\"p\">:</span> <span class=\"nb\">int</span><span class=\"p\">,</span> <span class=\"nb\">next</span><span class=\"p\">:</span> <span class=\"sh\">'</span><span class=\"s\">Node</span><span class=\"sh\">'</span> <span class=\"o\">=</span> <span class=\"bp\">None</span><span class=\"p\">,</span> <span class=\"n\">random</span><span class=\"p\">:</span> <span class=\"sh\">'</span><span class=\"s\">Node</span><span class=\"sh\">'</span> <span class=\"o\">=</span> <span class=\"bp\">None</span><span class=\"p\">):</span>    <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">val</span> <span class=\"o\">=</span> <span class=\"nf\">int</span><span class=\"p\">(</span><span class=\"n\">x</span><span class=\"p\">)</span>    <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"nb\">next</span> <span class=\"o\">=</span> <span class=\"nb\">next</span>    <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">random</span> <span class=\"o\">=</span> <span class=\"n\">random</span>  <span class=\"k\">def</span> <span class=\"nf\">__repr__</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">)</span> <span class=\"o\">-&gt;</span> <span class=\"nb\">str</span><span class=\"p\">:</span>    <span class=\"sh\">\"\"\"</span><span class=\"s\">Convenience method for debugging</span><span class=\"sh\">\"\"\"</span>    <span class=\"k\">return</span> <span class=\"nf\">str</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">val</span><span class=\"p\">)</span><span class=\"k\">class</span> <span class=\"nc\">Solution</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">copyRandomList</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">head</span><span class=\"p\">:</span> <span class=\"sh\">'</span><span class=\"s\">Optional[Node]</span><span class=\"sh\">'</span><span class=\"p\">)</span> <span class=\"o\">-&gt;</span> <span class=\"sh\">'</span><span class=\"s\">Optional[Node]</span><span class=\"sh\">'</span><span class=\"p\">:</span>    <span class=\"k\">if</span> <span class=\"n\">head</span> <span class=\"ow\">is</span> <span class=\"bp\">None</span><span class=\"p\">:</span>      <span class=\"k\">return</span> <span class=\"bp\">None</span>    <span class=\"n\">p1</span> <span class=\"o\">=</span> <span class=\"n\">head</span>    <span class=\"c1\"># Copy first node manually</span>    <span class=\"n\">new_head</span> <span class=\"o\">=</span> <span class=\"nc\">Node</span><span class=\"p\">(</span><span class=\"n\">head</span><span class=\"p\">.</span><span class=\"n\">val</span><span class=\"p\">)</span>    <span class=\"n\">p2</span> <span class=\"o\">=</span> <span class=\"n\">new_head</span>        <span class=\"c1\"># Create single linked list</span>    <span class=\"k\">while</span> <span class=\"n\">p1</span><span class=\"p\">.</span><span class=\"nb\">next</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"bp\">None</span><span class=\"p\">:</span>      <span class=\"n\">p2</span><span class=\"p\">.</span><span class=\"nb\">next</span> <span class=\"o\">=</span> <span class=\"nc\">Node</span><span class=\"p\">(</span><span class=\"n\">p1</span><span class=\"p\">.</span><span class=\"nb\">next</span><span class=\"p\">.</span><span class=\"n\">val</span><span class=\"p\">)</span>      <span class=\"n\">p2</span> <span class=\"o\">=</span> <span class=\"n\">p2</span><span class=\"p\">.</span><span class=\"nb\">next</span>      <span class=\"n\">p1</span> <span class=\"o\">=</span> <span class=\"n\">p1</span><span class=\"p\">.</span><span class=\"nb\">next</span>        <span class=\"c1\"># Reset pointers</span>    <span class=\"n\">t1</span> <span class=\"o\">=</span> <span class=\"n\">head</span>    <span class=\"n\">t2</span> <span class=\"o\">=</span> <span class=\"n\">new_head</span>    <span class=\"k\">while</span> <span class=\"n\">t1</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"bp\">None</span><span class=\"p\">:</span>      <span class=\"n\">p1</span> <span class=\"o\">=</span> <span class=\"n\">head</span>      <span class=\"n\">p2</span> <span class=\"o\">=</span> <span class=\"n\">new_head</span>            <span class=\"k\">while</span> <span class=\"n\">p1</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"bp\">None</span><span class=\"p\">:</span>        <span class=\"k\">if</span> <span class=\"n\">p1</span><span class=\"p\">.</span><span class=\"n\">random</span> <span class=\"o\">==</span> <span class=\"n\">t1</span><span class=\"p\">:</span>          <span class=\"n\">p2</span><span class=\"p\">.</span><span class=\"n\">random</span> <span class=\"o\">=</span> <span class=\"n\">t2</span>        <span class=\"n\">p1</span> <span class=\"o\">=</span> <span class=\"n\">p1</span><span class=\"p\">.</span><span class=\"nb\">next</span>        <span class=\"n\">p2</span> <span class=\"o\">=</span> <span class=\"n\">p2</span><span class=\"p\">.</span><span class=\"nb\">next</span>              <span class=\"n\">t1</span> <span class=\"o\">=</span> <span class=\"n\">t1</span><span class=\"p\">.</span><span class=\"nb\">next</span>      <span class=\"n\">t2</span> <span class=\"o\">=</span> <span class=\"n\">t2</span><span class=\"p\">.</span><span class=\"nb\">next</span>    <span class=\"k\">return</span> <span class=\"n\">new_head</span><span class=\"k\">if</span> <span class=\"n\">__name__</span> <span class=\"o\">==</span> <span class=\"sh\">'</span><span class=\"s\">__main__</span><span class=\"sh\">'</span><span class=\"p\">:</span>  <span class=\"n\">s</span> <span class=\"o\">=</span> <span class=\"nc\">Solution</span><span class=\"p\">().</span><span class=\"n\">copyRandomList</span>  <span class=\"n\">n</span> <span class=\"o\">=</span> <span class=\"nc\">Node</span><span class=\"p\">(</span><span class=\"mi\">1</span><span class=\"p\">,</span> <span class=\"nc\">Node</span><span class=\"p\">(</span><span class=\"mi\">2</span><span class=\"p\">,</span> <span class=\"nc\">Node</span><span class=\"p\">(</span><span class=\"mi\">3</span><span class=\"p\">,</span> <span class=\"nc\">Node</span><span class=\"p\">(</span><span class=\"mi\">4</span><span class=\"p\">))))</span>  <span class=\"n\">n</span><span class=\"p\">.</span><span class=\"n\">random</span> <span class=\"o\">=</span> <span class=\"n\">n</span><span class=\"p\">.</span><span class=\"nb\">next</span><span class=\"p\">.</span><span class=\"nb\">next</span>  <span class=\"n\">n</span><span class=\"p\">.</span><span class=\"n\">random</span><span class=\"p\">.</span><span class=\"n\">random</span> <span class=\"o\">=</span> <span class=\"n\">n</span><span class=\"p\">.</span><span class=\"nb\">next</span><span class=\"p\">.</span><span class=\"nb\">next</span><span class=\"p\">.</span><span class=\"nb\">next</span>  <span class=\"n\">n</span><span class=\"p\">.</span><span class=\"n\">random</span><span class=\"p\">.</span><span class=\"n\">random</span><span class=\"p\">.</span><span class=\"n\">random</span> <span class=\"o\">=</span> <span class=\"n\">n</span><span class=\"p\">.</span><span class=\"nb\">next</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">(</span><span class=\"n\">n</span><span class=\"p\">))</span>  <span class=\"n\">n</span> <span class=\"o\">=</span> <span class=\"nc\">Node</span><span class=\"p\">(</span><span class=\"mi\">7</span><span class=\"p\">,</span> <span class=\"nc\">Node</span><span class=\"p\">(</span><span class=\"mi\">13</span><span class=\"p\">,</span> <span class=\"nc\">Node</span><span class=\"p\">(</span><span class=\"mi\">11</span><span class=\"p\">,</span> <span class=\"nc\">Node</span><span class=\"p\">(</span><span class=\"mi\">10</span><span class=\"p\">,</span> <span class=\"nc\">Node</span><span class=\"p\">(</span><span class=\"mi\">1</span><span class=\"p\">)))))</span>  <span class=\"n\">n</span><span class=\"p\">.</span><span class=\"nb\">next</span><span class=\"p\">.</span><span class=\"n\">random</span> <span class=\"o\">=</span> <span class=\"n\">n</span>  <span class=\"n\">n</span><span class=\"p\">.</span><span class=\"nb\">next</span><span class=\"p\">.</span><span class=\"nb\">next</span><span class=\"p\">.</span><span class=\"n\">random</span> <span class=\"o\">=</span> <span class=\"n\">n</span><span class=\"p\">.</span><span class=\"nb\">next</span><span class=\"p\">.</span><span class=\"nb\">next</span><span class=\"p\">.</span><span class=\"nb\">next</span><span class=\"p\">.</span><span class=\"nb\">next</span>  <span class=\"n\">n</span><span class=\"p\">.</span><span class=\"nb\">next</span><span class=\"p\">.</span><span class=\"nb\">next</span><span class=\"p\">.</span><span class=\"nb\">next</span><span class=\"p\">.</span><span class=\"n\">random</span> <span class=\"o\">=</span> <span class=\"n\">n</span><span class=\"p\">.</span><span class=\"nb\">next</span><span class=\"p\">.</span><span class=\"nb\">next</span>  <span class=\"n\">n</span><span class=\"p\">.</span><span class=\"nb\">next</span><span class=\"p\">.</span><span class=\"nb\">next</span><span class=\"p\">.</span><span class=\"nb\">next</span><span class=\"p\">.</span><span class=\"nb\">next</span><span class=\"p\">.</span><span class=\"n\">random</span> <span class=\"o\">=</span> <span class=\"n\">n</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">(</span><span class=\"n\">n</span><span class=\"p\">))</span>  </code></pre></figure><blockquote>  <p>Runtime: 106 ms, faster than 5.23% of Python3 online submissions for Copy List with Random Pointer. Memory Usage: 15 MB, less than 46.50% of Python3 online submissions for Copy List with Random Pointer.</p></blockquote>",
      "date_published": "2022-11-03T20:00:00+00:00"
    },
    {
      "id": "https://fewald.net/leetcode/2022/11/02/leetcode-decode-ways.html",
      "url": "https://fewald.net/leetcode/2022/11/02/leetcode-decode-ways.html",
      "title": "Leetcode: Decode ways",
      "content_html": "<p>Characters can be encoded via different numbers. <code class=\"language-plaintext highlighter-rouge\">A -&gt; 1, B -&gt; 2, ..., Z -&gt; 26</code>. Given a string <code class=\"language-plaintext highlighter-rouge\">s</code> of numbers, return the number of possible decodings. For example <code class=\"language-plaintext highlighter-rouge\">12</code> can be decoded as <code class=\"language-plaintext highlighter-rouge\">A,B</code> and as <code class=\"language-plaintext highlighter-rouge\">L</code>.</p><!--more--><p>We can look at this problem as a recursive problem. By checking the first two characters of each string, we can see if we decode it in zero, one or two ways. It is impossible to decode a string if it starts in <code class=\"language-plaintext highlighter-rouge\">0</code>, according to the rules. In this case we can return immediately and do not need to proceed further.</p><p>Next, we test if the string is at least <code class=\"language-plaintext highlighter-rouge\">2</code> long. If this is the case we can either decode it as a single character or as two characters. The exception for two characters is that the number has to be below or equal to <code class=\"language-plaintext highlighter-rouge\">26</code>.</p><p>Lastly, it is a good idea to add memoization to the algorithm and avoid performing the same operation twice. For example if we have a string of length 4: <code class=\"language-plaintext highlighter-rouge\">1-1-1-1</code>, we can decode it as <code class=\"language-plaintext highlighter-rouge\">11-...</code> or as <code class=\"language-plaintext highlighter-rouge\">1-1...</code>. However, the number of ways the remainder can be decoded stays the same.</p><figure class=\"highlight\"><pre><code class=\"language-python\" data-lang=\"python\"><span class=\"k\">class</span> <span class=\"nc\">Solution</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">numDecodings</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">s</span><span class=\"p\">:</span> <span class=\"nb\">str</span><span class=\"p\">)</span> <span class=\"o\">-&gt;</span> <span class=\"nb\">int</span><span class=\"p\">:</span>    <span class=\"n\">mem</span> <span class=\"o\">=</span> <span class=\"p\">{}</span>    <span class=\"k\">def</span> <span class=\"nf\">inner</span><span class=\"p\">(</span><span class=\"n\">s</span><span class=\"p\">):</span>      <span class=\"k\">if</span> <span class=\"n\">s</span> <span class=\"ow\">in</span> <span class=\"n\">mem</span><span class=\"p\">:</span>        <span class=\"k\">return</span> <span class=\"n\">mem</span><span class=\"p\">[</span><span class=\"n\">s</span><span class=\"p\">]</span>      <span class=\"k\">if</span> <span class=\"n\">s</span> <span class=\"o\">==</span> <span class=\"sh\">\"\"</span><span class=\"p\">:</span>        <span class=\"k\">return</span> <span class=\"mi\">1</span>            <span class=\"k\">if</span> <span class=\"n\">s</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span> <span class=\"o\">==</span> <span class=\"sh\">'</span><span class=\"s\">0</span><span class=\"sh\">'</span><span class=\"p\">:</span>        <span class=\"k\">return</span> <span class=\"mi\">0</span>            <span class=\"k\">if</span> <span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">s</span><span class=\"p\">)</span> <span class=\"o\">&gt;=</span> <span class=\"mi\">2</span> <span class=\"ow\">and</span> <span class=\"n\">s</span><span class=\"p\">[:</span><span class=\"mi\">2</span><span class=\"p\">]</span> <span class=\"o\">&lt;=</span> <span class=\"sh\">'</span><span class=\"s\">26</span><span class=\"sh\">'</span><span class=\"p\">:</span>        <span class=\"n\">mem</span><span class=\"p\">[</span><span class=\"n\">s</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"nf\">inner</span><span class=\"p\">(</span><span class=\"n\">s</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">:])</span> <span class=\"o\">+</span> <span class=\"nf\">inner</span><span class=\"p\">(</span><span class=\"n\">s</span><span class=\"p\">[</span><span class=\"mi\">2</span><span class=\"p\">:])</span>        <span class=\"k\">return</span> <span class=\"n\">mem</span><span class=\"p\">[</span><span class=\"n\">s</span><span class=\"p\">]</span>      <span class=\"k\">else</span><span class=\"p\">:</span>        <span class=\"n\">mem</span><span class=\"p\">[</span><span class=\"n\">s</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"nf\">inner</span><span class=\"p\">(</span><span class=\"n\">s</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">:])</span>        <span class=\"k\">return</span> <span class=\"n\">mem</span><span class=\"p\">[</span><span class=\"n\">s</span><span class=\"p\">]</span>    <span class=\"k\">return</span> <span class=\"nf\">inner</span><span class=\"p\">(</span><span class=\"n\">s</span><span class=\"p\">)</span><span class=\"k\">if</span> <span class=\"n\">__name__</span> <span class=\"o\">==</span> <span class=\"sh\">'</span><span class=\"s\">__main__</span><span class=\"sh\">'</span><span class=\"p\">:</span>  <span class=\"n\">s</span> <span class=\"o\">=</span> <span class=\"nc\">Solution</span><span class=\"p\">().</span><span class=\"n\">numDecodings</span>    <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">(</span><span class=\"sh\">'</span><span class=\"s\">11106</span><span class=\"sh\">'</span><span class=\"p\">),</span> <span class=\"mi\">2</span><span class=\"p\">)</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">(</span><span class=\"sh\">'</span><span class=\"s\">06</span><span class=\"sh\">'</span><span class=\"p\">),</span> <span class=\"mi\">0</span><span class=\"p\">)</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">(</span><span class=\"sh\">'</span><span class=\"s\">12</span><span class=\"sh\">'</span><span class=\"p\">),</span> <span class=\"mi\">2</span><span class=\"p\">)</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">(</span><span class=\"sh\">'</span><span class=\"s\">226</span><span class=\"sh\">'</span><span class=\"p\">),</span> <span class=\"mi\">3</span><span class=\"p\">)</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">(</span><span class=\"sh\">\"</span><span class=\"s\">2611055971756562</span><span class=\"sh\">\"</span><span class=\"p\">),</span> <span class=\"mi\">4</span><span class=\"p\">)</span></code></pre></figure><blockquote>  <p>Runtime: 74 ms, faster than 8.01% of Python3 online submissions for Decode Ways. Memory Usage: 14.3 MB, less than 6.92% of Python3 online submissions for Decode Ways.</p></blockquote>",
      "date_published": "2022-11-02T20:00:00+00:00"
    },
    {
      "id": "https://fewald.net/leetcode/2022/11/01/leetcode-reconstruct-binary-tree.html",
      "url": "https://fewald.net/leetcode/2022/11/01/leetcode-reconstruct-binary-tree.html",
      "title": "Leetcode: Reconstruct binary tree",
      "content_html": "<p>Given two lists of a <code class=\"language-plaintext highlighter-rouge\">preorder</code> and <code class=\"language-plaintext highlighter-rouge\">inorder</code>, create a tree and return the <code class=\"language-plaintext highlighter-rouge\">root</code> node.</p><figure class=\"highlight\"><pre><code class=\"language-plain\" data-lang=\"plain\">preorder: [3,9,20,15,7]inorder:  [9,3,15,20,7]Resulting tree:      3    /   \\   9    20       /  \\      15   7</code></pre></figure><p>The problem is not so obvious at first. If the preorder list would also contain <code class=\"language-plaintext highlighter-rouge\">null</code> values for the places where the tree does not contain nodes, we could simply iterate over this list and reconstruct the tree from there. But since the <code class=\"language-plaintext highlighter-rouge\">null</code> values are missing, we also need to take a look at the <code class=\"language-plaintext highlighter-rouge\">inorder</code> list.</p><!--more--><p>We know about the <code class=\"language-plaintext highlighter-rouge\">preorder</code> list that the traversal is as follows: <code class=\"language-plaintext highlighter-rouge\">NLR</code>. This means that first the node is visited, then we go recursively to the left, and then recursively to the right. For <code class=\"language-plaintext highlighter-rouge\">inorder</code> it is <code class=\"language-plaintext highlighter-rouge\">LNR</code>: We go recursively to the left, then output the node, and finally go to the right.</p><p>Because of those properties, we know that the <code class=\"language-plaintext highlighter-rouge\">root</code> node is the first one in the <code class=\"language-plaintext highlighter-rouge\">preorder</code> list. Since there is only one <code class=\"language-plaintext highlighter-rouge\">root</code> node, we know that the next node will be in the next level. We don’t know, from looking at the <code class=\"language-plaintext highlighter-rouge\">preorder</code> list, if the next child node is to the left or right. To figure this out, we need to look at the <code class=\"language-plaintext highlighter-rouge\">inorder</code> list. If the node is to the left of our current element, know that it must be the immediate child to the left. We then determine the position of the node in the <code class=\"language-plaintext highlighter-rouge\">inorder</code> list and pass it recursively to the same method.</p><p>If it is to the right, we remove the left prefix from the <code class=\"language-plaintext highlighter-rouge\">preorder</code> list, add it as a right node, and pass in the left and right lists to the recursive function. Finally, we return the <code class=\"language-plaintext highlighter-rouge\">root</code> node.</p><figure class=\"highlight\"><pre><code class=\"language-python\" data-lang=\"python\"><span class=\"kn\">from</span> <span class=\"n\">typing</span> <span class=\"kn\">import</span> <span class=\"n\">List</span><span class=\"p\">,</span> <span class=\"n\">Optional</span><span class=\"c1\"># Definition for a binary tree node.</span><span class=\"k\">class</span> <span class=\"nc\">TreeNode</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">__init__</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">val</span><span class=\"o\">=</span><span class=\"mi\">0</span><span class=\"p\">,</span> <span class=\"n\">left</span><span class=\"o\">=</span><span class=\"bp\">None</span><span class=\"p\">,</span> <span class=\"n\">right</span><span class=\"o\">=</span><span class=\"bp\">None</span><span class=\"p\">):</span>    <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">val</span> <span class=\"o\">=</span> <span class=\"n\">val</span>    <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">left</span> <span class=\"o\">=</span> <span class=\"n\">left</span>    <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">right</span> <span class=\"o\">=</span> <span class=\"n\">right</span>    <span class=\"k\">def</span> <span class=\"nf\">__repr__</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">)</span> <span class=\"o\">-&gt;</span> <span class=\"nb\">str</span><span class=\"p\">:</span>    <span class=\"sh\">\"\"\"</span><span class=\"s\">Convenience function for easy debugging</span><span class=\"sh\">\"\"\"</span>    <span class=\"k\">return</span> <span class=\"nf\">str</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">val</span><span class=\"p\">)</span><span class=\"k\">class</span> <span class=\"nc\">Solution</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">buildTree</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">preorder</span><span class=\"p\">:</span> <span class=\"n\">List</span><span class=\"p\">[</span><span class=\"nb\">int</span><span class=\"p\">],</span> <span class=\"n\">inorder</span><span class=\"p\">:</span> <span class=\"n\">List</span><span class=\"p\">[</span><span class=\"nb\">int</span><span class=\"p\">])</span> <span class=\"o\">-&gt;</span> <span class=\"n\">Optional</span><span class=\"p\">[</span><span class=\"n\">TreeNode</span><span class=\"p\">]:</span>    <span class=\"k\">if</span> <span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">preorder</span><span class=\"p\">)</span> <span class=\"o\">==</span> <span class=\"mi\">0</span><span class=\"p\">:</span>      <span class=\"k\">return</span> <span class=\"bp\">None</span>        <span class=\"k\">def</span> <span class=\"nf\">inner</span><span class=\"p\">(</span><span class=\"n\">root</span><span class=\"p\">,</span> <span class=\"n\">preorder</span><span class=\"p\">,</span> <span class=\"n\">left</span><span class=\"p\">,</span> <span class=\"n\">right</span><span class=\"p\">):</span>      <span class=\"k\">if</span> <span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">preorder</span><span class=\"p\">)</span> <span class=\"o\">==</span> <span class=\"mi\">0</span><span class=\"p\">:</span>        <span class=\"k\">return</span>      <span class=\"c1\"># Determine if the element is left or right</span>      <span class=\"n\">curr</span> <span class=\"o\">=</span> <span class=\"n\">preorder</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span>      <span class=\"n\">idx</span> <span class=\"o\">=</span> <span class=\"o\">-</span><span class=\"mi\">1</span>      <span class=\"k\">for</span> <span class=\"n\">i</span> <span class=\"ow\">in</span> <span class=\"nf\">range</span><span class=\"p\">(</span><span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">left</span><span class=\"p\">)):</span>        <span class=\"k\">if</span> <span class=\"n\">left</span><span class=\"p\">[</span><span class=\"n\">i</span><span class=\"p\">]</span> <span class=\"o\">==</span> <span class=\"n\">curr</span><span class=\"p\">:</span>          <span class=\"n\">idx</span> <span class=\"o\">=</span> <span class=\"n\">i</span>          <span class=\"k\">break</span>      <span class=\"c1\"># Next element is in the left part</span>      <span class=\"k\">if</span> <span class=\"n\">idx</span> <span class=\"o\">&gt;</span> <span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">:</span>        <span class=\"n\">root</span><span class=\"p\">.</span><span class=\"n\">left</span> <span class=\"o\">=</span> <span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"n\">curr</span><span class=\"p\">)</span>        <span class=\"nf\">inner</span><span class=\"p\">(</span><span class=\"n\">root</span><span class=\"p\">.</span><span class=\"n\">left</span><span class=\"p\">,</span> <span class=\"n\">preorder</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">:],</span> <span class=\"n\">left</span><span class=\"p\">[:</span><span class=\"n\">idx</span><span class=\"p\">],</span> <span class=\"n\">left</span><span class=\"p\">[</span><span class=\"n\">idx</span><span class=\"o\">+</span><span class=\"mi\">1</span><span class=\"p\">:])</span>            <span class=\"n\">preorder</span> <span class=\"o\">=</span> <span class=\"n\">preorder</span><span class=\"p\">[</span><span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">left</span><span class=\"p\">):]</span>      <span class=\"n\">idx</span> <span class=\"o\">=</span> <span class=\"o\">-</span><span class=\"mi\">1</span>      <span class=\"k\">for</span> <span class=\"n\">i</span> <span class=\"ow\">in</span> <span class=\"nf\">range</span><span class=\"p\">(</span><span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">right</span><span class=\"p\">)):</span>        <span class=\"k\">if</span> <span class=\"n\">right</span><span class=\"p\">[</span><span class=\"n\">i</span><span class=\"p\">]</span> <span class=\"o\">==</span> <span class=\"n\">preorder</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]:</span>          <span class=\"n\">idx</span> <span class=\"o\">=</span> <span class=\"n\">i</span>          <span class=\"k\">break</span>      <span class=\"k\">if</span> <span class=\"n\">idx</span> <span class=\"o\">&gt;</span> <span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">:</span>        <span class=\"n\">root</span><span class=\"p\">.</span><span class=\"n\">right</span> <span class=\"o\">=</span> <span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"n\">preorder</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">])</span>        <span class=\"nf\">inner</span><span class=\"p\">(</span><span class=\"n\">root</span><span class=\"p\">.</span><span class=\"n\">right</span><span class=\"p\">,</span> <span class=\"n\">preorder</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">:],</span> <span class=\"n\">right</span><span class=\"p\">[:</span><span class=\"n\">idx</span><span class=\"p\">],</span> <span class=\"n\">right</span><span class=\"p\">[</span><span class=\"n\">idx</span><span class=\"o\">+</span><span class=\"mi\">1</span><span class=\"p\">:])</span>    <span class=\"n\">root</span> <span class=\"o\">=</span> <span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"n\">preorder</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">])</span>    <span class=\"n\">idx</span> <span class=\"o\">=</span> <span class=\"o\">-</span><span class=\"mi\">1</span>    <span class=\"k\">for</span> <span class=\"n\">i</span> <span class=\"ow\">in</span> <span class=\"nf\">range</span><span class=\"p\">(</span><span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">inorder</span><span class=\"p\">)):</span>      <span class=\"k\">if</span> <span class=\"n\">inorder</span><span class=\"p\">[</span><span class=\"n\">i</span><span class=\"p\">]</span> <span class=\"o\">==</span> <span class=\"n\">preorder</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]:</span>        <span class=\"n\">idx</span> <span class=\"o\">=</span> <span class=\"n\">i</span>    <span class=\"nf\">inner</span><span class=\"p\">(</span><span class=\"n\">root</span><span class=\"p\">,</span> <span class=\"n\">preorder</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">:],</span> <span class=\"n\">inorder</span><span class=\"p\">[:</span><span class=\"n\">idx</span><span class=\"p\">],</span> <span class=\"n\">inorder</span><span class=\"p\">[</span><span class=\"n\">idx</span><span class=\"o\">+</span><span class=\"mi\">1</span><span class=\"p\">:])</span>    <span class=\"k\">return</span> <span class=\"n\">root</span>              <span class=\"k\">if</span> <span class=\"n\">__name__</span> <span class=\"o\">==</span> <span class=\"sh\">'</span><span class=\"s\">__main__</span><span class=\"sh\">'</span><span class=\"p\">:</span>  <span class=\"n\">s</span> <span class=\"o\">=</span> <span class=\"nc\">Solution</span><span class=\"p\">().</span><span class=\"n\">buildTree</span>  <span class=\"n\">t1</span> <span class=\"o\">=</span> <span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"mi\">3</span><span class=\"p\">,</span> <span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"mi\">9</span><span class=\"p\">),</span> <span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"mi\">20</span><span class=\"p\">,</span> <span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"mi\">15</span><span class=\"p\">),</span> <span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"mi\">7</span><span class=\"p\">)))</span>  <span class=\"c1\"># print(s([3,9,20,15,7,81], [9,3,15,20,81,7]), t1)</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">([</span><span class=\"mi\">3</span><span class=\"p\">,</span><span class=\"mi\">1</span><span class=\"p\">,</span><span class=\"mi\">2</span><span class=\"p\">,</span><span class=\"mi\">4</span><span class=\"p\">],</span> <span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">,</span><span class=\"mi\">2</span><span class=\"p\">,</span><span class=\"mi\">3</span><span class=\"p\">,</span><span class=\"mi\">4</span><span class=\"p\">]))</span></code></pre></figure><blockquote>  <p>Runtime: 722 ms, faster than 5.01% of Python3 online submissions for Construct Binary Tree from Preorder and Inorder Traversal. Memory Usage: 89.2 MB, less than 5.37% of Python3 online submissions for Construct Binary Tree from Preorder and Inorder Traversal.</p></blockquote>",
      "date_published": "2022-11-01T20:00:00+00:00"
    },
    {
      "id": "https://fewald.net/leetcode/2022/10/31/leetcode-validate-binary-search-tree.html",
      "url": "https://fewald.net/leetcode/2022/10/31/leetcode-validate-binary-search-tree.html",
      "title": "Leetcode: Validate Binary Search Tree",
      "content_html": "<p>Given the <code class=\"language-plaintext highlighter-rouge\">root</code> node of a tree, validate whether it is a valid <strong>Binary Search Tree</strong>. This is a tree where each node is unique, left children are smaller than the root and right children are greater than the root.</p><!--more--><p>Because we’re dealing with a tree, there are two possible algorithms that we can use: DFS and BFS. In this case, I decided to go with a recursive DFS approach. In a simple example we see, that we need to compare the <code class=\"language-plaintext highlighter-rouge\">left</code> child with the <code class=\"language-plaintext highlighter-rouge\">val</code> of the current node. If the <code class=\"language-plaintext highlighter-rouge\">left</code> child is equal or greater than the value of the root node, we can return <code class=\"language-plaintext highlighter-rouge\">False</code> immediately and don’t need to traverse through the rest of the tree. Otherwise, we need to recursively visit the <code class=\"language-plaintext highlighter-rouge\">left</code> and <code class=\"language-plaintext highlighter-rouge\">right</code> children respectively.</p><figure class=\"highlight\"><pre><code class=\"language-plain\" data-lang=\"plain\">     2    / \\   1   5</code></pre></figure><p>If we expand the example above, we now see that we need to check also the children of <code class=\"language-plaintext highlighter-rouge\">5</code>. We then need to compare them to the <code class=\"language-plaintext highlighter-rouge\">root</code> of the tree. In this particular example, <code class=\"language-plaintext highlighter-rouge\">2</code> is less than <code class=\"language-plaintext highlighter-rouge\">3</code> and the tree is not a valid binary search tree.</p><p>To make the comparison recursively, we can pass ranges to the method in which the value has to be. We start with <code class=\"language-plaintext highlighter-rouge\">[float('-inf'), float('inf')]</code> and update it once we reach the first node. If we go to the left, we know that the <strong>upper</strong> boundary is now the nodes values. If we go to the right, we set the <strong>lower</strong> boundary to the <code class=\"language-plaintext highlighter-rouge\">max(lower, node.value)</code>.</p><figure class=\"highlight\"><pre><code class=\"language-plain\" data-lang=\"plain\">     3    / \\   1   5      / \\     2   9</code></pre></figure><p>Using <code class=\"language-plaintext highlighter-rouge\">float('-inf')</code> and <code class=\"language-plaintext highlighter-rouge\">float('inf')</code> respectively is a common way to initialize values in Python that need to get updated iteratively. The end result is the following code.</p><figure class=\"highlight\"><pre><code class=\"language-python\" data-lang=\"python\"><span class=\"kn\">from</span> <span class=\"n\">typing</span> <span class=\"kn\">import</span> <span class=\"n\">List</span><span class=\"p\">,</span> <span class=\"n\">Optional</span><span class=\"c1\"># Definition for a binary tree node.</span><span class=\"k\">class</span> <span class=\"nc\">TreeNode</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">__init__</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">val</span><span class=\"o\">=</span><span class=\"mi\">0</span><span class=\"p\">,</span> <span class=\"n\">left</span><span class=\"o\">=</span><span class=\"bp\">None</span><span class=\"p\">,</span> <span class=\"n\">right</span><span class=\"o\">=</span><span class=\"bp\">None</span><span class=\"p\">):</span>    <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">val</span> <span class=\"o\">=</span> <span class=\"n\">val</span>    <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">left</span> <span class=\"o\">=</span> <span class=\"n\">left</span>    <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">right</span> <span class=\"o\">=</span> <span class=\"n\">right</span>    <span class=\"k\">def</span> <span class=\"nf\">__repr__</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">)</span> <span class=\"o\">-&gt;</span> <span class=\"nb\">str</span><span class=\"p\">:</span>    <span class=\"sh\">\"\"\"</span><span class=\"s\">Convenience function for easy debugging</span><span class=\"sh\">\"\"\"</span>    <span class=\"k\">return</span> <span class=\"nf\">str</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">val</span><span class=\"p\">)</span><span class=\"k\">class</span> <span class=\"nc\">Solution</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">isValidBST</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">root</span><span class=\"p\">:</span> <span class=\"n\">Optional</span><span class=\"p\">[</span><span class=\"n\">TreeNode</span><span class=\"p\">])</span> <span class=\"o\">-&gt;</span> <span class=\"nb\">bool</span><span class=\"p\">:</span>    <span class=\"k\">if</span> <span class=\"n\">root</span> <span class=\"ow\">is</span> <span class=\"bp\">None</span><span class=\"p\">:</span>      <span class=\"k\">return</span> <span class=\"bp\">None</span>        <span class=\"k\">def</span> <span class=\"nf\">inner</span><span class=\"p\">(</span><span class=\"n\">root</span><span class=\"p\">,</span> <span class=\"n\">lt</span><span class=\"o\">=</span><span class=\"bp\">None</span><span class=\"p\">,</span> <span class=\"n\">gt</span><span class=\"o\">=</span><span class=\"bp\">None</span><span class=\"p\">):</span>      <span class=\"k\">if</span> <span class=\"n\">root</span> <span class=\"ow\">is</span> <span class=\"bp\">None</span><span class=\"p\">:</span>        <span class=\"k\">return</span> <span class=\"bp\">True</span>            <span class=\"k\">if</span> <span class=\"n\">root</span><span class=\"p\">.</span><span class=\"n\">val</span> <span class=\"o\">&lt;=</span> <span class=\"n\">lt</span> <span class=\"ow\">or</span> <span class=\"n\">root</span><span class=\"p\">.</span><span class=\"n\">val</span> <span class=\"o\">&gt;=</span> <span class=\"n\">gt</span><span class=\"p\">:</span>        <span class=\"k\">return</span> <span class=\"bp\">False</span>            <span class=\"n\">left</span> <span class=\"o\">=</span> <span class=\"nf\">inner</span><span class=\"p\">(</span><span class=\"n\">root</span><span class=\"p\">.</span><span class=\"n\">left</span><span class=\"p\">,</span> <span class=\"n\">lt</span><span class=\"o\">=</span><span class=\"n\">lt</span><span class=\"p\">,</span> <span class=\"n\">gt</span><span class=\"o\">=</span><span class=\"nf\">min</span><span class=\"p\">(</span><span class=\"n\">gt</span><span class=\"p\">,</span> <span class=\"n\">root</span><span class=\"p\">.</span><span class=\"n\">val</span><span class=\"p\">))</span>      <span class=\"n\">right</span> <span class=\"o\">=</span> <span class=\"nf\">inner</span><span class=\"p\">(</span><span class=\"n\">root</span><span class=\"p\">.</span><span class=\"n\">right</span><span class=\"p\">,</span> <span class=\"n\">lt</span><span class=\"o\">=</span><span class=\"nf\">max</span><span class=\"p\">(</span><span class=\"n\">lt</span><span class=\"p\">,</span> <span class=\"n\">root</span><span class=\"p\">.</span><span class=\"n\">val</span><span class=\"p\">),</span> <span class=\"n\">gt</span><span class=\"o\">=</span><span class=\"n\">gt</span><span class=\"p\">)</span>      <span class=\"k\">return</span> <span class=\"n\">left</span> <span class=\"ow\">and</span> <span class=\"n\">right</span>        <span class=\"k\">return</span> <span class=\"nf\">inner</span><span class=\"p\">(</span><span class=\"n\">root</span><span class=\"p\">,</span> <span class=\"nf\">float</span><span class=\"p\">(</span><span class=\"sh\">'</span><span class=\"s\">-inf</span><span class=\"sh\">'</span><span class=\"p\">),</span> <span class=\"nf\">float</span><span class=\"p\">(</span><span class=\"sh\">'</span><span class=\"s\">inf</span><span class=\"sh\">'</span><span class=\"p\">))</span>      <span class=\"k\">if</span> <span class=\"n\">__name__</span> <span class=\"o\">==</span> <span class=\"sh\">'</span><span class=\"s\">__main__</span><span class=\"sh\">'</span><span class=\"p\">:</span>  <span class=\"n\">s</span> <span class=\"o\">=</span> <span class=\"nc\">Solution</span><span class=\"p\">().</span><span class=\"n\">isValidBST</span>  <span class=\"n\">t1</span> <span class=\"o\">=</span> <span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"mi\">2</span><span class=\"p\">,</span> <span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"mi\">1</span><span class=\"p\">),</span> <span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"mi\">3</span><span class=\"p\">))</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">(</span><span class=\"n\">t1</span><span class=\"p\">),</span> <span class=\"bp\">True</span><span class=\"p\">)</span>  <span class=\"n\">t2</span> <span class=\"o\">=</span> <span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"mi\">5</span><span class=\"p\">,</span> <span class=\"bp\">None</span><span class=\"p\">,</span> <span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"mi\">7</span><span class=\"p\">,</span> <span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"mi\">4</span><span class=\"p\">)))</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">(</span><span class=\"n\">t2</span><span class=\"p\">),</span> <span class=\"bp\">False</span><span class=\"p\">)</span>  <span class=\"n\">t3</span> <span class=\"o\">=</span> <span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"mi\">0</span><span class=\"p\">,</span> <span class=\"bp\">None</span><span class=\"p\">,</span> <span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">))</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">(</span><span class=\"n\">t3</span><span class=\"p\">),</span> <span class=\"bp\">False</span><span class=\"p\">)</span>  <span class=\"n\">t4</span> <span class=\"o\">=</span> <span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"mi\">32</span><span class=\"p\">,</span> <span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"mi\">26</span><span class=\"p\">,</span> <span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"mi\">19</span><span class=\"p\">,</span> <span class=\"bp\">None</span><span class=\"p\">,</span> <span class=\"bp\">None</span><span class=\"p\">),</span> <span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"mi\">47</span><span class=\"p\">,</span> <span class=\"bp\">None</span><span class=\"p\">,</span> <span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"mi\">56</span><span class=\"p\">,</span> <span class=\"bp\">None</span><span class=\"p\">,</span> <span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"mi\">27</span><span class=\"p\">)))))</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">(</span><span class=\"n\">t4</span><span class=\"p\">),</span> <span class=\"bp\">False</span><span class=\"p\">)</span></code></pre></figure><blockquote>  <p>Runtime: 106 ms, faster than 5.15% of Python3 online submissions for Validate Binary Search Tree. Memory Usage: 16.5 MB, less than 47.01% of Python3 online submissions for Validate Binary Search Tree.</p></blockquote>",
      "date_published": "2022-10-31T20:00:00+00:00"
    },
    {
      "id": "https://fewald.net/leetcode/2022/10/30/leetcode-palindrome-partitioning.html",
      "url": "https://fewald.net/leetcode/2022/10/30/leetcode-palindrome-partitioning.html",
      "title": "Leetcode: Palindrome partitioning",
      "content_html": "<p>Given a string <code class=\"language-plaintext highlighter-rouge\">s</code>, return a <code class=\"language-plaintext highlighter-rouge\">list</code> containing of all possible partitions of that string that are palindromes. A palindrome is a string that is the same backwards as forwards. For example, <code class=\"language-plaintext highlighter-rouge\">abba</code> is a palindrome.</p><p>The string <code class=\"language-plaintext highlighter-rouge\">aab</code> can be partitioned as follows: <code class=\"language-plaintext highlighter-rouge\">[['a','a','b'], ['aa','b']]</code>. This is because a single character is by definition a palindrome.</p><!--more--><p>When thinking about the solution, we need to first think about the cases. The palindrome can be</p><ul>  <li>at the beginning,</li>  <li>in the middle,</li>  <li>or at the end</li></ul><p>of the string. Additionally there can be more than one palindrome at the same time. The base case is that every character is a palindrome on its own.</p><p>One possible solution is to look at this as a recursive problem with a prefix and a remainder of the string. We can then attempt to greedily find palindromes in the remainder.</p><h2 id=\"example\">Example</h2><figure class=\"highlight\"><pre><code class=\"language-plain\" data-lang=\"plain\">s = aabStep 1prefix = [], remainder = [a,a,b]Step 2aprefix = [a], remainder = [a,b]Step 3aprefix = [a,a], remainder = [b]Step 4aprefix = [a,a,b] remainder = []Now we can return to step 2a and add one more character and check for palindrome.Step 2bprefix = [aa], remainder = [b]Step 3bprefix = [aa,b], remainder = []Step2c[aab] is not a palindrome and gets discarded.</code></pre></figure><h2 id=\"solution\">Solution</h2><p>The following code snippet shows the recursive implementation.</p><figure class=\"highlight\"><pre><code class=\"language-python\" data-lang=\"python\"><span class=\"kn\">from</span> <span class=\"n\">typing</span> <span class=\"kn\">import</span> <span class=\"n\">List</span><span class=\"p\">,</span> <span class=\"n\">Optional</span><span class=\"k\">class</span> <span class=\"nc\">Solution</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">partition</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">s</span><span class=\"p\">:</span> <span class=\"nb\">str</span><span class=\"p\">)</span> <span class=\"o\">-&gt;</span> <span class=\"n\">List</span><span class=\"p\">[</span><span class=\"n\">List</span><span class=\"p\">[</span><span class=\"nb\">str</span><span class=\"p\">]]:</span>    <span class=\"c1\"># Convert string to list of characters</span>    <span class=\"n\">s</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">c</span> <span class=\"k\">for</span> <span class=\"n\">c</span> <span class=\"ow\">in</span> <span class=\"n\">s</span><span class=\"p\">]</span>    <span class=\"c1\"># Initialize output array/list.</span>    <span class=\"n\">out</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>    <span class=\"k\">def</span> <span class=\"nf\">inner</span><span class=\"p\">(</span><span class=\"n\">prefix</span><span class=\"p\">,</span> <span class=\"n\">s</span><span class=\"p\">):</span>      <span class=\"k\">if</span> <span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">s</span><span class=\"p\">)</span> <span class=\"o\">==</span> <span class=\"mi\">0</span><span class=\"p\">:</span>        <span class=\"n\">out</span><span class=\"p\">.</span><span class=\"nf\">append</span><span class=\"p\">(</span><span class=\"n\">prefix</span><span class=\"p\">)</span>        <span class=\"k\">return</span>            <span class=\"k\">for</span> <span class=\"n\">j</span> <span class=\"ow\">in</span> <span class=\"nf\">range</span><span class=\"p\">(</span><span class=\"mi\">1</span><span class=\"p\">,</span> <span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">s</span><span class=\"p\">)</span><span class=\"o\">+</span><span class=\"mi\">1</span><span class=\"p\">):</span>        <span class=\"n\">tmp</span> <span class=\"o\">=</span> <span class=\"n\">s</span><span class=\"p\">[:</span><span class=\"n\">j</span><span class=\"p\">]</span>        <span class=\"k\">if</span> <span class=\"n\">tmp</span> <span class=\"o\">==</span> <span class=\"n\">tmp</span><span class=\"p\">[::</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">]:</span>          <span class=\"nf\">inner</span><span class=\"p\">(</span><span class=\"n\">prefix</span> <span class=\"o\">+</span> <span class=\"p\">[</span><span class=\"sh\">\"\"</span><span class=\"p\">.</span><span class=\"nf\">join</span><span class=\"p\">(</span><span class=\"n\">tmp</span><span class=\"p\">)],</span> <span class=\"n\">s</span><span class=\"p\">[</span><span class=\"n\">j</span><span class=\"p\">:])</span>    <span class=\"c1\"># Call to recursive method        </span>    <span class=\"nf\">inner</span><span class=\"p\">([],</span><span class=\"n\">s</span><span class=\"p\">)</span>    <span class=\"k\">return</span> <span class=\"n\">out</span><span class=\"k\">if</span> <span class=\"n\">__name__</span> <span class=\"o\">==</span> <span class=\"sh\">'</span><span class=\"s\">__main__</span><span class=\"sh\">'</span><span class=\"p\">:</span>  <span class=\"n\">s</span> <span class=\"o\">=</span> <span class=\"nc\">Solution</span><span class=\"p\">().</span><span class=\"n\">partition</span>  <span class=\"c1\"># print(s('aab'), [['a','a','b'], ['aa', 'b']])</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">(</span><span class=\"sh\">'</span><span class=\"s\">abacccac</span><span class=\"sh\">'</span><span class=\"p\">),</span> <span class=\"p\">[</span>    <span class=\"p\">[</span><span class=\"sh\">'</span><span class=\"s\">a</span><span class=\"sh\">'</span><span class=\"p\">,</span><span class=\"sh\">'</span><span class=\"s\">b</span><span class=\"sh\">'</span><span class=\"p\">,</span><span class=\"sh\">'</span><span class=\"s\">a</span><span class=\"sh\">'</span><span class=\"p\">,</span><span class=\"sh\">'</span><span class=\"s\">c</span><span class=\"sh\">'</span><span class=\"p\">,</span><span class=\"sh\">'</span><span class=\"s\">c</span><span class=\"sh\">'</span><span class=\"p\">,</span><span class=\"sh\">'</span><span class=\"s\">c</span><span class=\"sh\">'</span><span class=\"p\">,</span><span class=\"sh\">'</span><span class=\"s\">a</span><span class=\"sh\">'</span><span class=\"p\">,</span><span class=\"sh\">'</span><span class=\"s\">c</span><span class=\"sh\">'</span><span class=\"p\">],</span>    <span class=\"p\">[</span><span class=\"sh\">'</span><span class=\"s\">aba</span><span class=\"sh\">'</span><span class=\"p\">,</span> <span class=\"sh\">'</span><span class=\"s\">c</span><span class=\"sh\">'</span><span class=\"p\">,</span> <span class=\"sh\">'</span><span class=\"s\">c</span><span class=\"sh\">'</span><span class=\"p\">,</span><span class=\"sh\">'</span><span class=\"s\">c</span><span class=\"sh\">'</span><span class=\"p\">,</span><span class=\"sh\">'</span><span class=\"s\">a</span><span class=\"sh\">'</span><span class=\"p\">,</span><span class=\"sh\">'</span><span class=\"s\">c</span><span class=\"sh\">'</span><span class=\"p\">],</span>    <span class=\"p\">])</span></code></pre></figure><blockquote>  <p>Runtime: 1690 ms, faster than 5.03% of Python3 online submissions for Palindrome Partitioning. Memory Usage: 30.4 MB, less than 45.23% of Python3 online submissions for Palindrome Partitioning.</p></blockquote>",
      "date_published": "2022-10-30T20:00:00+00:00"
    },
    {
      "id": "https://fewald.net/leetcode/2022/10/29/leetcode-remove-element-from-list.html",
      "url": "https://fewald.net/leetcode/2022/10/29/leetcode-remove-element-from-list.html",
      "title": "Leetcode: Remove element from list",
      "content_html": "<p>Given the <code class=\"language-plaintext highlighter-rouge\">head</code> of a linked list, remove all items with <code class=\"language-plaintext highlighter-rouge\">val</code>. Afterwards, return the new head.</p><!--more--><p>To solve this problem, we need to consider three cases. The element can be in the beginning, in the middle or at the end. For the middle case, we can just define <code class=\"language-plaintext highlighter-rouge\">el.next = el.next.next</code>. This also works for the end, except that we need to make sure that the <code class=\"language-plaintext highlighter-rouge\">next</code> element is not <code class=\"language-plaintext highlighter-rouge\">None</code>. The beginning is a little bit more difficult. Here we can work around this problem by adding a pseudo-node in front of the beginning and then always look at the <code class=\"language-plaintext highlighter-rouge\">next</code> one. At the end we return <code class=\"language-plaintext highlighter-rouge\">new_head.next</code>. The other corner case is if two nodes that need to be deleted are right next to each other. In this case we cannot move the pointer forward.</p><figure class=\"highlight\"><pre><code class=\"language-python\" data-lang=\"python\"><span class=\"kn\">from</span> <span class=\"n\">typing</span> <span class=\"kn\">import</span> <span class=\"n\">Optional</span><span class=\"c1\"># Definition for singly-linked list.</span><span class=\"k\">class</span> <span class=\"nc\">ListNode</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">__init__</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">val</span><span class=\"o\">=</span><span class=\"mi\">0</span><span class=\"p\">,</span> <span class=\"nb\">next</span><span class=\"o\">=</span><span class=\"bp\">None</span><span class=\"p\">):</span>    <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">val</span> <span class=\"o\">=</span> <span class=\"n\">val</span>    <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"nb\">next</span> <span class=\"o\">=</span> <span class=\"nb\">next</span>    <span class=\"k\">def</span> <span class=\"nf\">__repr__</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">)</span> <span class=\"o\">-&gt;</span> <span class=\"nb\">str</span><span class=\"p\">:</span>    <span class=\"n\">l</span> <span class=\"o\">=</span> <span class=\"nf\">str</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">val</span><span class=\"p\">)</span>    <span class=\"k\">if</span> <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"nb\">next</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"bp\">None</span><span class=\"p\">:</span>      <span class=\"n\">l</span> <span class=\"o\">+=</span> <span class=\"sa\">f</span><span class=\"sh\">\"</span><span class=\"s\">, </span><span class=\"si\">{</span><span class=\"n\">self</span><span class=\"p\">.</span><span class=\"nb\">next</span><span class=\"si\">}</span><span class=\"sh\">\"</span>    <span class=\"k\">return</span> <span class=\"n\">l</span><span class=\"k\">class</span> <span class=\"nc\">Solution</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">removeElements</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">head</span><span class=\"p\">:</span> <span class=\"n\">Optional</span><span class=\"p\">[</span><span class=\"n\">ListNode</span><span class=\"p\">],</span> <span class=\"n\">val</span><span class=\"p\">:</span> <span class=\"nb\">int</span><span class=\"p\">)</span> <span class=\"o\">-&gt;</span> <span class=\"n\">Optional</span><span class=\"p\">[</span><span class=\"n\">ListNode</span><span class=\"p\">]:</span>    <span class=\"n\">new_head</span> <span class=\"o\">=</span> <span class=\"nc\">ListNode</span><span class=\"p\">(</span><span class=\"mi\">0</span><span class=\"p\">,</span> <span class=\"n\">head</span><span class=\"p\">)</span>    <span class=\"n\">n</span> <span class=\"o\">=</span> <span class=\"n\">new_head</span>    <span class=\"k\">while</span> <span class=\"n\">n</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"bp\">None</span><span class=\"p\">:</span>      <span class=\"k\">if</span> <span class=\"n\">n</span><span class=\"p\">.</span><span class=\"nb\">next</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"bp\">None</span> <span class=\"ow\">and</span> <span class=\"n\">n</span><span class=\"p\">.</span><span class=\"nb\">next</span><span class=\"p\">.</span><span class=\"n\">val</span> <span class=\"o\">==</span> <span class=\"n\">val</span><span class=\"p\">:</span>        <span class=\"n\">n</span><span class=\"p\">.</span><span class=\"nb\">next</span> <span class=\"o\">=</span> <span class=\"n\">n</span><span class=\"p\">.</span><span class=\"nb\">next</span><span class=\"p\">.</span><span class=\"nb\">next</span>      <span class=\"k\">else</span><span class=\"p\">:</span>        <span class=\"n\">n</span> <span class=\"o\">=</span> <span class=\"n\">n</span><span class=\"p\">.</span><span class=\"nb\">next</span>    <span class=\"k\">return</span> <span class=\"n\">new_head</span><span class=\"p\">.</span><span class=\"nb\">next</span><span class=\"k\">if</span> <span class=\"n\">__name__</span> <span class=\"o\">==</span> <span class=\"sh\">'</span><span class=\"s\">__main__</span><span class=\"sh\">'</span><span class=\"p\">:</span>  <span class=\"n\">s</span> <span class=\"o\">=</span> <span class=\"nc\">Solution</span><span class=\"p\">().</span><span class=\"n\">removeElements</span>  <span class=\"n\">l</span> <span class=\"o\">=</span> <span class=\"nc\">ListNode</span><span class=\"p\">(</span><span class=\"mi\">1</span><span class=\"p\">,</span> <span class=\"nc\">ListNode</span><span class=\"p\">(</span><span class=\"mi\">2</span><span class=\"p\">,</span> <span class=\"nc\">ListNode</span><span class=\"p\">(</span><span class=\"mi\">6</span><span class=\"p\">,</span> <span class=\"nc\">ListNode</span><span class=\"p\">(</span><span class=\"mi\">3</span><span class=\"p\">,</span> <span class=\"nc\">ListNode</span><span class=\"p\">(</span><span class=\"mi\">4</span><span class=\"p\">,</span> <span class=\"nc\">ListNode</span><span class=\"p\">(</span><span class=\"mi\">5</span><span class=\"p\">,</span> <span class=\"nc\">ListNode</span><span class=\"p\">(</span><span class=\"mi\">6</span><span class=\"p\">)))))))</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">(</span><span class=\"n\">l</span><span class=\"p\">,</span> <span class=\"mi\">6</span><span class=\"p\">))</span></code></pre></figure><blockquote>  <p>Runtime: 169 ms, faster than 5.14% of Python3 online submissions for Remove Linked List Elements. Memory Usage: 17.8 MB, less than 81.69% of Python3 online submissions for Remove Linked List Elements.</p></blockquote>",
      "date_published": "2022-10-29T20:00:00+00:00"
    },
    {
      "id": "https://fewald.net/leetcode/2022/10/28/leetcode-array-remove-element.html",
      "url": "https://fewald.net/leetcode/2022/10/28/leetcode-array-remove-element.html",
      "title": "Leetcode: Remove element",
      "content_html": "<p>Given an array <code class=\"language-plaintext highlighter-rouge\">nums</code>, delete all items that are equal to <code class=\"language-plaintext highlighter-rouge\">val</code>. Return the total number of remaining items and perform the deletion in place.</p><!--more--><p>The solution looks as follows. Initially, <code class=\"language-plaintext highlighter-rouge\">k</code> is set to the length of the list. If we remove one element we decrement <code class=\"language-plaintext highlighter-rouge\">k</code> by one. The one thing here is that we advance the pointer <code class=\"language-plaintext highlighter-rouge\">i</code> only if we don’t remove an element. This is due to the fact that the position of the following elements change if we delete an item from the array.</p><figure class=\"highlight\"><pre><code class=\"language-python\" data-lang=\"python\"><span class=\"kn\">from</span> <span class=\"n\">typing</span> <span class=\"kn\">import</span> <span class=\"n\">List</span><span class=\"k\">class</span> <span class=\"nc\">Solution</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">removeElement</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">nums</span><span class=\"p\">:</span> <span class=\"n\">List</span><span class=\"p\">[</span><span class=\"nb\">int</span><span class=\"p\">],</span> <span class=\"n\">val</span><span class=\"p\">:</span> <span class=\"nb\">int</span><span class=\"p\">)</span> <span class=\"o\">-&gt;</span> <span class=\"nb\">int</span><span class=\"p\">:</span>    <span class=\"n\">i</span> <span class=\"o\">=</span> <span class=\"mi\">0</span>    <span class=\"n\">k</span> <span class=\"o\">=</span> <span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">nums</span><span class=\"p\">)</span>    <span class=\"k\">while</span> <span class=\"n\">i</span> <span class=\"o\">&lt;</span> <span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">nums</span><span class=\"p\">):</span>      <span class=\"k\">if</span> <span class=\"n\">nums</span><span class=\"p\">[</span><span class=\"n\">i</span><span class=\"p\">]</span> <span class=\"o\">==</span> <span class=\"n\">val</span><span class=\"p\">:</span>        <span class=\"k\">del</span> <span class=\"n\">nums</span><span class=\"p\">[</span><span class=\"n\">i</span><span class=\"p\">]</span>        <span class=\"n\">k</span> <span class=\"o\">-=</span> <span class=\"mi\">1</span>      <span class=\"k\">else</span><span class=\"p\">:</span>        <span class=\"n\">i</span> <span class=\"o\">+=</span> <span class=\"mi\">1</span>    <span class=\"k\">return</span> <span class=\"n\">k</span>    <span class=\"k\">if</span> <span class=\"n\">__name__</span> <span class=\"o\">==</span> <span class=\"sh\">'</span><span class=\"s\">__main__</span><span class=\"sh\">'</span><span class=\"p\">:</span>  <span class=\"n\">s</span> <span class=\"o\">=</span> <span class=\"nc\">Solution</span><span class=\"p\">().</span><span class=\"n\">removeElement</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">([</span><span class=\"mi\">3</span><span class=\"p\">,</span><span class=\"mi\">2</span><span class=\"p\">,</span><span class=\"mi\">2</span><span class=\"p\">,</span><span class=\"mi\">3</span><span class=\"p\">],</span> <span class=\"mi\">3</span><span class=\"p\">),</span> <span class=\"mi\">2</span><span class=\"p\">)</span></code></pre></figure>",
      "date_published": "2022-10-28T20:00:00+00:00"
    },
    {
      "id": "https://fewald.net/leetcode/2022/10/27/leetcode-binary-tree-zigzag-level-order-traversal.html",
      "url": "https://fewald.net/leetcode/2022/10/27/leetcode-binary-tree-zigzag-level-order-traversal.html",
      "title": "Leetcode: Binary tree zigzag level order traversal",
      "content_html": "<p>Traverse a tree where the <code class=\"language-plaintext highlighter-rouge\">root</code> node is given with a zig-zag level and return the values in a <code class=\"language-plaintext highlighter-rouge\">list</code> with one entry per level. For example:</p><figure class=\"highlight\"><pre><code class=\"language-plain\" data-lang=\"plain\">--&gt;    3    /     \\   9       5      &lt;--        /     \\--&gt;    7       8Returns: [[3], [5,9], [7,8]]</code></pre></figure><!--more--><p>This can be achieved with the same BFS algorithm from before, with an additional introduction of a <code class=\"language-plaintext highlighter-rouge\">reverse</code> boolean variable. If the order is reversed, the output values are prepended instead of appended. After each level, the reverse variable is toggled via <code class=\"language-plaintext highlighter-rouge\">reverse = not reverse</code>.</p><figure class=\"highlight\"><pre><code class=\"language-python\" data-lang=\"python\"><span class=\"kn\">from</span> <span class=\"n\">typing</span> <span class=\"kn\">import</span> <span class=\"n\">List</span><span class=\"p\">,</span> <span class=\"n\">Optional</span><span class=\"c1\"># Definition for a binary tree node.</span><span class=\"k\">class</span> <span class=\"nc\">TreeNode</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">__init__</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">val</span><span class=\"o\">=</span><span class=\"mi\">0</span><span class=\"p\">,</span> <span class=\"n\">left</span><span class=\"o\">=</span><span class=\"bp\">None</span><span class=\"p\">,</span> <span class=\"n\">right</span><span class=\"o\">=</span><span class=\"bp\">None</span><span class=\"p\">):</span>    <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">val</span> <span class=\"o\">=</span> <span class=\"n\">val</span>    <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">left</span> <span class=\"o\">=</span> <span class=\"n\">left</span>    <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">right</span> <span class=\"o\">=</span> <span class=\"n\">right</span>    <span class=\"k\">def</span> <span class=\"nf\">__repr__</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">)</span> <span class=\"o\">-&gt;</span> <span class=\"nb\">str</span><span class=\"p\">:</span>    <span class=\"k\">return</span> <span class=\"sa\">f</span><span class=\"sh\">\"</span><span class=\"si\">{</span><span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">val</span><span class=\"si\">}</span><span class=\"sh\">\"</span><span class=\"k\">class</span> <span class=\"nc\">Solution</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">zigzagLevelOrder</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">root</span><span class=\"p\">:</span> <span class=\"n\">Optional</span><span class=\"p\">[</span><span class=\"n\">TreeNode</span><span class=\"p\">])</span> <span class=\"o\">-&gt;</span> <span class=\"n\">List</span><span class=\"p\">[</span><span class=\"n\">List</span><span class=\"p\">[</span><span class=\"nb\">int</span><span class=\"p\">]]:</span>    <span class=\"k\">if</span> <span class=\"n\">root</span> <span class=\"ow\">is</span> <span class=\"bp\">None</span><span class=\"p\">:</span>      <span class=\"k\">return</span> <span class=\"p\">[]</span>        <span class=\"n\">res</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>    <span class=\"n\">queue</span> <span class=\"o\">=</span> <span class=\"p\">[[</span><span class=\"n\">root</span><span class=\"p\">]]</span>    <span class=\"n\">reverse</span> <span class=\"o\">=</span> <span class=\"bp\">False</span>        <span class=\"k\">while</span> <span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">queue</span><span class=\"p\">)</span> <span class=\"o\">&gt;</span> <span class=\"mi\">0</span><span class=\"p\">:</span>      <span class=\"n\">itms</span> <span class=\"o\">=</span> <span class=\"n\">queue</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span>      <span class=\"n\">res</span><span class=\"p\">.</span><span class=\"nf\">append</span><span class=\"p\">([])</span>      <span class=\"n\">queue</span><span class=\"p\">.</span><span class=\"nf\">append</span><span class=\"p\">([])</span>      <span class=\"k\">for</span> <span class=\"n\">item</span> <span class=\"ow\">in</span> <span class=\"n\">itms</span><span class=\"p\">:</span>        <span class=\"k\">if</span> <span class=\"ow\">not</span> <span class=\"n\">item</span><span class=\"p\">:</span>          <span class=\"k\">continue</span>        <span class=\"k\">if</span> <span class=\"n\">reverse</span><span class=\"p\">:</span>          <span class=\"c1\"># prepend</span>          <span class=\"n\">res</span><span class=\"p\">[</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">item</span><span class=\"p\">.</span><span class=\"n\">val</span><span class=\"p\">]</span> <span class=\"o\">+</span> <span class=\"n\">res</span><span class=\"p\">[</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">]</span>        <span class=\"k\">else</span><span class=\"p\">:</span>          <span class=\"n\">res</span><span class=\"p\">[</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">].</span><span class=\"nf\">append</span><span class=\"p\">(</span><span class=\"n\">item</span><span class=\"p\">.</span><span class=\"n\">val</span><span class=\"p\">)</span>                  <span class=\"n\">queue</span><span class=\"p\">[</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">].</span><span class=\"nf\">append</span><span class=\"p\">(</span><span class=\"n\">item</span><span class=\"p\">.</span><span class=\"n\">left</span><span class=\"p\">)</span>        <span class=\"n\">queue</span><span class=\"p\">[</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">].</span><span class=\"nf\">append</span><span class=\"p\">(</span><span class=\"n\">item</span><span class=\"p\">.</span><span class=\"n\">right</span><span class=\"p\">)</span>      <span class=\"k\">if</span> <span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">res</span><span class=\"p\">[</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">])</span> <span class=\"o\">==</span> <span class=\"mi\">0</span><span class=\"p\">:</span>        <span class=\"n\">queue</span><span class=\"p\">.</span><span class=\"nf\">pop</span><span class=\"p\">()</span>      <span class=\"n\">queue</span> <span class=\"o\">=</span> <span class=\"n\">queue</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">:]</span>      <span class=\"c1\"># Toggle reverse for next level</span>      <span class=\"n\">reverse</span> <span class=\"o\">=</span> <span class=\"ow\">not</span> <span class=\"n\">reverse</span>    <span class=\"k\">if</span> <span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">res</span><span class=\"p\">[</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">])</span> <span class=\"o\">==</span> <span class=\"mi\">0</span><span class=\"p\">:</span>      <span class=\"n\">res</span><span class=\"p\">.</span><span class=\"nf\">pop</span><span class=\"p\">()</span>    <span class=\"k\">return</span> <span class=\"n\">res</span><span class=\"k\">if</span> <span class=\"n\">__name__</span> <span class=\"o\">==</span> <span class=\"sh\">'</span><span class=\"s\">__main__</span><span class=\"sh\">'</span><span class=\"p\">:</span>  <span class=\"n\">s</span> <span class=\"o\">=</span> <span class=\"nc\">Solution</span><span class=\"p\">().</span><span class=\"n\">zigzagLevelOrder</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">(</span><span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"mi\">3</span><span class=\"p\">,</span> <span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"mi\">9</span><span class=\"p\">),</span> <span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"mi\">20</span><span class=\"p\">,</span> <span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"mi\">15</span><span class=\"p\">),</span> <span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"mi\">7</span><span class=\"p\">)))),</span> <span class=\"p\">[[</span><span class=\"mi\">3</span><span class=\"p\">],</span> <span class=\"p\">[</span><span class=\"mi\">20</span><span class=\"p\">,</span><span class=\"mi\">9</span><span class=\"p\">],</span> <span class=\"p\">[</span><span class=\"mi\">15</span><span class=\"p\">,</span><span class=\"mi\">7</span><span class=\"p\">]])</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">(</span><span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"mi\">1</span><span class=\"p\">)),</span> <span class=\"p\">[[</span><span class=\"mi\">1</span><span class=\"p\">]])</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">(</span><span class=\"bp\">None</span><span class=\"p\">),</span> <span class=\"p\">[])</span></code></pre></figure>",
      "date_published": "2022-10-27T20:00:00+00:00"
    },
    {
      "id": "https://fewald.net/leetcode/2022/10/26/leetcode-binary-tree-level-order-traversal.html",
      "url": "https://fewald.net/leetcode/2022/10/26/leetcode-binary-tree-level-order-traversal.html",
      "title": "Leetcode: Binary tree level order traversal",
      "content_html": "<p>Given the <code class=\"language-plaintext highlighter-rouge\">root</code> node of a binary tree, print all values in order, meaning from left to right on the same level.</p><p>To solve this, we can use the standard breadth-first-search (BFS) algorithm in an iterative fashion with a <code class=\"language-plaintext highlighter-rouge\">queue</code>. As an output we use a <code class=\"language-plaintext highlighter-rouge\">list</code> and leverage the position <code class=\"language-plaintext highlighter-rouge\">-1</code> to add the current level to the list. For every level we append one new element to the end of the list.</p><!--more--><figure class=\"highlight\"><pre><code class=\"language-python\" data-lang=\"python\"><span class=\"kn\">from</span> <span class=\"n\">typing</span> <span class=\"kn\">import</span> <span class=\"n\">List</span><span class=\"p\">,</span> <span class=\"n\">Optional</span><span class=\"c1\"># Definition for a binary tree node.</span><span class=\"k\">class</span> <span class=\"nc\">TreeNode</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">__init__</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">val</span><span class=\"o\">=</span><span class=\"mi\">0</span><span class=\"p\">,</span> <span class=\"n\">left</span><span class=\"o\">=</span><span class=\"bp\">None</span><span class=\"p\">,</span> <span class=\"n\">right</span><span class=\"o\">=</span><span class=\"bp\">None</span><span class=\"p\">):</span>    <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">val</span> <span class=\"o\">=</span> <span class=\"n\">val</span>    <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">left</span> <span class=\"o\">=</span> <span class=\"n\">left</span>    <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">right</span> <span class=\"o\">=</span> <span class=\"n\">right</span><span class=\"k\">class</span> <span class=\"nc\">Solution</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">levelOrder</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">root</span><span class=\"p\">:</span> <span class=\"n\">Optional</span><span class=\"p\">[</span><span class=\"n\">TreeNode</span><span class=\"p\">])</span> <span class=\"o\">-&gt;</span> <span class=\"n\">List</span><span class=\"p\">[</span><span class=\"n\">List</span><span class=\"p\">[</span><span class=\"nb\">int</span><span class=\"p\">]]:</span>    <span class=\"k\">if</span> <span class=\"n\">root</span> <span class=\"ow\">is</span> <span class=\"bp\">None</span><span class=\"p\">:</span>      <span class=\"k\">return</span> <span class=\"p\">[]</span>        <span class=\"n\">res</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>    <span class=\"n\">queue</span> <span class=\"o\">=</span> <span class=\"p\">[[</span><span class=\"n\">root</span><span class=\"p\">]]</span>        <span class=\"k\">while</span> <span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">queue</span><span class=\"p\">)</span> <span class=\"o\">&gt;</span> <span class=\"mi\">0</span><span class=\"p\">:</span>      <span class=\"n\">itms</span> <span class=\"o\">=</span> <span class=\"n\">queue</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span>      <span class=\"n\">res</span><span class=\"p\">.</span><span class=\"nf\">append</span><span class=\"p\">([])</span>      <span class=\"n\">queue</span><span class=\"p\">.</span><span class=\"nf\">append</span><span class=\"p\">([])</span>      <span class=\"k\">for</span> <span class=\"n\">item</span> <span class=\"ow\">in</span> <span class=\"n\">itms</span><span class=\"p\">:</span>        <span class=\"n\">res</span><span class=\"p\">[</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">].</span><span class=\"nf\">append</span><span class=\"p\">(</span><span class=\"n\">item</span><span class=\"p\">.</span><span class=\"n\">val</span><span class=\"p\">)</span>        <span class=\"k\">if</span> <span class=\"n\">item</span><span class=\"p\">.</span><span class=\"n\">left</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"bp\">None</span><span class=\"p\">:</span>          <span class=\"n\">queue</span><span class=\"p\">[</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">].</span><span class=\"nf\">append</span><span class=\"p\">(</span><span class=\"n\">item</span><span class=\"p\">.</span><span class=\"n\">left</span><span class=\"p\">)</span>        <span class=\"k\">if</span> <span class=\"n\">item</span><span class=\"p\">.</span><span class=\"n\">right</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"bp\">None</span><span class=\"p\">:</span>          <span class=\"n\">queue</span><span class=\"p\">[</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">].</span><span class=\"nf\">append</span><span class=\"p\">(</span><span class=\"n\">item</span><span class=\"p\">.</span><span class=\"n\">right</span><span class=\"p\">)</span>      <span class=\"k\">if</span> <span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">queue</span><span class=\"p\">[</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">])</span> <span class=\"o\">==</span> <span class=\"mi\">0</span><span class=\"p\">:</span>        <span class=\"n\">queue</span><span class=\"p\">.</span><span class=\"nf\">pop</span><span class=\"p\">()</span>      <span class=\"n\">queue</span> <span class=\"o\">=</span> <span class=\"n\">queue</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">:]</span>    <span class=\"k\">return</span> <span class=\"n\">res</span><span class=\"k\">if</span> <span class=\"n\">__name__</span> <span class=\"o\">==</span> <span class=\"sh\">'</span><span class=\"s\">__main__</span><span class=\"sh\">'</span><span class=\"p\">:</span>  <span class=\"n\">s</span> <span class=\"o\">=</span> <span class=\"nc\">Solution</span><span class=\"p\">().</span><span class=\"n\">levelOrder</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">(</span><span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"mi\">3</span><span class=\"p\">,</span> <span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"mi\">9</span><span class=\"p\">),</span> <span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"mi\">20</span><span class=\"p\">,</span> <span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"mi\">15</span><span class=\"p\">),</span> <span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"mi\">7</span><span class=\"p\">)))),</span> <span class=\"p\">[[</span><span class=\"mi\">3</span><span class=\"p\">],</span> <span class=\"p\">[</span><span class=\"mi\">9</span><span class=\"p\">,</span><span class=\"mi\">20</span><span class=\"p\">],</span> <span class=\"p\">[</span><span class=\"mi\">15</span><span class=\"p\">,</span><span class=\"mi\">7</span><span class=\"p\">]])</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">(</span><span class=\"nc\">TreeNode</span><span class=\"p\">(</span><span class=\"mi\">1</span><span class=\"p\">)),</span> <span class=\"p\">[[</span><span class=\"mi\">1</span><span class=\"p\">]])</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">(</span><span class=\"bp\">None</span><span class=\"p\">),</span> <span class=\"p\">[])</span></code></pre></figure><blockquote>  <p>Runtime: 74 ms, faster than 10.45% of Python3 online submissions for Binary Tree Level Order Traversal. Memory Usage: 14.2 MB, less than 84.74% of Python3 online submissions for Binary Tree Level Order Traversal.</p></blockquote>",
      "date_published": "2022-10-26T20:00:00+00:00"
    },
    {
      "id": "https://fewald.net/leetcode/2022/10/25/leetcode-valid-sudoku.html",
      "url": "https://fewald.net/leetcode/2022/10/25/leetcode-valid-sudoku.html",
      "title": "Leetcode: Valid sudoku",
      "content_html": "<p>Given a Sudoku field as a two-dimension list, determine if the field is valid. A valid field only has the number 1-9 once in each sub-field, each row, and each column. The field is not completely filled and we don’t need to check for a valid solution.</p><!--more--><p>The solution for this task is written in the conditions for a valid Sudoku field. We create a <code class=\"language-plaintext highlighter-rouge\">set</code> for each row, each column, and each sub-field. If a number is duplicated in any of those, the field is not valid. If we don’t find any violation until the end, the field is valid.</p><p>The remaining problem is to filter out the empty fields (<code class=\"language-plaintext highlighter-rouge\">.</code>) and to find the correct sub-field. For the second part, we can leverage Pythons full integer division and determine the field as follows: <code class=\"language-plaintext highlighter-rouge\">field = 3 * (row // 3) + (column // 3)</code>. This yields numbers from <code class=\"language-plaintext highlighter-rouge\">[0-8]</code>.</p><p>This solution is faster than 96% of the submitted solutions at leetcode. It does use <code class=\"language-plaintext highlighter-rouge\">O(3*n)</code> memory in the worst case, as every element needs to be stored in each set.</p><figure class=\"highlight\"><pre><code class=\"language-python\" data-lang=\"python\"><span class=\"kn\">from</span> <span class=\"n\">typing</span> <span class=\"kn\">import</span> <span class=\"n\">List</span><span class=\"k\">class</span> <span class=\"nc\">Solution</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">isValidSudoku</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">board</span><span class=\"p\">:</span> <span class=\"n\">List</span><span class=\"p\">[</span><span class=\"n\">List</span><span class=\"p\">[</span><span class=\"nb\">str</span><span class=\"p\">]])</span> <span class=\"o\">-&gt;</span> <span class=\"nb\">bool</span><span class=\"p\">:</span>    <span class=\"n\">rows</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"nf\">set</span><span class=\"p\">()</span> <span class=\"k\">for</span> <span class=\"n\">_</span> <span class=\"ow\">in</span> <span class=\"n\">board</span><span class=\"p\">]</span>    <span class=\"n\">cols</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"nf\">set</span><span class=\"p\">()</span> <span class=\"k\">for</span> <span class=\"n\">_</span> <span class=\"ow\">in</span> <span class=\"n\">board</span><span class=\"p\">]</span>    <span class=\"n\">subfields</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"nf\">set</span><span class=\"p\">()</span> <span class=\"k\">for</span> <span class=\"n\">_</span> <span class=\"ow\">in</span> <span class=\"nf\">range</span><span class=\"p\">(</span><span class=\"mi\">9</span><span class=\"p\">)]</span>    <span class=\"k\">for</span> <span class=\"n\">i</span> <span class=\"ow\">in</span> <span class=\"nf\">range</span><span class=\"p\">(</span><span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">board</span><span class=\"p\">)):</span>      <span class=\"k\">for</span> <span class=\"n\">j</span> <span class=\"ow\">in</span> <span class=\"nf\">range</span><span class=\"p\">(</span><span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">board</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">])):</span>        <span class=\"k\">if</span> <span class=\"n\">board</span><span class=\"p\">[</span><span class=\"n\">i</span><span class=\"p\">][</span><span class=\"n\">j</span><span class=\"p\">]</span> <span class=\"o\">==</span> <span class=\"sh\">'</span><span class=\"s\">.</span><span class=\"sh\">'</span><span class=\"p\">:</span>          <span class=\"k\">continue</span>                <span class=\"k\">if</span> <span class=\"n\">board</span><span class=\"p\">[</span><span class=\"n\">i</span><span class=\"p\">][</span><span class=\"n\">j</span><span class=\"p\">]</span> <span class=\"ow\">in</span> <span class=\"n\">rows</span><span class=\"p\">[</span><span class=\"n\">i</span><span class=\"p\">]:</span>          <span class=\"k\">return</span> <span class=\"bp\">False</span>        <span class=\"n\">rows</span><span class=\"p\">[</span><span class=\"n\">i</span><span class=\"p\">].</span><span class=\"nf\">add</span><span class=\"p\">(</span><span class=\"n\">board</span><span class=\"p\">[</span><span class=\"n\">i</span><span class=\"p\">][</span><span class=\"n\">j</span><span class=\"p\">])</span>        <span class=\"k\">if</span> <span class=\"n\">board</span><span class=\"p\">[</span><span class=\"n\">i</span><span class=\"p\">][</span><span class=\"n\">j</span><span class=\"p\">]</span> <span class=\"ow\">in</span> <span class=\"n\">cols</span><span class=\"p\">[</span><span class=\"n\">j</span><span class=\"p\">]:</span>          <span class=\"k\">return</span> <span class=\"bp\">False</span>        <span class=\"n\">cols</span><span class=\"p\">[</span><span class=\"n\">j</span><span class=\"p\">].</span><span class=\"nf\">add</span><span class=\"p\">(</span><span class=\"n\">board</span><span class=\"p\">[</span><span class=\"n\">i</span><span class=\"p\">][</span><span class=\"n\">j</span><span class=\"p\">])</span>        <span class=\"n\">field</span> <span class=\"o\">=</span> <span class=\"mi\">3</span> <span class=\"o\">*</span> <span class=\"p\">(</span><span class=\"n\">i</span> <span class=\"o\">//</span> <span class=\"mi\">3</span><span class=\"p\">)</span> <span class=\"o\">+</span> <span class=\"p\">(</span><span class=\"n\">j</span> <span class=\"o\">//</span> <span class=\"mi\">3</span><span class=\"p\">)</span>        <span class=\"k\">if</span> <span class=\"n\">board</span><span class=\"p\">[</span><span class=\"n\">i</span><span class=\"p\">][</span><span class=\"n\">j</span><span class=\"p\">]</span> <span class=\"ow\">in</span> <span class=\"n\">subfields</span><span class=\"p\">[</span><span class=\"n\">field</span><span class=\"p\">]:</span>          <span class=\"k\">return</span> <span class=\"bp\">False</span>        <span class=\"n\">subfields</span><span class=\"p\">[</span><span class=\"n\">field</span><span class=\"p\">].</span><span class=\"nf\">add</span><span class=\"p\">(</span><span class=\"n\">board</span><span class=\"p\">[</span><span class=\"n\">i</span><span class=\"p\">][</span><span class=\"n\">j</span><span class=\"p\">])</span>    <span class=\"k\">return</span> <span class=\"bp\">True</span><span class=\"k\">if</span> <span class=\"n\">__name__</span> <span class=\"o\">==</span> <span class=\"sh\">'</span><span class=\"s\">__main__</span><span class=\"sh\">'</span><span class=\"p\">:</span>  <span class=\"n\">s</span> <span class=\"o\">=</span> <span class=\"nc\">Solution</span><span class=\"p\">()</span>  <span class=\"c1\"># Valid board</span>  <span class=\"n\">board1</span> <span class=\"o\">=</span> <span class=\"p\">[[</span><span class=\"sh\">\"</span><span class=\"s\">5</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">3</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">7</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">]</span>           <span class=\"p\">,[</span><span class=\"sh\">\"</span><span class=\"s\">6</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">1</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">9</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">5</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">]</span>           <span class=\"p\">,[</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">9</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">8</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">6</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">]</span>           <span class=\"p\">,[</span><span class=\"sh\">\"</span><span class=\"s\">8</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">6</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">3</span><span class=\"sh\">\"</span><span class=\"p\">]</span>           <span class=\"p\">,[</span><span class=\"sh\">\"</span><span class=\"s\">4</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">8</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">3</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">1</span><span class=\"sh\">\"</span><span class=\"p\">]</span>           <span class=\"p\">,[</span><span class=\"sh\">\"</span><span class=\"s\">7</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">2</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">6</span><span class=\"sh\">\"</span><span class=\"p\">]</span>           <span class=\"p\">,[</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">6</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">2</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">8</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">]</span>           <span class=\"p\">,[</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">4</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">1</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">9</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">5</span><span class=\"sh\">\"</span><span class=\"p\">]</span>           <span class=\"p\">,[</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">8</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">7</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">9</span><span class=\"sh\">\"</span><span class=\"p\">]]</span>    <span class=\"c1\"># Invalid board</span>  <span class=\"n\">board2</span> <span class=\"o\">=</span> <span class=\"p\">[[</span><span class=\"sh\">\"</span><span class=\"s\">8</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">3</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">7</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">]</span>           <span class=\"p\">,[</span><span class=\"sh\">\"</span><span class=\"s\">6</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">1</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">9</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">5</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">]</span>           <span class=\"p\">,[</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">9</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">8</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">6</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">]</span>           <span class=\"p\">,[</span><span class=\"sh\">\"</span><span class=\"s\">8</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">6</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">3</span><span class=\"sh\">\"</span><span class=\"p\">]</span>           <span class=\"p\">,[</span><span class=\"sh\">\"</span><span class=\"s\">4</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">8</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">3</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">1</span><span class=\"sh\">\"</span><span class=\"p\">]</span>           <span class=\"p\">,[</span><span class=\"sh\">\"</span><span class=\"s\">7</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">2</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">6</span><span class=\"sh\">\"</span><span class=\"p\">]</span>           <span class=\"p\">,[</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">6</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">2</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">8</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">]</span>           <span class=\"p\">,[</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">4</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">1</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">9</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">5</span><span class=\"sh\">\"</span><span class=\"p\">]</span>           <span class=\"p\">,[</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">8</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">.</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">7</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">9</span><span class=\"sh\">\"</span><span class=\"p\">]]</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"n\">s</span><span class=\"p\">.</span><span class=\"nf\">isValidSudoku</span><span class=\"p\">(</span><span class=\"n\">board1</span><span class=\"p\">),</span> <span class=\"bp\">True</span><span class=\"p\">)</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"n\">s</span><span class=\"p\">.</span><span class=\"nf\">isValidSudoku</span><span class=\"p\">(</span><span class=\"n\">board2</span><span class=\"p\">),</span> <span class=\"bp\">False</span><span class=\"p\">)</span></code></pre></figure>",
      "date_published": "2022-10-25T20:00:00+00:00"
    },
    {
      "id": "https://fewald.net/leetcode/2022/10/24/leetcode-top-k-frequent-elements.html",
      "url": "https://fewald.net/leetcode/2022/10/24/leetcode-top-k-frequent-elements.html",
      "title": "Leetcode: Top k frequent elements",
      "content_html": "<p>Given a list <code class=\"language-plaintext highlighter-rouge\">nums</code> and a positive integer <code class=\"language-plaintext highlighter-rouge\">k</code>, return the top <code class=\"language-plaintext highlighter-rouge\">k</code> frequent items from the list. A time complexity constraint is not given for this task.</p><!--more--><p>The task can be broken down into two problems. First, identifying the most frequent items and then returning them. For the first part, I used a simple map to count the occurrences. Afterwards, I iterated over the map and flipped the <code class=\"language-plaintext highlighter-rouge\">key</code> and <code class=\"language-plaintext highlighter-rouge\">value</code> around in a tuple. If for example the number <code class=\"language-plaintext highlighter-rouge\">10</code> occurred <code class=\"language-plaintext highlighter-rouge\">5</code> times, the corresponding tuple would be <code class=\"language-plaintext highlighter-rouge\">(5, 10)</code>. With this structure I was able to use the <code class=\"language-plaintext highlighter-rouge\">heapq</code> package in Python. This library uses the first element of a tuple to determine the order of elements. As his library creates by default a  <strong>min</strong> heap and we are interested in the <strong>maximum</strong> elements, we can simply make the values negative. This puts the most frequent elements at the top of the heap.</p><figure class=\"highlight\"><pre><code class=\"language-python\" data-lang=\"python\"><span class=\"kn\">import</span> <span class=\"n\">heapq</span><span class=\"kn\">from</span> <span class=\"n\">typing</span> <span class=\"kn\">import</span> <span class=\"n\">List</span><span class=\"p\">,</span> <span class=\"n\">Optional</span><span class=\"k\">class</span> <span class=\"nc\">Solution</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">topKFrequent</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">nums</span><span class=\"p\">:</span> <span class=\"n\">List</span><span class=\"p\">[</span><span class=\"nb\">int</span><span class=\"p\">],</span> <span class=\"n\">k</span><span class=\"p\">:</span> <span class=\"nb\">int</span><span class=\"p\">)</span> <span class=\"o\">-&gt;</span> <span class=\"n\">List</span><span class=\"p\">[</span><span class=\"nb\">int</span><span class=\"p\">]:</span>    <span class=\"c1\"># Count all occurences</span>    <span class=\"n\">m</span> <span class=\"o\">=</span> <span class=\"p\">{}</span>    <span class=\"k\">for</span> <span class=\"n\">n</span> <span class=\"ow\">in</span> <span class=\"n\">nums</span><span class=\"p\">:</span>      <span class=\"k\">if</span> <span class=\"n\">n</span> <span class=\"ow\">not</span> <span class=\"ow\">in</span> <span class=\"n\">m</span><span class=\"p\">:</span>        <span class=\"n\">m</span><span class=\"p\">[</span><span class=\"n\">n</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"mi\">0</span>      <span class=\"n\">m</span><span class=\"p\">[</span><span class=\"n\">n</span><span class=\"p\">]</span> <span class=\"o\">+=</span> <span class=\"mi\">1</span>        <span class=\"n\">l</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>    <span class=\"k\">for</span> <span class=\"n\">key</span><span class=\"p\">,</span> <span class=\"n\">v</span> <span class=\"ow\">in</span> <span class=\"n\">m</span><span class=\"p\">.</span><span class=\"nf\">items</span><span class=\"p\">():</span>      <span class=\"n\">l</span><span class=\"p\">.</span><span class=\"nf\">append</span><span class=\"p\">((</span><span class=\"o\">-</span><span class=\"n\">key</span><span class=\"p\">,</span><span class=\"n\">v</span><span class=\"p\">))</span>        <span class=\"n\">heapq</span><span class=\"p\">.</span><span class=\"nf\">heapify</span><span class=\"p\">(</span><span class=\"n\">l</span><span class=\"p\">)</span>    <span class=\"n\">output</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>    <span class=\"k\">for</span> <span class=\"n\">_</span> <span class=\"ow\">in</span> <span class=\"nf\">range</span><span class=\"p\">(</span><span class=\"n\">k</span><span class=\"p\">):</span>      <span class=\"n\">output</span><span class=\"p\">.</span><span class=\"nf\">append</span><span class=\"p\">(</span><span class=\"n\">heapq</span><span class=\"p\">.</span><span class=\"nf\">heappop</span><span class=\"p\">(</span><span class=\"n\">l</span><span class=\"p\">)[</span><span class=\"mi\">1</span><span class=\"p\">])</span>    <span class=\"k\">return</span> <span class=\"n\">output</span><span class=\"k\">if</span> <span class=\"n\">__name__</span> <span class=\"o\">==</span> <span class=\"sh\">'</span><span class=\"s\">__main__</span><span class=\"sh\">'</span><span class=\"p\">:</span>  <span class=\"n\">s</span> <span class=\"o\">=</span> <span class=\"nc\">Solution</span><span class=\"p\">().</span><span class=\"n\">topKFrequent</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">([</span><span class=\"mi\">1</span><span class=\"p\">,</span><span class=\"mi\">1</span><span class=\"p\">,</span><span class=\"mi\">1</span><span class=\"p\">,</span><span class=\"mi\">2</span><span class=\"p\">,</span><span class=\"mi\">2</span><span class=\"p\">,</span><span class=\"mi\">3</span><span class=\"p\">],</span> <span class=\"mi\">2</span><span class=\"p\">),</span> <span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">,</span><span class=\"mi\">2</span><span class=\"p\">])</span></code></pre></figure><blockquote>  <p>Runtime: 227 ms, faster than 14.08% of Python3 online submissions for Top K Frequent Elements. Memory Usage: 18.7 MB, less than 71.72% of Python3 online submissions for Top K Frequent Elements.</p></blockquote>",
      "date_published": "2022-10-24T20:00:00+00:00"
    },
    {
      "id": "https://fewald.net/leetcode/2022/10/23/leetcode-find-peak-element.html",
      "url": "https://fewald.net/leetcode/2022/10/23/leetcode-find-peak-element.html",
      "title": "Leetcode: Find peak element",
      "content_html": "<p>Given an array <code class=\"language-plaintext highlighter-rouge\">n</code> of integers, find one peak in <code class=\"language-plaintext highlighter-rouge\">O(log*n)</code> time. A peak is defined as a number where the two numbers immediately left and right are strictly less than the number. Numbers outside of the bounds of this array are considered to be smaller.</p><!--more--><p>We can leverage the binary search algorithm to find a peak. This is because we can split the array in two parts and look in the middle. If left and right are smaller, it is a peak, otherwise we move in the direction of the higher element. This allows us to find the peak in <code class=\"language-plaintext highlighter-rouge\">log*n</code> steps.</p><figure class=\"highlight\"><pre><code class=\"language-python\" data-lang=\"python\"><span class=\"kn\">from</span> <span class=\"n\">typing</span> <span class=\"kn\">import</span> <span class=\"n\">List</span><span class=\"k\">class</span> <span class=\"nc\">Solution</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">findPeakElement</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">nums</span><span class=\"p\">:</span> <span class=\"n\">List</span><span class=\"p\">[</span><span class=\"nb\">int</span><span class=\"p\">])</span> <span class=\"o\">-&gt;</span> <span class=\"nb\">int</span><span class=\"p\">:</span>    <span class=\"n\">l</span> <span class=\"o\">=</span> <span class=\"mi\">0</span>    <span class=\"n\">r</span> <span class=\"o\">=</span> <span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">nums</span><span class=\"p\">)</span> <span class=\"o\">-</span> <span class=\"mi\">1</span>    <span class=\"n\">m</span> <span class=\"o\">=</span> <span class=\"p\">(</span><span class=\"n\">l</span><span class=\"o\">+</span><span class=\"n\">r</span><span class=\"p\">)</span><span class=\"o\">//</span><span class=\"mi\">2</span>    <span class=\"k\">while</span> <span class=\"n\">l</span> <span class=\"o\">&lt;</span> <span class=\"n\">r</span><span class=\"p\">:</span>      <span class=\"n\">peak</span> <span class=\"o\">=</span> <span class=\"n\">nums</span><span class=\"p\">[</span><span class=\"n\">m</span><span class=\"p\">]</span>      <span class=\"n\">lsm</span> <span class=\"o\">=</span> <span class=\"bp\">True</span> <span class=\"k\">if</span> <span class=\"n\">m</span> <span class=\"o\">==</span> <span class=\"mi\">0</span> <span class=\"ow\">or</span> <span class=\"n\">nums</span><span class=\"p\">[</span><span class=\"n\">m</span><span class=\"p\">]</span> <span class=\"o\">&gt;</span> <span class=\"n\">nums</span><span class=\"p\">[</span><span class=\"n\">m</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">]</span> <span class=\"k\">else</span> <span class=\"bp\">False</span>      <span class=\"n\">rsm</span> <span class=\"o\">=</span> <span class=\"bp\">True</span> <span class=\"k\">if</span> <span class=\"n\">m</span> <span class=\"o\">==</span> <span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">nums</span><span class=\"p\">)</span><span class=\"o\">-</span><span class=\"mi\">1</span> <span class=\"ow\">or</span> <span class=\"n\">nums</span><span class=\"p\">[</span><span class=\"n\">m</span><span class=\"p\">]</span> <span class=\"o\">&gt;</span> <span class=\"n\">nums</span><span class=\"p\">[</span><span class=\"n\">m</span><span class=\"o\">+</span><span class=\"mi\">1</span><span class=\"p\">]</span> <span class=\"k\">else</span> <span class=\"bp\">False</span>      <span class=\"k\">if</span> <span class=\"n\">lsm</span> <span class=\"ow\">and</span> <span class=\"n\">rsm</span><span class=\"p\">:</span>        <span class=\"k\">return</span> <span class=\"n\">m</span>      <span class=\"k\">if</span> <span class=\"n\">lsm</span><span class=\"p\">:</span>        <span class=\"n\">l</span> <span class=\"o\">=</span> <span class=\"n\">m</span><span class=\"o\">+</span><span class=\"mi\">1</span>      <span class=\"k\">else</span><span class=\"p\">:</span>        <span class=\"n\">r</span> <span class=\"o\">=</span> <span class=\"n\">m</span>      <span class=\"n\">m</span> <span class=\"o\">=</span> <span class=\"p\">(</span><span class=\"n\">l</span><span class=\"o\">+</span><span class=\"n\">r</span><span class=\"p\">)</span><span class=\"o\">//</span><span class=\"mi\">2</span>    <span class=\"k\">return</span> <span class=\"n\">m</span>    <span class=\"k\">if</span> <span class=\"n\">__name__</span> <span class=\"o\">==</span> <span class=\"sh\">'</span><span class=\"s\">__main__</span><span class=\"sh\">'</span><span class=\"p\">:</span>  <span class=\"n\">s</span> <span class=\"o\">=</span> <span class=\"nc\">Solution</span><span class=\"p\">().</span><span class=\"n\">findPeakElement</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">([</span><span class=\"mi\">1</span><span class=\"p\">,</span><span class=\"mi\">2</span><span class=\"p\">,</span><span class=\"mi\">3</span><span class=\"p\">,</span><span class=\"mi\">1</span><span class=\"p\">]),</span> <span class=\"mi\">2</span><span class=\"p\">)</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">([</span><span class=\"mi\">5</span><span class=\"p\">,</span><span class=\"mi\">4</span><span class=\"p\">,</span><span class=\"mi\">3</span><span class=\"p\">,</span><span class=\"mi\">2</span><span class=\"p\">,</span><span class=\"mi\">1</span><span class=\"p\">,</span><span class=\"mi\">0</span><span class=\"p\">,</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">,</span><span class=\"o\">-</span><span class=\"mi\">2</span><span class=\"p\">,</span><span class=\"o\">-</span><span class=\"mi\">3</span><span class=\"p\">,</span><span class=\"o\">-</span><span class=\"mi\">4</span><span class=\"p\">]),</span> <span class=\"mi\">0</span><span class=\"p\">)</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">([</span><span class=\"mi\">0</span><span class=\"p\">,</span><span class=\"mi\">1</span><span class=\"p\">,</span><span class=\"mi\">2</span><span class=\"p\">,</span><span class=\"mi\">3</span><span class=\"p\">,</span><span class=\"mi\">4</span><span class=\"p\">,</span><span class=\"mi\">5</span><span class=\"p\">,</span><span class=\"mi\">6</span><span class=\"p\">,</span><span class=\"mi\">5</span><span class=\"p\">,</span><span class=\"mi\">4</span><span class=\"p\">,</span><span class=\"mi\">3</span><span class=\"p\">]),</span> <span class=\"mi\">6</span><span class=\"p\">)</span></code></pre></figure>",
      "date_published": "2022-10-23T20:00:00+00:00"
    },
    {
      "id": "https://fewald.net/leetcode/2022/10/22/leetcode-minimum-stack.html",
      "url": "https://fewald.net/leetcode/2022/10/22/leetcode-minimum-stack.html",
      "title": "Leetcode: Minimum Stack",
      "content_html": "<p>Create a stack implementation that is able to return the minimum element as well as <code class=\"language-plaintext highlighter-rouge\">push</code> and <code class=\"language-plaintext highlighter-rouge\">pop</code> elements, all in constant time <code class=\"language-plaintext highlighter-rouge\">O(1)</code>.</p><!--more--><p>I used the builtin <code class=\"language-plaintext highlighter-rouge\">heapq</code> <a href=\"https://docs.python.org/3/library/heapq.html\">library</a> which provides a heap implementation. This works realiably to find the minimum element. When deleting element from the stack structure we don’t know where on the heap the element is stored. Any type of scanning the heap would violate the <code class=\"language-plaintext highlighter-rouge\">O(1)</code> requirement. Instead, I chose to store the deleted elements in a <code class=\"language-plaintext highlighter-rouge\">dictionary</code>. If the min element should be returned, the tombstones are checked. If the element is marked as deleted, the next item is checked, and so on. I use a list as the actual stack because <code class=\"language-plaintext highlighter-rouge\">append</code> and <code class=\"language-plaintext highlighter-rouge\">pop</code> perform both in <code class=\"language-plaintext highlighter-rouge\">O(1)</code>.</p><figure class=\"highlight\"><pre><code class=\"language-python\" data-lang=\"python\"><span class=\"kn\">import</span> <span class=\"n\">heapq</span><span class=\"k\">class</span> <span class=\"nc\">MinStack</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">__init__</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">):</span>    <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">_s</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>    <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">_h</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>    <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">_popped</span> <span class=\"o\">=</span> <span class=\"p\">{}</span>  <span class=\"k\">def</span> <span class=\"nf\">push</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">val</span><span class=\"p\">:</span> <span class=\"nb\">int</span><span class=\"p\">)</span> <span class=\"o\">-&gt;</span> <span class=\"bp\">None</span><span class=\"p\">:</span>    <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">_s</span><span class=\"p\">.</span><span class=\"nf\">append</span><span class=\"p\">(</span><span class=\"n\">val</span><span class=\"p\">)</span>    <span class=\"n\">heapq</span><span class=\"p\">.</span><span class=\"nf\">heappush</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">_h</span><span class=\"p\">,</span> <span class=\"n\">val</span><span class=\"p\">)</span>  <span class=\"k\">def</span> <span class=\"nf\">pop</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">)</span> <span class=\"o\">-&gt;</span> <span class=\"bp\">None</span><span class=\"p\">:</span>    <span class=\"n\">item</span> <span class=\"o\">=</span> <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">_s</span><span class=\"p\">.</span><span class=\"nf\">pop</span><span class=\"p\">()</span>        <span class=\"k\">if</span> <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">_h</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span> <span class=\"o\">==</span> <span class=\"n\">item</span><span class=\"p\">:</span>      <span class=\"n\">heapq</span><span class=\"p\">.</span><span class=\"nf\">heappop</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">_h</span><span class=\"p\">)</span>    <span class=\"k\">else</span><span class=\"p\">:</span>      <span class=\"k\">if</span> <span class=\"n\">item</span> <span class=\"ow\">not</span> <span class=\"ow\">in</span> <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">_popped</span><span class=\"p\">:</span>          <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">_popped</span><span class=\"p\">[</span><span class=\"n\">item</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"mi\">0</span>      <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">_popped</span><span class=\"p\">[</span><span class=\"n\">item</span><span class=\"p\">]</span>  <span class=\"o\">+=</span> <span class=\"mi\">1</span>  <span class=\"k\">def</span> <span class=\"nf\">top</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">)</span> <span class=\"o\">-&gt;</span> <span class=\"nb\">int</span><span class=\"p\">:</span>    <span class=\"k\">return</span> <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">_s</span><span class=\"p\">[</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">]</span>  <span class=\"k\">def</span> <span class=\"nf\">getMin</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">)</span> <span class=\"o\">-&gt;</span> <span class=\"nb\">int</span><span class=\"p\">:</span>    <span class=\"k\">while</span> <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">_h</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span> <span class=\"ow\">in</span> <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">_popped</span><span class=\"p\">:</span>      <span class=\"n\">item</span> <span class=\"o\">=</span> <span class=\"n\">heapq</span><span class=\"p\">.</span><span class=\"nf\">heappop</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">_h</span><span class=\"p\">)</span>      <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">_popped</span><span class=\"p\">[</span><span class=\"n\">item</span><span class=\"p\">]</span> <span class=\"o\">-=</span> <span class=\"mi\">1</span>      <span class=\"k\">if</span> <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">_popped</span><span class=\"p\">[</span><span class=\"n\">item</span><span class=\"p\">]</span> <span class=\"o\">==</span> <span class=\"mi\">0</span><span class=\"p\">:</span>        <span class=\"k\">del</span> <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">_popped</span><span class=\"p\">[</span><span class=\"n\">item</span><span class=\"p\">]</span>    <span class=\"k\">return</span> <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">_h</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span></code></pre></figure><blockquote>  <p>Runtime: 157 ms, faster than 12.25% of Python3 online submissions for Min Stack. Memory Usage: 18 MB, less than 60.07% of Python3 online submissions for Min Stack.</p></blockquote>",
      "date_published": "2022-10-22T20:00:00+00:00"
    },
    {
      "id": "https://fewald.net/leetcode/2022/10/21/leetcode-populate-next-pointers.html",
      "url": "https://fewald.net/leetcode/2022/10/21/leetcode-populate-next-pointers.html",
      "title": "Leetcode: Populate next pointers in tree",
      "content_html": "<p>Given a perfect binary search tree, connect all the nodes to the node on their right</p><p>The solution is to do breadth-first-search in an iterative fashion. This puts all the nodes at the same level in a queue. By iterating over the queue we can connect each node to its successor. During this process we add the children of each node as the next entry. We repeat this process until the queue is empty.</p><!--more--><figure class=\"highlight\"><pre><code class=\"language-python\" data-lang=\"python\"><span class=\"k\">class</span> <span class=\"nc\">Node</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">__init__</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">val</span><span class=\"o\">=</span><span class=\"mi\">0</span><span class=\"p\">,</span> <span class=\"n\">left</span><span class=\"o\">=</span><span class=\"bp\">None</span><span class=\"p\">,</span> <span class=\"n\">right</span><span class=\"o\">=</span><span class=\"bp\">None</span><span class=\"p\">,</span> <span class=\"nb\">next</span><span class=\"o\">=</span><span class=\"bp\">None</span><span class=\"p\">):</span>    <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">val</span> <span class=\"o\">=</span> <span class=\"n\">val</span>    <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">left</span> <span class=\"o\">=</span> <span class=\"n\">left</span>    <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">right</span> <span class=\"o\">=</span> <span class=\"n\">right</span>    <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"nb\">next</span> <span class=\"o\">=</span> <span class=\"nb\">next</span>  <span class=\"k\">def</span> <span class=\"nf\">__repr__</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">)</span> <span class=\"o\">-&gt;</span> <span class=\"nb\">str</span><span class=\"p\">:</span>    <span class=\"k\">return</span> <span class=\"sa\">f</span><span class=\"sh\">'</span><span class=\"si\">{</span><span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">val</span><span class=\"si\">}</span><span class=\"sh\">'</span><span class=\"k\">class</span> <span class=\"nc\">Solution</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">connect</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">root</span><span class=\"p\">):</span>    <span class=\"n\">queue</span> <span class=\"o\">=</span> <span class=\"p\">[[</span><span class=\"n\">root</span><span class=\"p\">]]</span>    <span class=\"k\">while</span> <span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">queue</span><span class=\"p\">)</span> <span class=\"o\">&gt;</span> <span class=\"mi\">0</span><span class=\"p\">:</span>      <span class=\"k\">for</span> <span class=\"n\">i</span> <span class=\"ow\">in</span> <span class=\"nf\">range</span><span class=\"p\">(</span><span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">queue</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">])</span> <span class=\"o\">-</span> <span class=\"mi\">1</span><span class=\"p\">):</span>        <span class=\"n\">queue</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">][</span><span class=\"n\">i</span><span class=\"p\">].</span><span class=\"nb\">next</span> <span class=\"o\">=</span> <span class=\"n\">queue</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">][</span><span class=\"n\">i</span><span class=\"o\">+</span><span class=\"mi\">1</span><span class=\"p\">]</span>      <span class=\"n\">queue</span><span class=\"p\">.</span><span class=\"nf\">append</span><span class=\"p\">([])</span>            <span class=\"k\">for</span> <span class=\"n\">i</span> <span class=\"ow\">in</span> <span class=\"nf\">range</span><span class=\"p\">(</span><span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">queue</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">])):</span>        <span class=\"n\">item</span> <span class=\"o\">=</span> <span class=\"n\">queue</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">][</span><span class=\"n\">i</span><span class=\"p\">]</span>        <span class=\"k\">if</span> <span class=\"n\">item</span><span class=\"p\">.</span><span class=\"n\">left</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"bp\">None</span><span class=\"p\">:</span>          <span class=\"n\">queue</span><span class=\"p\">[</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">].</span><span class=\"nf\">append</span><span class=\"p\">(</span><span class=\"n\">item</span><span class=\"p\">.</span><span class=\"n\">left</span><span class=\"p\">)</span>        <span class=\"k\">if</span> <span class=\"n\">item</span><span class=\"p\">.</span><span class=\"n\">right</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"bp\">None</span><span class=\"p\">:</span>          <span class=\"n\">queue</span><span class=\"p\">[</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">].</span><span class=\"nf\">append</span><span class=\"p\">(</span><span class=\"n\">item</span><span class=\"p\">.</span><span class=\"n\">right</span><span class=\"p\">)</span>            <span class=\"k\">if</span> <span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">queue</span><span class=\"p\">[</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">]):</span>        <span class=\"n\">queue</span><span class=\"p\">.</span><span class=\"nf\">pop</span><span class=\"p\">()</span>              <span class=\"n\">queue</span> <span class=\"o\">=</span> <span class=\"n\">queue</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">:]</span>        <span class=\"k\">return</span> <span class=\"n\">root</span><span class=\"k\">if</span> <span class=\"n\">__name__</span> <span class=\"o\">==</span> <span class=\"sh\">'</span><span class=\"s\">__main__</span><span class=\"sh\">'</span><span class=\"p\">:</span>  <span class=\"n\">s</span> <span class=\"o\">=</span> <span class=\"nc\">Solution</span><span class=\"p\">().</span><span class=\"n\">connect</span>  <span class=\"n\">r</span> <span class=\"o\">=</span> <span class=\"nc\">Node</span><span class=\"p\">(</span><span class=\"mi\">1</span><span class=\"p\">,</span> <span class=\"nc\">Node</span><span class=\"p\">(</span><span class=\"mi\">2</span><span class=\"p\">,</span> <span class=\"nc\">Node</span><span class=\"p\">(</span><span class=\"mi\">4</span><span class=\"p\">),</span> <span class=\"nc\">Node</span><span class=\"p\">(</span><span class=\"mi\">5</span><span class=\"p\">)),</span> <span class=\"nc\">Node</span><span class=\"p\">(</span><span class=\"mi\">3</span><span class=\"p\">,</span> <span class=\"nc\">Node</span><span class=\"p\">(</span><span class=\"mi\">6</span><span class=\"p\">),</span> <span class=\"nc\">Node</span><span class=\"p\">(</span><span class=\"mi\">7</span><span class=\"p\">)))</span>  <span class=\"nf\">s</span><span class=\"p\">(</span><span class=\"n\">r</span><span class=\"p\">)</span></code></pre></figure><blockquote>  <p>Runtime: 84 ms, faster than 70.50% of Python3 online submissions for Populating Next Right Pointers in Each Node. Memory Usage: 15.8 MB, less than 49.85% of Python3 online submissions for Populating Next Right Pointers in Each Node.</p></blockquote>",
      "date_published": "2022-10-21T20:00:00+00:00"
    },
    {
      "id": "https://fewald.net/leetcode/2022/10/20/leetcode-generate-parentheses.html",
      "url": "https://fewald.net/leetcode/2022/10/20/leetcode-generate-parentheses.html",
      "title": "Leetcode: Generate parentheses",
      "content_html": "<p>Given a strictly positive integer <code class=\"language-plaintext highlighter-rouge\">n</code>, write a function that returns all possible combinations of well-formed parentheses.</p><p>Parentheses can be nested and added one after the other. It is important that we don’t create invalid combinations, such as <code class=\"language-plaintext highlighter-rouge\">)(</code>. The idea then becomes to start with a single set of parentheses <code class=\"language-plaintext highlighter-rouge\">()</code>. We can add another set of parentheses at three possible places: <code class=\"language-plaintext highlighter-rouge\">1(2)3</code>. When looking closely, we see that <code class=\"language-plaintext highlighter-rouge\">1</code> and <code class=\"language-plaintext highlighter-rouge\">3</code> are the same position.</p><p>We can then utilize Pythons string splitting capabilities which allow us to insert one or more characters at any place in the string. We do this by iterating over the string and inserting <code class=\"language-plaintext highlighter-rouge\">()</code> at every possible position. This creates all valid pairs like <code class=\"language-plaintext highlighter-rouge\">(())</code> and <code class=\"language-plaintext highlighter-rouge\">()()</code> etc.</p><p>To avoid the aforementioned duplicates we can add a memory to the function and store all the visited possible combinations. This allows us to speed the process up significantly. For example when we visit <code class=\"language-plaintext highlighter-rouge\">()()</code>, we don’t need to visit it again to form <code class=\"language-plaintext highlighter-rouge\">()()()</code> or <code class=\"language-plaintext highlighter-rouge\">()(())</code> (for <code class=\"language-plaintext highlighter-rouge\">n=3</code>) because they would already been visited.</p><!--more--><figure class=\"highlight\"><pre><code class=\"language-python\" data-lang=\"python\"><span class=\"kn\">from</span> <span class=\"n\">typing</span> <span class=\"kn\">import</span> <span class=\"n\">List</span><span class=\"k\">class</span> <span class=\"nc\">Solution</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">generateParenthesis</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">n</span><span class=\"p\">:</span> <span class=\"nb\">int</span><span class=\"p\">)</span> <span class=\"o\">-&gt;</span> <span class=\"n\">List</span><span class=\"p\">[</span><span class=\"nb\">str</span><span class=\"p\">]:</span>    <span class=\"k\">if</span> <span class=\"n\">n</span> <span class=\"o\">==</span> <span class=\"mi\">0</span><span class=\"p\">:</span>      <span class=\"k\">return</span> <span class=\"p\">[]</span>    <span class=\"n\">memo</span> <span class=\"o\">=</span> <span class=\"nf\">set</span><span class=\"p\">()</span>        <span class=\"k\">def</span> <span class=\"nf\">inner</span><span class=\"p\">(</span><span class=\"n\">n</span><span class=\"p\">,</span> <span class=\"n\">s</span><span class=\"p\">):</span>      <span class=\"k\">if</span> <span class=\"n\">n</span> <span class=\"o\">==</span> <span class=\"mi\">0</span><span class=\"p\">:</span>        <span class=\"k\">return</span> <span class=\"p\">[</span><span class=\"n\">s</span><span class=\"p\">]</span>      <span class=\"n\">res</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>      <span class=\"k\">for</span> <span class=\"n\">p</span> <span class=\"ow\">in</span> <span class=\"nf\">range</span><span class=\"p\">(</span><span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">s</span><span class=\"p\">)):</span>        <span class=\"n\">cand</span> <span class=\"o\">=</span> <span class=\"n\">s</span><span class=\"p\">[:</span><span class=\"n\">p</span><span class=\"p\">]</span> <span class=\"o\">+</span> <span class=\"sh\">'</span><span class=\"s\">()</span><span class=\"sh\">'</span> <span class=\"o\">+</span> <span class=\"n\">s</span><span class=\"p\">[</span><span class=\"n\">p</span><span class=\"p\">:]</span>        <span class=\"k\">if</span> <span class=\"n\">cand</span> <span class=\"ow\">in</span> <span class=\"n\">memo</span><span class=\"p\">:</span>          <span class=\"k\">continue</span>        <span class=\"n\">memo</span><span class=\"p\">.</span><span class=\"nf\">add</span><span class=\"p\">(</span><span class=\"n\">cand</span><span class=\"p\">)</span>        <span class=\"n\">res</span> <span class=\"o\">+=</span> <span class=\"nf\">inner</span><span class=\"p\">(</span><span class=\"n\">n</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">,</span> <span class=\"n\">cand</span><span class=\"p\">)</span>              <span class=\"k\">return</span> <span class=\"n\">res</span>    <span class=\"n\">res</span> <span class=\"o\">=</span> <span class=\"nf\">inner</span><span class=\"p\">(</span><span class=\"n\">n</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">,</span> <span class=\"sh\">'</span><span class=\"s\">()</span><span class=\"sh\">'</span><span class=\"p\">)</span>    <span class=\"k\">return</span> <span class=\"n\">res</span>    <span class=\"k\">if</span> <span class=\"n\">__name__</span> <span class=\"o\">==</span> <span class=\"sh\">'</span><span class=\"s\">__main__</span><span class=\"sh\">'</span><span class=\"p\">:</span>  <span class=\"n\">s</span> <span class=\"o\">=</span> <span class=\"nc\">Solution</span><span class=\"p\">().</span><span class=\"n\">generateParenthesis</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">(</span><span class=\"mi\">0</span><span class=\"p\">),</span> <span class=\"p\">[])</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">(</span><span class=\"mi\">1</span><span class=\"p\">),</span> <span class=\"p\">[</span><span class=\"sh\">'</span><span class=\"s\">()</span><span class=\"sh\">'</span><span class=\"p\">])</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">(</span><span class=\"mi\">2</span><span class=\"p\">),</span> <span class=\"p\">[</span><span class=\"sh\">'</span><span class=\"s\">()()</span><span class=\"sh\">'</span><span class=\"p\">,</span> <span class=\"sh\">'</span><span class=\"s\">(())</span><span class=\"sh\">'</span><span class=\"p\">])</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">(</span><span class=\"mi\">3</span><span class=\"p\">),</span> <span class=\"p\">[</span><span class=\"sh\">'</span><span class=\"s\">((()))</span><span class=\"sh\">'</span><span class=\"p\">,</span> <span class=\"sh\">'</span><span class=\"s\">(()())</span><span class=\"sh\">'</span><span class=\"p\">,</span> <span class=\"sh\">'</span><span class=\"s\">(())()</span><span class=\"sh\">'</span><span class=\"p\">,</span> <span class=\"sh\">'</span><span class=\"s\">()(())</span><span class=\"sh\">'</span><span class=\"p\">,</span> <span class=\"sh\">'</span><span class=\"s\">()()()</span><span class=\"sh\">'</span><span class=\"p\">])</span></code></pre></figure><p>This solution beats 98% of all submitted solutions in terms of speed.</p>",
      "date_published": "2022-10-20T20:00:00+00:00"
    },
    {
      "id": "https://fewald.net/leetcode/2022/10/19/leetcode-word-break.html",
      "url": "https://fewald.net/leetcode/2022/10/19/leetcode-word-break.html",
      "title": "Leetcode: Word break",
      "content_html": "<p>Given a string <code class=\"language-plaintext highlighter-rouge\">s</code> and a list of <code class=\"language-plaintext highlighter-rouge\">words</code>, return <code class=\"language-plaintext highlighter-rouge\">True</code> if the string can be constructed from any combination of the words and <code class=\"language-plaintext highlighter-rouge\">False</code> otherwise. The alphabet contains only lowercase English characters.</p><p>My initial idea was to replace all occurrences of a <code class=\"language-plaintext highlighter-rouge\">word</code> in the string <code class=\"language-plaintext highlighter-rouge\">s</code>. The problem with this approach is that a string <code class=\"language-plaintext highlighter-rouge\">aabb</code> with the words <code class=\"language-plaintext highlighter-rouge\">['ab']</code> is considered valid, while it is not. I then tried on adding breaking characters (<code class=\"language-plaintext highlighter-rouge\">.</code>) to prevent this. It worked although very slowly.</p><!--more--><p>My next approach was to start only from the beginning and remove the current <code class=\"language-plaintext highlighter-rouge\">word</code> from <code class=\"language-plaintext highlighter-rouge\">s</code> if it was the prefix. The advantage is that this alrogrithm is relatively straightforward. The disadvantage is that it is relatively time-intensive. The same words will be checked over and over again.</p><figure class=\"highlight\"><pre><code class=\"language-python\" data-lang=\"python\"><span class=\"k\">class</span> <span class=\"nc\">Solution</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">wordBreak</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">s</span><span class=\"p\">:</span> <span class=\"nb\">str</span><span class=\"p\">,</span> <span class=\"n\">wordDict</span><span class=\"p\">:</span> <span class=\"n\">List</span><span class=\"p\">[</span><span class=\"nb\">str</span><span class=\"p\">])</span> <span class=\"o\">-&gt;</span> <span class=\"nb\">bool</span><span class=\"p\">:</span>    <span class=\"k\">if</span> <span class=\"n\">s</span> <span class=\"o\">==</span> <span class=\"sh\">\"\"</span><span class=\"p\">:</span>      <span class=\"k\">return</span> <span class=\"bp\">True</span>        <span class=\"n\">res</span> <span class=\"o\">=</span> <span class=\"bp\">False</span>    <span class=\"k\">for</span> <span class=\"n\">i</span><span class=\"p\">,</span> <span class=\"n\">word</span> <span class=\"ow\">in</span> <span class=\"nf\">enumerate</span><span class=\"p\">(</span><span class=\"n\">wordDict</span><span class=\"p\">):</span>      <span class=\"k\">if</span> <span class=\"n\">s</span><span class=\"p\">.</span><span class=\"nf\">startswith</span><span class=\"p\">(</span><span class=\"n\">word</span><span class=\"p\">):</span>        <span class=\"n\">res</span> <span class=\"o\">=</span> <span class=\"n\">res</span> <span class=\"ow\">or</span> <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"nf\">wordBreak</span><span class=\"p\">(</span><span class=\"n\">s</span><span class=\"p\">[</span><span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">word</span><span class=\"p\">):],</span> <span class=\"n\">wordDict</span><span class=\"p\">)</span>        <span class=\"k\">return</span> <span class=\"n\">res</span></code></pre></figure><p>Adding memoization speeds up the solution by quite a bit. Here, we initialize a <code class=\"language-plaintext highlighter-rouge\">map</code> or <code class=\"language-plaintext highlighter-rouge\">dictionary</code>, that contains all previously seen and invalid or valid substrings. Before going into the recursion, we check whether the word was already seen and if it was, we return the previously computed value.</p><figure class=\"highlight\"><pre><code class=\"language-python\" data-lang=\"python\"><span class=\"kn\">from</span> <span class=\"n\">typing</span> <span class=\"kn\">import</span> <span class=\"n\">List</span><span class=\"k\">class</span> <span class=\"nc\">Solution</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">wordBreak</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">s</span><span class=\"p\">:</span> <span class=\"nb\">str</span><span class=\"p\">,</span> <span class=\"n\">wordDict</span><span class=\"p\">:</span> <span class=\"n\">List</span><span class=\"p\">[</span><span class=\"nb\">str</span><span class=\"p\">])</span> <span class=\"o\">-&gt;</span> <span class=\"nb\">bool</span><span class=\"p\">:</span>    <span class=\"n\">memo</span> <span class=\"o\">=</span> <span class=\"p\">{}</span>    <span class=\"k\">def</span> <span class=\"nf\">inner</span><span class=\"p\">(</span><span class=\"n\">s</span><span class=\"p\">,</span> <span class=\"n\">wordDict</span><span class=\"p\">):</span>      <span class=\"k\">if</span> <span class=\"n\">s</span> <span class=\"o\">==</span> <span class=\"sh\">\"\"</span><span class=\"p\">:</span>        <span class=\"k\">return</span> <span class=\"bp\">True</span>      <span class=\"k\">if</span> <span class=\"n\">s</span> <span class=\"ow\">in</span> <span class=\"n\">memo</span><span class=\"p\">:</span>        <span class=\"k\">return</span> <span class=\"n\">memo</span><span class=\"p\">[</span><span class=\"n\">s</span><span class=\"p\">]</span>            <span class=\"k\">for</span> <span class=\"n\">word</span> <span class=\"ow\">in</span> <span class=\"n\">wordDict</span><span class=\"p\">:</span>        <span class=\"k\">if</span> <span class=\"n\">s</span><span class=\"p\">.</span><span class=\"nf\">startswith</span><span class=\"p\">(</span><span class=\"n\">word</span><span class=\"p\">):</span>          <span class=\"k\">if</span> <span class=\"nf\">inner</span><span class=\"p\">(</span><span class=\"n\">s</span><span class=\"p\">[</span><span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">word</span><span class=\"p\">):],</span> <span class=\"n\">wordDict</span><span class=\"p\">):</span>            <span class=\"n\">memo</span><span class=\"p\">[</span><span class=\"n\">s</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"bp\">True</span>            <span class=\"k\">return</span> <span class=\"bp\">True</span>      <span class=\"n\">memo</span><span class=\"p\">[</span><span class=\"n\">s</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"bp\">False</span>      <span class=\"k\">return</span> <span class=\"bp\">False</span>    <span class=\"k\">return</span> <span class=\"nf\">inner</span><span class=\"p\">(</span><span class=\"n\">s</span><span class=\"p\">,</span> <span class=\"n\">wordDict</span><span class=\"p\">)</span><span class=\"k\">if</span> <span class=\"n\">__name__</span> <span class=\"o\">==</span> <span class=\"sh\">'</span><span class=\"s\">__main__</span><span class=\"sh\">'</span><span class=\"p\">:</span>  <span class=\"n\">s</span> <span class=\"o\">=</span> <span class=\"nc\">Solution</span><span class=\"p\">().</span><span class=\"n\">wordBreak</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">(</span><span class=\"sh\">'</span><span class=\"s\">leetcode</span><span class=\"sh\">'</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"sh\">\"</span><span class=\"s\">leet</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">code</span><span class=\"sh\">\"</span><span class=\"p\">]),</span> <span class=\"bp\">True</span><span class=\"p\">)</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">(</span><span class=\"sh\">'</span><span class=\"s\">applepenapple</span><span class=\"sh\">'</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"sh\">\"</span><span class=\"s\">apple</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">pen</span><span class=\"sh\">\"</span><span class=\"p\">]),</span> <span class=\"bp\">True</span><span class=\"p\">)</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">(</span><span class=\"sh\">'</span><span class=\"s\">catsandog</span><span class=\"sh\">'</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"sh\">\"</span><span class=\"s\">cats</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">dog</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">sand</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">and</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">cat</span><span class=\"sh\">\"</span><span class=\"p\">]),</span> <span class=\"bp\">False</span><span class=\"p\">)</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">(</span><span class=\"sh\">'</span><span class=\"s\">ccbb</span><span class=\"sh\">'</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"sh\">'</span><span class=\"s\">cb</span><span class=\"sh\">'</span><span class=\"p\">]),</span> <span class=\"bp\">False</span><span class=\"p\">)</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">(</span><span class=\"sh\">'</span><span class=\"s\">cbca</span><span class=\"sh\">'</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"sh\">'</span><span class=\"s\">bc</span><span class=\"sh\">'</span><span class=\"p\">,</span><span class=\"sh\">'</span><span class=\"s\">ca</span><span class=\"sh\">'</span><span class=\"p\">]),</span> <span class=\"bp\">False</span><span class=\"p\">)</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">(</span><span class=\"sh\">\"</span><span class=\"s\">ddadddbdddadd</span><span class=\"sh\">\"</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"sh\">\"</span><span class=\"s\">dd</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">ad</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">da</span><span class=\"sh\">\"</span><span class=\"p\">,</span><span class=\"sh\">\"</span><span class=\"s\">b</span><span class=\"sh\">\"</span><span class=\"p\">]),</span> <span class=\"bp\">True</span><span class=\"p\">)</span></code></pre></figure><blockquote>  <p>Runtime: 75 ms, faster than 25.22% of Python3 online submissions for Word Break. Memory Usage: 14.2 MB, less than 14.55% of Python3 online submissions for Word Break.</p></blockquote><p>A good explanation on dynamic programming and memoization can be found <a href=\"https://www.freecodecamp.org/news/follow-these-steps-to-solve-any-dynamic-programming-interview-problem-cc98e508cd0e/\">here</a>.</p>",
      "date_published": "2022-10-19T20:00:00+00:00"
    },
    {
      "id": "https://fewald.net/leetcode/2022/10/18/leetcode-maximum-product-subarray.html",
      "url": "https://fewald.net/leetcode/2022/10/18/leetcode-maximum-product-subarray.html",
      "title": "Leetcode: Maximum product subarray",
      "content_html": "<p>Given an array <code class=\"language-plaintext highlighter-rouge\">nums</code> with integer values <code class=\"language-plaintext highlighter-rouge\">-10 &lt;= x &lt;= 10</code>, calculate the maximum consecutive product. For example, the array <code class=\"language-plaintext highlighter-rouge\">[2,3,-1,4]</code> produces the maximum product <code class=\"language-plaintext highlighter-rouge\">6</code> because <code class=\"language-plaintext highlighter-rouge\">2*3=6</code>.</p><p>The first obsveration that we can make is that whenever a <code class=\"language-plaintext highlighter-rouge\">0</code> is encountered, it sets the whole product to <code class=\"language-plaintext highlighter-rouge\">0</code>. This means, if a <code class=\"language-plaintext highlighter-rouge\">0</code> is somewhere in the middle of the array, we need to look at the left and right part individually because they cannot be connected.</p><p>Secondly, an odd amount of negative numbers makes the whole product negative. Having <code class=\"language-plaintext highlighter-rouge\">2</code>, <code class=\"language-plaintext highlighter-rouge\">4,</code>, <code class=\"language-plaintext highlighter-rouge\">6</code>, … negative numbers will keep the product at the same amount (if all of them are <code class=\"language-plaintext highlighter-rouge\">-1</code>) or increase it.</p><p>Finally, since those numbers are all integers, the longer the chain of multiplied numbers, the higher to outcome.</p><!--more--><p>The algorithm that I came up with iterates twice over the whole array and has a time complexity of <code class=\"language-plaintext highlighter-rouge\">O(n)</code> and constant space complexity <code class=\"language-plaintext highlighter-rouge\">O(1)</code>. Given the following example <code class=\"language-plaintext highlighter-rouge\">[2,-2,3,-2,0,9]</code> we can define two pointers, <code class=\"language-plaintext highlighter-rouge\">i</code> and <code class=\"language-plaintext highlighter-rouge\">j</code> and two helper variables, <code class=\"language-plaintext highlighter-rouge\">max_product</code> and <code class=\"language-plaintext highlighter-rouge\">current_product</code>. We start at position <code class=\"language-plaintext highlighter-rouge\">0,0</code>, then move the <code class=\"language-plaintext highlighter-rouge\">j</code> pointer to the right and multiply the numbers that we encounter until we hit a <code class=\"language-plaintext highlighter-rouge\">0</code>. This way we slowly increment our product. At each step we compare the current product with the overal maximum. Once we reach the end or <code class=\"language-plaintext highlighter-rouge\">0</code>, we move <code class=\"language-plaintext highlighter-rouge\">i</code> to the right and divide at each step until we reach <code class=\"language-plaintext highlighter-rouge\">j</code>. If we reached a <code class=\"language-plaintext highlighter-rouge\">0</code> we add this as possible maximum and then jump with <code class=\"language-plaintext highlighter-rouge\">i</code> and <code class=\"language-plaintext highlighter-rouge\">j</code> over the <code class=\"language-plaintext highlighter-rouge\">0</code>, starting the above process again.</p><figure class=\"highlight\"><pre><code class=\"language-plain\" data-lang=\"plain\">[2,-2,3,-2,0,9] cur_prod=2, max_prod=2 ^ i[2,-2,3,-2,0,9] cur_prod=-4, max_prod=2 ^  ^ i  j[2,-2,3,-2,0,9] cur_prod=-12, max_prod=2 ^    ^ i    j[2,-2,3,-2,0,9] cur_prod=24, max_prod=24 ^       ^ i       j[2,-2,3,-2,0,9] cur_prod=12, max_prod=24    ^    ^    i    j[2,-2,3,-2,0,9] cur_prod=-6, max_prod=24      ^  ^      i  j[2,-2,3,-2,0,9] cur_prod=-2, max_prod=24        ^^        ij[2,-2,3,-2,0,9] cur_prod=9, max_prod=24             ^             i  j</code></pre></figure><p>The Python code handles some more corner cases, for example if there’s only one element, and looks like this:</p><figure class=\"highlight\"><pre><code class=\"language-python\" data-lang=\"python\"><span class=\"kn\">from</span> <span class=\"n\">typing</span> <span class=\"kn\">import</span> <span class=\"n\">List</span><span class=\"k\">class</span> <span class=\"nc\">Solution</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">maxProduct</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">nums</span><span class=\"p\">:</span> <span class=\"n\">List</span><span class=\"p\">[</span><span class=\"nb\">int</span><span class=\"p\">])</span> <span class=\"o\">-&gt;</span> <span class=\"nb\">int</span><span class=\"p\">:</span>    <span class=\"n\">i</span><span class=\"p\">,</span><span class=\"n\">j</span> <span class=\"o\">=</span> <span class=\"mi\">0</span><span class=\"p\">,</span><span class=\"mi\">0</span>    <span class=\"n\">cur_prod</span> <span class=\"o\">=</span> <span class=\"n\">nums</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span>    <span class=\"n\">max_prod</span> <span class=\"o\">=</span> <span class=\"n\">nums</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span>        <span class=\"k\">while</span> <span class=\"n\">j</span> <span class=\"o\">&lt;</span> <span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">nums</span><span class=\"p\">):</span>      <span class=\"n\">cur_prod</span> <span class=\"o\">=</span> <span class=\"n\">nums</span><span class=\"p\">[</span><span class=\"n\">i</span><span class=\"p\">]</span>      <span class=\"n\">max_prod</span> <span class=\"o\">=</span> <span class=\"nf\">max</span><span class=\"p\">(</span><span class=\"n\">max_prod</span><span class=\"p\">,</span> <span class=\"n\">cur_prod</span><span class=\"p\">)</span>      <span class=\"k\">if</span> <span class=\"n\">cur_prod</span> <span class=\"o\">==</span> <span class=\"mi\">0</span><span class=\"p\">:</span>        <span class=\"n\">i</span> <span class=\"o\">+=</span> <span class=\"mi\">1</span>      <span class=\"n\">j</span> <span class=\"o\">+=</span> <span class=\"mi\">1</span>      <span class=\"k\">while</span> <span class=\"n\">i</span> <span class=\"o\">&lt;</span> <span class=\"n\">j</span><span class=\"p\">:</span>        <span class=\"k\">if</span> <span class=\"n\">j</span> <span class=\"o\">&lt;</span> <span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">nums</span><span class=\"p\">)</span> <span class=\"ow\">and</span> <span class=\"n\">nums</span><span class=\"p\">[</span><span class=\"n\">j</span><span class=\"p\">]</span> <span class=\"o\">!=</span> <span class=\"mi\">0</span><span class=\"p\">:</span>          <span class=\"n\">cur_prod</span> <span class=\"o\">*=</span> <span class=\"n\">nums</span><span class=\"p\">[</span><span class=\"n\">j</span><span class=\"p\">]</span>          <span class=\"n\">j</span><span class=\"o\">+=</span><span class=\"mi\">1</span>        <span class=\"k\">else</span><span class=\"p\">:</span>          <span class=\"k\">if</span> <span class=\"n\">i</span> <span class=\"o\">+</span> <span class=\"mi\">1</span> <span class=\"o\">&lt;</span> <span class=\"n\">j</span><span class=\"p\">:</span>            <span class=\"n\">cur_prod</span> <span class=\"o\">//=</span> <span class=\"n\">nums</span><span class=\"p\">[</span><span class=\"n\">i</span><span class=\"p\">]</span>          <span class=\"n\">i</span> <span class=\"o\">+=</span> <span class=\"mi\">1</span>        <span class=\"n\">max_prod</span> <span class=\"o\">=</span> <span class=\"nf\">max</span><span class=\"p\">(</span><span class=\"n\">max_prod</span><span class=\"p\">,</span> <span class=\"n\">cur_prod</span><span class=\"p\">)</span>    <span class=\"c1\"># max_prod = max(max_prod, cur_prod)</span>    <span class=\"k\">return</span> <span class=\"n\">max_prod</span>  <span class=\"k\">if</span> <span class=\"n\">__name__</span> <span class=\"o\">==</span> <span class=\"sh\">'</span><span class=\"s\">__main__</span><span class=\"sh\">'</span><span class=\"p\">:</span>  <span class=\"n\">s</span> <span class=\"o\">=</span> <span class=\"nc\">Solution</span><span class=\"p\">().</span><span class=\"n\">maxProduct</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">([</span><span class=\"mi\">2</span><span class=\"p\">,</span><span class=\"mi\">3</span><span class=\"p\">,</span><span class=\"o\">-</span><span class=\"mi\">2</span><span class=\"p\">,</span><span class=\"mi\">4</span><span class=\"p\">]),</span> <span class=\"mi\">6</span><span class=\"p\">)</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">([</span><span class=\"o\">-</span><span class=\"mi\">2</span><span class=\"p\">,</span> <span class=\"mi\">4</span><span class=\"p\">,</span> <span class=\"o\">-</span><span class=\"mi\">2</span><span class=\"p\">]),</span> <span class=\"mi\">16</span><span class=\"p\">)</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">([</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">,</span><span class=\"o\">-</span><span class=\"mi\">2</span><span class=\"p\">,</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">,</span><span class=\"mi\">2</span><span class=\"p\">,</span><span class=\"o\">-</span><span class=\"mi\">2</span><span class=\"p\">,</span> <span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">]),</span> <span class=\"mi\">8</span><span class=\"p\">)</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">([</span><span class=\"mi\">1</span><span class=\"p\">,</span><span class=\"mi\">2</span><span class=\"p\">,</span><span class=\"mi\">0</span><span class=\"p\">,</span><span class=\"mi\">9</span><span class=\"p\">]),</span> <span class=\"mi\">9</span><span class=\"p\">)</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">([</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">]),</span> <span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">)</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">([</span><span class=\"mi\">10</span><span class=\"p\">]),</span> <span class=\"mi\">10</span><span class=\"p\">)</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">([</span><span class=\"o\">-</span><span class=\"mi\">2</span><span class=\"p\">,</span><span class=\"mi\">0</span><span class=\"p\">,</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">]),</span><span class=\"mi\">0</span><span class=\"p\">)</span></code></pre></figure>",
      "date_published": "2022-10-18T20:00:00+00:00"
    },
    {
      "id": "https://fewald.net/leetcode/2022/10/17/leetcode-sort-list.html",
      "url": "https://fewald.net/leetcode/2022/10/17/leetcode-sort-list.html",
      "title": "Leetcode: Sort list",
      "content_html": "<p>Given a single linked list, sort the values in ascending order.</p><figure class=\"highlight\"><pre><code class=\"language-md\" data-lang=\"md\"><span class=\"gh\"># Example</span>Input: (4)-&gt;(3)-&gt;(1)-&gt;(2)Output: (1)-&gt;(2)-&gt;(3)-(4)</code></pre></figure><!--more--><p>The solution for the single linked list looks as follows. It has a time complexity of <code class=\"language-plaintext highlighter-rouge\">O(n*log(n))</code>. The technique here is first split the list in two lists of (almost) equal length. Then split the resulting lists again along the middle until the list contains only <code class=\"language-plaintext highlighter-rouge\">1</code> or <code class=\"language-plaintext highlighter-rouge\">0</code> elements.</p><p>This leaves only at most <code class=\"language-plaintext highlighter-rouge\">2</code> elements to compare. If the <code class=\"language-plaintext highlighter-rouge\">left</code> one is less than the <code class=\"language-plaintext highlighter-rouge\">right</code> one, attach the <code class=\"language-plaintext highlighter-rouge\">left</code> one to <code class=\"language-plaintext highlighter-rouge\">right.next</code> and vice versa. This small list is now sorted. When going up the stack, the <code class=\"language-plaintext highlighter-rouge\">left</code> and <code class=\"language-plaintext highlighter-rouge\">right</code> lists are therefore sorted as well and can be merged in a similar fashion. This ultimately results in a completely sorted list.</p><figure class=\"highlight\"><pre><code class=\"language-python\" data-lang=\"python\"><span class=\"kn\">from</span> <span class=\"n\">typing</span> <span class=\"kn\">import</span> <span class=\"n\">Optional</span><span class=\"c1\"># Definition for singly-linked list.</span><span class=\"k\">class</span> <span class=\"nc\">ListNode</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">__init__</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">val</span><span class=\"o\">=</span><span class=\"mi\">0</span><span class=\"p\">,</span> <span class=\"nb\">next</span><span class=\"o\">=</span><span class=\"bp\">None</span><span class=\"p\">):</span>    <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">val</span> <span class=\"o\">=</span> <span class=\"n\">val</span>    <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"nb\">next</span> <span class=\"o\">=</span> <span class=\"nb\">next</span>    <span class=\"k\">def</span> <span class=\"nf\">__str__</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">)</span> <span class=\"o\">-&gt;</span> <span class=\"nb\">str</span><span class=\"p\">:</span>    <span class=\"n\">s</span> <span class=\"o\">=</span> <span class=\"sa\">f</span><span class=\"sh\">\"</span><span class=\"si\">{</span><span class=\"n\">self</span><span class=\"p\">.</span><span class=\"n\">val</span><span class=\"si\">}</span><span class=\"sh\">\"</span>    <span class=\"k\">if</span> <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"nb\">next</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"bp\">None</span><span class=\"p\">:</span>      <span class=\"n\">s</span> <span class=\"o\">+=</span> <span class=\"sa\">f</span><span class=\"sh\">\"</span><span class=\"s\">, </span><span class=\"si\">{</span><span class=\"n\">self</span><span class=\"p\">.</span><span class=\"nb\">next</span><span class=\"si\">}</span><span class=\"sh\">\"</span>    <span class=\"k\">return</span> <span class=\"n\">s</span><span class=\"k\">class</span> <span class=\"nc\">Solution</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">sortList</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">head</span><span class=\"p\">:</span> <span class=\"n\">Optional</span><span class=\"p\">[</span><span class=\"n\">ListNode</span><span class=\"p\">])</span> <span class=\"o\">-&gt;</span> <span class=\"n\">Optional</span><span class=\"p\">[</span><span class=\"n\">ListNode</span><span class=\"p\">]:</span>    <span class=\"k\">return</span> <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"nf\">mergeSort</span><span class=\"p\">(</span><span class=\"n\">head</span><span class=\"p\">)</span>  <span class=\"k\">def</span> <span class=\"nf\">mergeSort</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">h</span><span class=\"p\">):</span>    <span class=\"k\">if</span> <span class=\"n\">h</span> <span class=\"ow\">is</span> <span class=\"bp\">None</span> <span class=\"ow\">or</span> <span class=\"n\">h</span><span class=\"p\">.</span><span class=\"nb\">next</span> <span class=\"ow\">is</span> <span class=\"bp\">None</span><span class=\"p\">:</span>      <span class=\"k\">return</span> <span class=\"n\">h</span>    <span class=\"n\">middle</span> <span class=\"o\">=</span> <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"nf\">findMiddle</span><span class=\"p\">(</span><span class=\"n\">h</span><span class=\"p\">)</span>    <span class=\"n\">middle_next</span> <span class=\"o\">=</span> <span class=\"n\">middle</span><span class=\"p\">.</span><span class=\"nb\">next</span>    <span class=\"n\">middle</span><span class=\"p\">.</span><span class=\"nb\">next</span> <span class=\"o\">=</span> <span class=\"bp\">None</span>    <span class=\"n\">left</span> <span class=\"o\">=</span> <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"nf\">mergeSort</span><span class=\"p\">(</span><span class=\"n\">h</span><span class=\"p\">)</span>    <span class=\"n\">right</span> <span class=\"o\">=</span> <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"nf\">mergeSort</span><span class=\"p\">(</span><span class=\"n\">middle_next</span><span class=\"p\">)</span>        <span class=\"n\">sorted_list</span> <span class=\"o\">=</span> <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"nf\">sort</span><span class=\"p\">(</span><span class=\"n\">left</span><span class=\"p\">,</span> <span class=\"n\">right</span><span class=\"p\">)</span>    <span class=\"k\">return</span> <span class=\"n\">sorted_list</span>    <span class=\"k\">def</span> <span class=\"nf\">sort</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">l</span><span class=\"p\">,</span> <span class=\"n\">r</span><span class=\"p\">):</span>    <span class=\"n\">result</span> <span class=\"o\">=</span> <span class=\"bp\">None</span>    <span class=\"k\">if</span> <span class=\"n\">l</span> <span class=\"ow\">is</span> <span class=\"bp\">None</span><span class=\"p\">:</span>      <span class=\"k\">return</span> <span class=\"n\">r</span>    <span class=\"k\">if</span> <span class=\"n\">r</span> <span class=\"ow\">is</span> <span class=\"bp\">None</span><span class=\"p\">:</span>      <span class=\"k\">return</span> <span class=\"n\">l</span>        <span class=\"k\">if</span> <span class=\"n\">l</span><span class=\"p\">.</span><span class=\"n\">val</span> <span class=\"o\">&lt;=</span> <span class=\"n\">r</span><span class=\"p\">.</span><span class=\"n\">val</span><span class=\"p\">:</span>      <span class=\"n\">result</span> <span class=\"o\">=</span> <span class=\"n\">l</span>      <span class=\"n\">result</span><span class=\"p\">.</span><span class=\"nb\">next</span> <span class=\"o\">=</span> <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"nf\">sort</span><span class=\"p\">(</span><span class=\"n\">l</span><span class=\"p\">.</span><span class=\"nb\">next</span><span class=\"p\">,</span> <span class=\"n\">r</span><span class=\"p\">)</span>    <span class=\"k\">else</span><span class=\"p\">:</span>      <span class=\"n\">result</span> <span class=\"o\">=</span> <span class=\"n\">r</span>      <span class=\"n\">result</span><span class=\"p\">.</span><span class=\"nb\">next</span> <span class=\"o\">=</span> <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"nf\">sort</span><span class=\"p\">(</span><span class=\"n\">l</span><span class=\"p\">,</span> <span class=\"n\">r</span><span class=\"p\">.</span><span class=\"nb\">next</span><span class=\"p\">)</span>    <span class=\"k\">return</span> <span class=\"n\">result</span>  <span class=\"k\">def</span> <span class=\"nf\">findMiddle</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">head</span><span class=\"p\">):</span>    <span class=\"k\">if</span> <span class=\"n\">head</span> <span class=\"ow\">is</span> <span class=\"bp\">None</span><span class=\"p\">:</span>      <span class=\"k\">return</span> <span class=\"n\">head</span>    <span class=\"n\">slow</span> <span class=\"o\">=</span> <span class=\"n\">head</span>    <span class=\"n\">fast</span> <span class=\"o\">=</span> <span class=\"n\">head</span>    <span class=\"k\">while</span> <span class=\"n\">fast</span><span class=\"p\">.</span><span class=\"nb\">next</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"bp\">None</span> <span class=\"ow\">and</span> <span class=\"n\">fast</span><span class=\"p\">.</span><span class=\"nb\">next</span><span class=\"p\">.</span><span class=\"nb\">next</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"bp\">None</span><span class=\"p\">:</span>      <span class=\"n\">slow</span> <span class=\"o\">=</span> <span class=\"n\">slow</span><span class=\"p\">.</span><span class=\"nb\">next</span>      <span class=\"n\">fast</span> <span class=\"o\">=</span> <span class=\"n\">fast</span><span class=\"p\">.</span><span class=\"nb\">next</span><span class=\"p\">.</span><span class=\"nb\">next</span>    <span class=\"k\">return</span> <span class=\"n\">slow</span><span class=\"k\">if</span> <span class=\"n\">__name__</span> <span class=\"o\">==</span> <span class=\"sh\">'</span><span class=\"s\">__main__</span><span class=\"sh\">'</span><span class=\"p\">:</span>  <span class=\"n\">s</span> <span class=\"o\">=</span> <span class=\"nc\">Solution</span><span class=\"p\">().</span><span class=\"n\">sortList</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">(</span><span class=\"nc\">ListNode</span><span class=\"p\">(</span><span class=\"mi\">4</span><span class=\"p\">,</span> <span class=\"nc\">ListNode</span><span class=\"p\">(</span><span class=\"mi\">2</span><span class=\"p\">,</span> <span class=\"nc\">ListNode</span><span class=\"p\">(</span><span class=\"mi\">1</span><span class=\"p\">,</span> <span class=\"nc\">ListNode</span><span class=\"p\">(</span><span class=\"mi\">3</span><span class=\"p\">))))),</span> <span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">,</span><span class=\"mi\">2</span><span class=\"p\">,</span><span class=\"mi\">3</span><span class=\"p\">,</span><span class=\"mi\">4</span><span class=\"p\">])</span></code></pre></figure><blockquote>  <p>Runtime: 1282 ms, faster than 22.47% of Python3 online submissions for Sort List. Memory Usage: 86.7 MB, less than 5.62% of Python3 online submissions for Sort List.</p></blockquote><p>Another option is a merge sort for a standard list in Python:</p><figure class=\"highlight\"><pre><code class=\"language-python\" data-lang=\"python\"><span class=\"k\">class</span> <span class=\"nc\">Solution</span><span class=\"p\">:</span>  <span class=\"k\">def</span> <span class=\"nf\">sortList</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">l</span><span class=\"p\">):</span>    <span class=\"k\">return</span> <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"nf\">mergeSort</span><span class=\"p\">(</span><span class=\"n\">l</span><span class=\"p\">)</span>  <span class=\"k\">def</span> <span class=\"nf\">mergeSort</span><span class=\"p\">(</span><span class=\"n\">self</span><span class=\"p\">,</span> <span class=\"n\">l</span><span class=\"p\">):</span>    <span class=\"c1\"># If the length of the list is one element or less,</span>    <span class=\"c1\"># return it as it means we reached the innermost</span>    <span class=\"c1\"># recursion.</span>    <span class=\"k\">if</span> <span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">l</span><span class=\"p\">)</span> <span class=\"o\">&lt;=</span> <span class=\"mi\">1</span><span class=\"p\">:</span>      <span class=\"k\">return</span> <span class=\"n\">l</span>    <span class=\"c1\"># Find middle element with floor integer division</span>    <span class=\"n\">middle</span> <span class=\"o\">=</span> <span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">l</span><span class=\"p\">)</span> <span class=\"o\">//</span> <span class=\"mi\">2</span>    <span class=\"c1\"># Recursively create left and right until left and right are only</span>    <span class=\"c1\"># one element long, then merge</span>    <span class=\"n\">left</span> <span class=\"o\">=</span> <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"nf\">mergeSort</span><span class=\"p\">(</span><span class=\"n\">l</span><span class=\"p\">[:</span><span class=\"n\">middle</span><span class=\"p\">])</span>    <span class=\"n\">right</span> <span class=\"o\">=</span> <span class=\"n\">self</span><span class=\"p\">.</span><span class=\"nf\">mergeSort</span><span class=\"p\">(</span><span class=\"n\">l</span><span class=\"p\">[</span><span class=\"n\">middle</span><span class=\"p\">:])</span>    <span class=\"n\">pl</span> <span class=\"o\">=</span> <span class=\"mi\">0</span>    <span class=\"n\">pr</span> <span class=\"o\">=</span> <span class=\"mi\">0</span>    <span class=\"n\">sorted_list</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>    <span class=\"k\">while</span> <span class=\"n\">pl</span> <span class=\"o\">&lt;</span> <span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">left</span><span class=\"p\">)</span> <span class=\"ow\">and</span> <span class=\"n\">pr</span> <span class=\"o\">&lt;</span> <span class=\"nf\">len</span><span class=\"p\">(</span><span class=\"n\">right</span><span class=\"p\">):</span>      <span class=\"k\">if</span> <span class=\"n\">left</span><span class=\"p\">[</span><span class=\"n\">pl</span><span class=\"p\">]</span> <span class=\"o\">&lt;=</span> <span class=\"n\">right</span><span class=\"p\">[</span><span class=\"n\">pr</span><span class=\"p\">]:</span>        <span class=\"n\">sorted_list</span><span class=\"p\">.</span><span class=\"nf\">append</span><span class=\"p\">(</span><span class=\"n\">left</span><span class=\"p\">[</span><span class=\"n\">pl</span><span class=\"p\">])</span>        <span class=\"n\">pl</span> <span class=\"o\">+=</span> <span class=\"mi\">1</span>      <span class=\"k\">else</span><span class=\"p\">:</span>        <span class=\"n\">sorted_list</span><span class=\"p\">.</span><span class=\"nf\">append</span><span class=\"p\">(</span><span class=\"n\">right</span><span class=\"p\">[</span><span class=\"n\">pr</span><span class=\"p\">])</span>        <span class=\"n\">pr</span> <span class=\"o\">+=</span> <span class=\"mi\">1</span>        <span class=\"c1\"># Add the remainders of the list</span>    <span class=\"n\">sorted_list</span> <span class=\"o\">+=</span> <span class=\"n\">left</span><span class=\"p\">[</span><span class=\"n\">pl</span><span class=\"p\">:]</span>    <span class=\"n\">sorted_list</span> <span class=\"o\">+=</span> <span class=\"n\">right</span><span class=\"p\">[</span><span class=\"n\">pr</span><span class=\"p\">:]</span>        <span class=\"k\">return</span> <span class=\"n\">sorted_list</span>  <span class=\"k\">if</span> <span class=\"n\">__name__</span> <span class=\"o\">==</span> <span class=\"sh\">'</span><span class=\"s\">__main__</span><span class=\"sh\">'</span><span class=\"p\">:</span>  <span class=\"n\">s</span> <span class=\"o\">=</span> <span class=\"nc\">Solution</span><span class=\"p\">().</span><span class=\"n\">sortList</span>  <span class=\"nf\">print</span><span class=\"p\">(</span><span class=\"nf\">s</span><span class=\"p\">([</span><span class=\"mi\">4</span><span class=\"p\">,</span><span class=\"mi\">3</span><span class=\"p\">,</span><span class=\"mi\">1</span><span class=\"p\">,</span><span class=\"mi\">2</span><span class=\"p\">]),</span> <span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">,</span><span class=\"mi\">2</span><span class=\"p\">,</span><span class=\"mi\">3</span><span class=\"p\">,</span><span class=\"mi\">4</span><span class=\"p\">])</span></code></pre></figure>",
      "date_published": "2022-10-17T20:00:00+00:00"
    },
    {
      "id": "https://fewald.net/leetcode/2022/10/16/leetcode-gas-station.html",
      "url": "https://fewald.net/leetcode/2022/10/16/leetcode-gas-station.html",
      "title": "Leetcode: Gas station",
      "content_html": "<p>G