<?xml version="1.0" encoding="utf-8" standalone="yes"?><?xml-stylesheet type="text/xsl" href="https://perrotta.dev/rss.xsl"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Bytebytego on ¬ just serendipity 🍀</title>
    <link>https://perrotta.dev/</link>
    <description>Recent content in Bytebytego on ¬ just serendipity 🍀</description>
    <generator>Hugo</generator>
    <language>en-us</language>
    <managingEditor>serendipity@perrotta.dev (Thiago Perrotta)</managingEditor>
    <webMaster>serendipity@perrotta.dev (Thiago Perrotta)</webMaster>
    <copyright>© 2013 - 2026 Thiago Perrotta ·
  a fork of [hugo ʕ•ᴥ•ʔ bear](https://github.com/janraasch/hugo-bearblog/)
</copyright>
    <lastBuildDate>Sun, 22 Feb 2026 00:18:38 +0100</lastBuildDate>
    <atom:link href="https://perrotta.dev/tags/bytebytego/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>ByteByteGo: Product Array Without Current Element
      </title>
      <link>https://perrotta.dev/2025/12/bytebytego-product-array-without-current-element/</link>
      <pubDate>Tue, 02 Dec 2025 23:12:24 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/12/bytebytego-product-array-without-current-element/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/bytebytego:-product-array-without-current-element&#34;&gt;ByteByteGo: Product Array Without Current Element&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;from functools import reduce&#xA;import operator&#xA;&#xA;def product_array_without_current_element(nums: List[int]) -&amp;gt; List[int]:&#xA;    total_no_zeroes = 1&#xA;    for num in nums:&#xA;        total_no_zeroes *= num if num != 0 else 1&#xA;&#xA;    num_zeroes = len([num for num in nums if num == 0])&#xA;&#xA;    if num_zeroes &amp;gt;= 2:&#xA;        return [0] * len(nums)&#xA;&#xA;    if num_zeroes == 0:&#xA;        return [total_no_zeroes // num for num in nums]&#xA;&#xA;    assert num_zeroes == 1&#xA;    return [0 if num != 0 else total_no_zeroes for num in nums]&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Product Array Without Current Element&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: K-Sum Subarrays
      </title>
      <link>https://perrotta.dev/2025/12/bytebytego-k-sum-subarrays/</link>
      <pubDate>Tue, 02 Dec 2025 22:56:27 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/12/bytebytego-k-sum-subarrays/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/prefix-sums/k-sum-subarrays&#34;&gt;ByteByteGo: K-Sum Subarrays&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;import itertools&#xA;&#xA;def k_sum_subarrays(nums: List[int], k: int) -&amp;gt; int:&#xA;    acc = list(itertools.accumulate(nums))&#xA;&#xA;    ans = 0&#xA;&#xA;    for i in range(len(nums)):&#xA;        for j in range(i, len(nums)):&#xA;            mysum = acc[j] - (acc[i - 1] if i != 0 else 0)&#xA;            if mysum == k:&#xA;                ans &amp;#43;= 1&#xA;&#xA;    return ans&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: K-Sum Subarrays&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Sum Between Range
      </title>
      <link>https://perrotta.dev/2025/12/bytebytego-sum-between-range/</link>
      <pubDate>Tue, 02 Dec 2025 22:50:57 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/12/bytebytego-sum-between-range/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/prefix-sums/sum-between-range&#34;&gt;ByteByteGo: Sum Between Range&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;&#xA;import itertools&#xA;&#xA;class SumBetweenRange:&#xA;    def __init__(self, nums: List[int]):&#xA;        self.acc = list(itertools.accumulate(nums))&#xA;&#xA;    def sum_range(self, i: int, j: int):&#xA;        return self.acc[j] - (self.acc[i - 1] if i != 0 else 0)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Sum Between Range&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: First and Last Occurrences of a Number
      </title>
      <link>https://perrotta.dev/2025/12/bytebytego-first-and-last-occurrences-of-a-number/</link>
      <pubDate>Tue, 02 Dec 2025 16:37:49 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/12/bytebytego-first-and-last-occurrences-of-a-number/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/binary-search/first-and-last-occurrences-of-a-number&#34;&gt;ByteByteGo: First and Last Occurrences of a Number&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;import bisect&#xA;from typing import List&#xA;&#xA;def first_and_last_occurrences_of_a_number(nums: List[int], target: int) -&amp;gt; int:&#xA;    if not nums:&#xA;        return [-1, -1]&#xA;&#xA;    left = bisect.bisect_left(nums, target)&#xA;    right = bisect.bisect_right(nums, target)&#xA;&#xA;    if (left &amp;lt; len(nums) and nums[left] == target) and (right &amp;gt; 0 and nums[right - 1] == target):&#xA;        return [left, right - 1]&#xA;&#xA;    return [-1, -1]&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: First and Last Occurrences of a Number&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Happy Number
      </title>
      <link>https://perrotta.dev/2025/12/bytebytego-happy-number/</link>
      <pubDate>Tue, 02 Dec 2025 16:32:31 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/12/bytebytego-happy-number/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/fast-and-slow-pointers/happy-number&#34;&gt;ByteByteGo: Happy Number&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;def happy_number(n: int) -&amp;gt; bool:&#xA;    seen = (set([n]))&#xA;&#xA;    def apply(n):&#xA;        ans = 0&#xA;&#xA;        while n &amp;gt; 0:&#xA;            ans &amp;#43;= (n % 10) ** 2&#xA;            n //= 10&#xA;&#xA;        return ans&#xA;&#xA;    assert apply(23) == 13&#xA;&#xA;    while n != 1:&#xA;        n = apply(n)&#xA;&#xA;        if n in seen:&#xA;            return False&#xA;        else:&#xA;            seen.add(n)&#xA;&#xA;    return True&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Happy Number&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Linked List Midpoint
      </title>
      <link>https://perrotta.dev/2025/12/bytebytego-linked-list-midpoint/</link>
      <pubDate>Tue, 02 Dec 2025 16:10:08 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/12/bytebytego-linked-list-midpoint/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/fast-and-slow-pointers/linked-list-midpoint&#34;&gt;ByteByteGo: Linked List Midpoint&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;p&gt;Slow and fast pointers.&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from ds import ListNode&#xA;&#xA;&amp;#34;&amp;#34;&amp;#34;&#xA;Definition of ListNode:&#xA;class ListNode:&#xA;    def __init__(self, val=None, next=None):&#xA;        self.val = val&#xA;        self.next = next&#xA;&amp;#34;&amp;#34;&amp;#34;&#xA;&#xA;def linked_list_midpoint(head: ListNode) -&amp;gt; ListNode:&#xA;    if not head or not head.next:&#xA;        return head&#xA;&#xA;    slow = head&#xA;    fast = head&#xA;&#xA;    while fast and fast.next:&#xA;        slow = slow.next&#xA;        fast = fast.next.next&#xA;&#xA;    return slow&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Linked List Midpoint&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Linked List Loop
      </title>
      <link>https://perrotta.dev/2025/12/bytebytego-linked-list-loop/</link>
      <pubDate>Tue, 02 Dec 2025 16:01:34 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/12/bytebytego-linked-list-loop/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/fast-and-slow-pointers/detect-linked-list-loops&#34;&gt;ByteByteGo: Linked List Loop&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;p&gt;Slow and fast pointers.&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from ds import ListNode&#xA;&#xA;&amp;#34;&amp;#34;&amp;#34;&#xA;Definition of ListNode:&#xA;class ListNode:&#xA;    def __init__(self, val=None, next=None):&#xA;        self.val = val&#xA;        self.next = next&#xA;&amp;#34;&amp;#34;&amp;#34;&#xA;&#xA;def linked_list_loop(head: ListNode) -&amp;gt; bool:&#xA;    if not head or not head.next:&#xA;        return False&#xA;&#xA;    slow = head&#xA;    fast = head.next&#xA;&#xA;    while slow != fast:&#xA;        slow = slow.next&#xA;&#xA;        if not fast or not fast.next:&#xA;            return False&#xA;&#xA;        fast = fast.next.next&#xA;&#xA;    return True&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Linked List Loop&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Remove the Kth Last Node From a Linked List
      </title>
      <link>https://perrotta.dev/2025/12/bytebytego-remove-the-kth-last-node-from-a-linked-list/</link>
      <pubDate>Tue, 02 Dec 2025 03:58:25 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/12/bytebytego-remove-the-kth-last-node-from-a-linked-list/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/linked-lists/remove-the-kth-last-node-from-a-linked-list&#34;&gt;ByteByteGo: Remove the Kth Last Node From a Linked List&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from ds import ListNode&#xA;&#xA;&amp;#34;&amp;#34;&amp;#34;&#xA;Definition of ListNode:&#xA;class ListNode:&#xA;    def __init__(self, val=None, next=None):&#xA;        self.val = val&#xA;        self.next = next&#xA;&amp;#34;&amp;#34;&amp;#34;&#xA;&#xA;def remove_kth_last_node(head: ListNode, k: int) -&amp;gt; ListNode:&#xA;    pivot = head&#xA;    back = head&#xA;&#xA;    for _ in range(k):&#xA;        if pivot:&#xA;            pivot = pivot.next&#xA;        else:&#xA;            return head&#xA;&#xA;    if not pivot:&#xA;        return head.next&#xA;&#xA;    while pivot.next:&#xA;        pivot = pivot.next&#xA;        back = back.next&#xA;&#xA;    back.next = back.next.next&#xA;&#xA;    return head&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Remove the Kth Last Node From a Linked List&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Largest Square in a Matrix
      </title>
      <link>https://perrotta.dev/2025/11/bytebytego-largest-square-in-a-matrix/</link>
      <pubDate>Sun, 30 Nov 2025 23:18:37 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/11/bytebytego-largest-square-in-a-matrix/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/dynamic-programming/largest-square-in-a-matrix&#34;&gt;ByteByteGo: Largest Square in a Matrix&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;from functools import lru_cache&#xA;&#xA;def largest_square_in_a_matrix(matrix: List[List[int]]) -&amp;gt; int:&#xA;    @lru_cache(maxsize=None)&#xA;    def solve(x: int, y: int) -&amp;gt; int:&#xA;        if x &amp;lt; 0 or y &amp;lt; 0:&#xA;            return 0&#xA;        if matrix[x][y] == 0 or x == 0 or y == 0:&#xA;            return matrix[x][y]&#xA;        return 1 &amp;#43; min(&#xA;            solve(x - 1, y),&#xA;            solve(x, y - 1),&#xA;            solve(x - 1, y - 1),&#xA;        )&#xA;&#xA;    # stores the maximum side length&#xA;    ans = 0&#xA;    for i in range(len(matrix)):&#xA;        for j in range(len(matrix[0])):&#xA;            ans = max(ans, solve(i, j))&#xA;&#xA;    # Return the area (side length squared)&#xA;    return ans * ans&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Largest Square in a Matrix&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Longest Palindrome in a String
      </title>
      <link>https://perrotta.dev/2025/11/bytebytego-longest-palindrome-in-a-string/</link>
      <pubDate>Sun, 30 Nov 2025 22:16:55 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/11/bytebytego-longest-palindrome-in-a-string/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/dynamic-programming/longest-palindrome-in-a-string&#34;&gt;ByteByteGo: Longest Palindrome in a String&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;def longest_palindrome_in_a_string(s: str) -&amp;gt; str:&#xA;    from functools import lru_cache&#xA;&#xA;    ans = &amp;#34;&amp;#34;&#xA;&#xA;    @lru_cache(maxsize=None)&#xA;    def is_palindrome(i, j):&#xA;        assert i &amp;gt;= 0&#xA;        assert j &amp;lt; len(s)&#xA;&#xA;        if i &amp;gt;= j:  # out of bounds&#xA;            return True&#xA;&#xA;        if s[i] != s[j]:&#xA;            return False&#xA;&#xA;        return is_palindrome(i &amp;#43; 1, j - 1)&#xA;&#xA;    if len(s) &amp;gt; 0:&#xA;        ans = s[0]&#xA;&#xA;    for i in range(len(s)):&#xA;        for j in range(i &amp;#43; 1, len(s)):  # both inclusive&#xA;            if is_palindrome(i, j):&#xA;                curr_len = j - i &amp;#43; 1&#xA;                if curr_len &amp;gt; len(ans):&#xA;                    ans = s[i:j &amp;#43; 1]&#xA;&#xA;    return ans&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Longest Palindrome in a String&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: N Queens
      </title>
      <link>https://perrotta.dev/2025/11/bytebytego-n-queens/</link>
      <pubDate>Sun, 30 Nov 2025 21:14:24 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/11/bytebytego-n-queens/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/backtracking/n-queens&#34;&gt;ByteByteGo: N Queens&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;def n_queens(n: int) -&amp;gt; int:&#xA;    ans = 0&#xA;&#xA;    def backtrack(r, vset: set, d1set: set, d2set: set):&#xA;        nonlocal ans&#xA;&#xA;        # r, c: row, col&#xA;&#xA;        # d1 set: r &amp;#43; c&#xA;        # d2 set: r - c (or c - r, just be consistent)&#xA;&#xA;        if r == n:&#xA;            ans &amp;#43;= 1&#xA;            return&#xA;&#xA;        for c in range(n):&#xA;            # place a queen in (r, c) position if possible&#xA;            if c not in vset and c &amp;#43; r not in d1set and c - r not in d2set:&#xA;                vset.add(c)&#xA;                d1set.add(c &amp;#43; r)&#xA;                d2set.add(c - r)&#xA;&#xA;                backtrack(r &amp;#43; 1, vset, d1set, d2set)&#xA;&#xA;                vset.remove(c)&#xA;                d1set.remove(c &amp;#43; r)&#xA;                d2set.remove(c - r)&#xA;&#xA;&#xA;    backtrack(0, set(), set(), set())&#xA;&#xA;    return ans&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: N Queens&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Josephus
      </title>
      <link>https://perrotta.dev/2025/11/bytebytego-josephus/</link>
      <pubDate>Sun, 30 Nov 2025 17:26:22 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/11/bytebytego-josephus/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/math-and-geometry/the-josephus-problem&#34;&gt;ByteByteGo: Josephus&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;p&gt;With an array:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;def josephus(n: int, k: int) -&amp;gt; int:&#xA;    circle = list(range(n))&#xA;    victim = 0&#xA;    # [0, 1, 2, 3, 4]&#xA;    #  ^&#xA;    #&#xA;    # clockwise: victim &amp;#43;= 1&#xA;    #&#xA;    # next victim: victim &amp;#43; k - 1 (remember to mod)&#xA;&#xA;    while len(circle) &amp;gt; 1:&#xA;        victim = (victim &amp;#43; (k - 1)) % len(circle)&#xA;        del circle[victim]&#xA;&#xA;    return circle[0]&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;With a linked list (&lt;code&gt;deque&lt;/code&gt;):&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from collections import deque&#xA;&#xA;def josephus(n: int, k: int) -&amp;gt; int:&#xA;    circle = deque(range(n))&#xA;    victim = 0&#xA;    # [0, 1, 2, 3, 4]&#xA;    #  ^&#xA;    #&#xA;    # clockwise: victim &amp;#43;= 1&#xA;    #&#xA;    # next victim: victim &amp;#43; k - 1 (remember to mod)&#xA;&#xA;    while len(circle) &amp;gt; 1:&#xA;        victim = (victim &amp;#43; (k - 1)) % len(circle)&#xA;        del circle[victim]&#xA;&#xA;    return circle[0]&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;With a linked list, efficient removals from the end (beginning) of the list:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from collections import deque&#xA;&#xA;def josephus(n: int, k: int) -&amp;gt; int:&#xA;    circle = deque(range(n))&#xA;&#xA;    while len(circle) &amp;gt; 1:&#xA;        # rotate left so that the k‑th person becomes the leftmost element&#xA;        # deque.rotate moves elements to the right; negative rotates left&#xA;        circle.rotate(-(k - 1))&#xA;&#xA;        # eliminate that person&#xA;        circle.popleft()&#xA;&#xA;    return circle[0]&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Josephus&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Spiral Traversal
      </title>
      <link>https://perrotta.dev/2025/11/bytebytego-spiral-traversal/</link>
      <pubDate>Sun, 30 Nov 2025 16:49:27 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/11/bytebytego-spiral-traversal/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/math-and-geometry/spiral-traversal&#34;&gt;ByteByteGo: Spiral Traversal&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;&#xA;def spiral_matrix(matrix: List[List[int]]) -&amp;gt; List[int]:&#xA;    ans = []&#xA;&#xA;    start = (0, 0)&#xA;    end = (len(matrix), len(matrix[0]))&#xA;&#xA;    def debian_once(start, end):&#xA;        for j in range(start[1], end[1]):&#xA;            ans.append(matrix[start[0]][j])&#xA;&#xA;        for i in range(start[0] &amp;#43; 1, end[0] - 1):&#xA;            ans.append(matrix[i][end[1] - 1])&#xA;&#xA;        if end[0] - 1 &amp;gt; start[0]:&#xA;            for j in range(end[1] - 1, start[1] - 1, -1):&#xA;                ans.append(matrix[end[0] - 1][j])&#xA;&#xA;        if end[1] - 1 &amp;gt; start[1]:&#xA;            for i in range(end[0] - 1 - 1, start[1], -1):&#xA;                ans.append(matrix[i][start[1]])&#xA;&#xA;    # while start[0] &amp;lt; end[0] and start[1] &amp;lt; end[1]:&#xA;    # while all(start[i] &amp;lt; end[i] for i in range(len(start))):&#xA;    while all(x &amp;lt; y for (x, y) in zip(start, end)):&#xA;        debian_once(start, end)&#xA;        start = tuple(el &amp;#43; 1 for el in start)&#xA;        end = tuple(el - 1 for el in end)&#xA;&#xA;    return ans&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Beware of not doing &lt;code&gt;&amp;lt;-&lt;/code&gt; (leftwards) and &lt;code&gt;^&lt;/code&gt; (upwards) whenever there&amp;rsquo;s a single&#xA;column or a single row!&lt;/p&gt;&#xA;&lt;p&gt;Or, changing the meaning of &lt;code&gt;end&lt;/code&gt; by shifting one cell diagonally inwards&#xA;(inclusive):&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;&#xA;def spiral_matrix(matrix: List[List[int]]) -&amp;gt; List[int]:&#xA;    ans = []&#xA;&#xA;    start = (0, 0)&#xA;    end = (len(matrix) - 1, len(matrix[0]) - 1)&#xA;&#xA;    def debian_once(start, end):&#xA;        for j in range(start[1], end[1] &amp;#43; 1):&#xA;            ans.append(matrix[start[0]][j])&#xA;&#xA;        for i in range(start[0] &amp;#43; 1, end[0]):&#xA;            ans.append(matrix[i][end[1]])&#xA;&#xA;        if start[0] &amp;lt; end[0]:&#xA;            for j in range(end[1], start[1] - 1, -1):&#xA;                ans.append(matrix[end[0]][j])&#xA;&#xA;        if start[1] &amp;lt; end[1]:&#xA;            for i in range(end[0] - 1, start[1], -1):&#xA;                ans.append(matrix[i][start[1]])&#xA;&#xA;    # while start[0] &amp;lt;= end[0] and start[1] &amp;lt;= end[1]:&#xA;    # while all(start[i] &amp;lt;= end[i] for i in range(len(start))):&#xA;    while all(x &amp;lt;= y for (x, y) in zip(start, end)):&#xA;        debian_once(start, end)&#xA;        start = tuple(el &amp;#43; 1 for el in start)&#xA;        end = tuple(el - 1 for el in end)&#xA;&#xA;    return ans&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Spiral Traversal&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Combinations of a Sum
      </title>
      <link>https://perrotta.dev/2025/11/bytebytego-combinations-of-a-sum/</link>
      <pubDate>Sun, 30 Nov 2025 14:58:14 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/11/bytebytego-combinations-of-a-sum/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/backtracking/combinations-of-a-sum&#34;&gt;ByteByteGo: Combinations of a Sum&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;p&gt;Initial solution, wrong:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;Input: nums = [1, 2, 3], target = 4&#xA;Output: [[1,1,1,1],[1,1,2],[1,2,1],[1,3],[2,1,1],[2,2],[3,1]]&lt;/code&gt;&lt;/pre&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;&#xA;def combinations_of_sum_k(nums: List[int], target: int) -&amp;gt; List[List[int]]:&#xA;    ans = []&#xA;&#xA;    def backtrack(candidate = [], target = target):&#xA;        if target &amp;lt; 0:&#xA;            return&#xA;&#xA;        if target == 0:&#xA;            ans.append(candidate[:])&#xA;            return&#xA;&#xA;        for num in nums:&#xA;            # include num&#xA;            candidate.append(num)&#xA;            backtrack(candidate, target - num)&#xA;            candidate.pop()&#xA;&#xA;            # do not include num&#xA;            continue&#xA;&#xA;    backtrack()&#xA;&#xA;    return ans&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;It is incorrect because it includes certain combinations multiple times e.g.&#xA;&lt;code&gt;[1,1,2]&lt;/code&gt;, &lt;code&gt;[1,2,1]&lt;/code&gt;, &lt;code&gt;[2,1,1]&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;p&gt;The key insight is, upon moving to the next &lt;code&gt;num&lt;/code&gt; in &lt;code&gt;nums&lt;/code&gt;, not to include the&#xA;entire &lt;code&gt;nums&lt;/code&gt; list during the backtracking, but rather only from the index&#xA;corresponding to &lt;code&gt;num&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Final solution, correct:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;Input: nums = [1, 2, 3], target = 4&#xA;Output: [[1, 1, 1, 1], [1, 1, 2], [1, 3], [2, 2]]&lt;/code&gt;&lt;/pre&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;&#xA;def combinations_of_sum_k(nums: List[int], target: int) -&amp;gt; List[List[int]]:&#xA;    ans = []&#xA;&#xA;    def backtrack(nums = nums, candidate = [], target = target):&#xA;        if target &amp;lt; 0:&#xA;            return&#xA;&#xA;        if target == 0:&#xA;            ans.append(candidate[:])&#xA;            return&#xA;&#xA;        for i, num in enumerate(nums):&#xA;            # include num&#xA;            candidate.append(num)&#xA;            backtrack(nums[i:], candidate, target - num)&#xA;            candidate.pop()&#xA;&#xA;            # do not include num&#xA;            continue&#xA;&#xA;    backtrack()&#xA;&#xA;    return ans&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Combinations of a Sum&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Find All Subsets
      </title>
      <link>https://perrotta.dev/2025/11/bytebytego-find-all-subsets/</link>
      <pubDate>Sun, 30 Nov 2025 14:42:17 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/11/bytebytego-find-all-subsets/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/backtracking/find-all-subsets&#34;&gt;ByteByteGo: Find All Subsets&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;&#xA;def find_all_subsets(nums: List[int]) -&amp;gt; List[List[int]]:&#xA;    ans = []&#xA;&#xA;    def backtrack(nums = nums, candidate = []):&#xA;        if not nums:&#xA;            ans.append(candidate[:])&#xA;            return&#xA;&#xA;        num = nums[0]&#xA;&#xA;        # or do include num&#xA;        candidate.append(num)&#xA;        backtrack(nums[1:], candidate)&#xA;        candidate.pop()&#xA;&#xA;        # either do not include num&#xA;        backtrack(nums[1:], candidate)&#xA;&#xA;&#xA;    backtrack()&#xA;&#xA;    return ans&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;It&amp;rsquo;s also possible to do it with a single recursion:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;&#xA;def find_all_subsets(nums: List[int]) -&amp;gt; List[List[int]]:&#xA;    ans = []&#xA;&#xA;    def backtrack(start, candidate):&#xA;        # Every point in the recursion represents a valid subset&#xA;        ans.append(candidate[:])&#xA;&#xA;        for i in range(start, len(nums)):&#xA;            candidate.append(nums[i])    # include nums[i]&#xA;            backtrack(i &amp;#43; 1, candidate)  # recurse on the rest&#xA;            candidate.pop()              # backtrack&#xA;&#xA;    backtrack(0, [])&#xA;&#xA;    return ans&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Find All Subsets&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Find All Permutations
      </title>
      <link>https://perrotta.dev/2025/11/bytebytego-find-all-permutations/</link>
      <pubDate>Sun, 30 Nov 2025 14:02:35 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/11/bytebytego-find-all-permutations/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/backtracking/find-all-permutations&#34;&gt;ByteByteGo: Find All Permutations&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;&#xA;def find_all_permutations(nums: List[int]) -&amp;gt; List[List[int]]:&#xA;    ans = []&#xA;&#xA;    def backtrack(candidate = [], visited = set()):&#xA;        if len(candidate) == len(nums):&#xA;            ans.append(candidate[:])&#xA;            return&#xA;&#xA;        for num in nums:&#xA;            if num not in visited:&#xA;                candidate.append(num)&#xA;                visited.add(num)&#xA;&#xA;                backtrack(candidate, visited)&#xA;&#xA;                candidate.pop()&#xA;                visited.remove(num)&#xA;&#xA;&#xA;    backtrack()&#xA;&#xA;    return ans&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;If we do not make a copy of candidate (&lt;code&gt;candidate[:]&lt;/code&gt;), &lt;code&gt;ans&lt;/code&gt; will be a mess.&lt;/p&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Find All Permutations&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Hamming Weights of Integers
      </title>
      <link>https://perrotta.dev/2025/11/bytebytego-hamming-weights-of-integers/</link>
      <pubDate>Thu, 27 Nov 2025 03:09:16 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/11/bytebytego-hamming-weights-of-integers/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/bit-manipulation/hamming-weights-of-integers&#34;&gt;ByteByteGo: Hamming Weights of Integers&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;p&gt;Did someone say something about variable scoping? Poor &lt;code&gt;n&lt;/code&gt; is overworked.&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;&#xA;def hamming_weights_of_integers(n: int) -&amp;gt; List[int]:&#xA;    def hammer(n):&#xA;        ans = 0&#xA;        while n &amp;gt; 0:&#xA;            ans &amp;#43;= n &amp;amp; 1&#xA;            n &amp;gt;&amp;gt;= 1&#xA;        return ans&#xA;&#xA;    return [hammer(n) for n in range(n &amp;#43; 1)]&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;With top-down dynamic programming, linear:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;&#xA;def hamming_weights_of_integers(n: int) -&amp;gt; List[int]:&#xA;    from functools import lru_cache&#xA;&#xA;    @lru_cache(maxsize=None)&#xA;    def hammer(n):&#xA;        if n in [0, 1]:&#xA;            return n&#xA;&#xA;        return hammer(n &amp;gt;&amp;gt; 1) &amp;#43; (n &amp;amp; 1)&#xA;&#xA;    return [hammer(n) for n in range(n &amp;#43; 1)]&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;With bottom-up dynamic programming, linear:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;&#xA;def hamming_weights_of_integers(n: int) -&amp;gt; List[int]:&#xA;    from functools import lru_cache&#xA;&#xA;    if n == 0:&#xA;        return [0]&#xA;&#xA;    hammer = [0] * (n &amp;#43; 1)&#xA;    hammer[0] = 0&#xA;    hammer[1] = 1&#xA;&#xA;    for x in range(2, n &amp;#43; 1):&#xA;        hammer[x] = hammer[x &amp;gt;&amp;gt; 1] &amp;#43; (x &amp;amp; 1)&#xA;&#xA;    return hammer&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Hamming Weights of Integers&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Swap Odd and Even Bits
      </title>
      <link>https://perrotta.dev/2025/11/bytebytego-swap-odd-and-even-bits/</link>
      <pubDate>Thu, 27 Nov 2025 03:06:15 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/11/bytebytego-swap-odd-and-even-bits/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/bit-manipulation/swap-odd-and-even-bits&#34;&gt;ByteByteGo: Swap Odd and Even Bits&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;def swap_odd_and_even_bits(n: int) -&amp;gt; int:&#xA;    ans = 0&#xA;    shift = 0&#xA;&#xA;    while n &amp;gt; 0:&#xA;        ans &amp;#43;= ((n &amp;amp; 0b01) &amp;lt;&amp;lt; 1) &amp;#43; ((n &amp;amp; 0b10) &amp;gt;&amp;gt; 1) &amp;lt;&amp;lt; shift&#xA;        n &amp;gt;&amp;gt;= 2&#xA;        shift &amp;#43;= 2&#xA;&#xA;    return ans&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Swap Odd and Even Bits&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Lonely Integer
      </title>
      <link>https://perrotta.dev/2025/11/bytebytego-lonely-integer/</link>
      <pubDate>Thu, 27 Nov 2025 02:58:33 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/11/bytebytego-lonely-integer/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/bit-manipulation/lonely-integer&#34;&gt;ByteByteGo: Lonely Integer&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;from functools import reduce&#xA;&#xA;def lonely_integer(nums: List[int]) -&amp;gt; int:&#xA;    return reduce(lambda x, y: x ^ y, nums)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;If you forget how to call &lt;code&gt;reduce&lt;/code&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;from functools import reduce&#xA;&#xA;def lonely_integer(nums: List[int]) -&amp;gt; int:&#xA;    ans = 0&#xA;    for num in nums:&#xA;        ans ^= num&#xA;    return ans&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Lonely Integer&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Maximum Subarray Sum
      </title>
      <link>https://perrotta.dev/2025/11/bytebytego-maximum-subarray-sum/</link>
      <pubDate>Thu, 27 Nov 2025 02:51:23 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/11/bytebytego-maximum-subarray-sum/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/dynamic-programming/maximum-subarray-sum&#34;&gt;ByteByteGo: Maximum Subarray Sum&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;&#xA;def maximum_subarray_sum(nums: List[int]) -&amp;gt; int:&#xA;    from functools import lru_cache&#xA;&#xA;    @lru_cache(maxsize=None)&#xA;    def solve(i, must_take=False):&#xA;        if i &amp;lt; 0:&#xA;            return 0&#xA;&#xA;        if i == 0:&#xA;            if must_take:&#xA;                return max(0, nums[i])&#xA;            else:&#xA;                return nums[i]&#xA;&#xA;        take = nums[i] &amp;#43; solve(i - 1, True)&#xA;        if must_take:&#xA;            return max(0, take)&#xA;&#xA;        skip = solve(i - 1)&#xA;        return max(take, skip)&#xA;&#xA;    return solve(len(nums) - 1)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Maximum Subarray Sum&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: 0/1 Knapsack
      </title>
      <link>https://perrotta.dev/2025/11/bytebytego-0/1-knapsack/</link>
      <pubDate>Wed, 26 Nov 2025 22:06:31 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/11/bytebytego-0/1-knapsack/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/dynamic-programming/01-knapsack&#34;&gt;ByteByteGo: 0/1 Knapsack&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;&#xA;def knapsack(k: int, weights: List[int], values: List[int]) -&amp;gt; int:&#xA;    from functools import lru_cache&#xA;&#xA;    assert len(weights) == len(values)&#xA;&#xA;    @lru_cache(maxsize=None)&#xA;    def solve(i, k):&#xA;        assert i &amp;gt;= 0&#xA;&#xA;        if i == 0:&#xA;            if weights[i] &amp;lt;= k:&#xA;                return values[i]&#xA;            else:&#xA;                return 0&#xA;&#xA;        if k &amp;lt;= 0:&#xA;            return 0&#xA;&#xA;        skip = solve(i - 1, k)&#xA;&#xA;        take = 0&#xA;        if weights[i] &amp;lt;= k:&#xA;            take = values[i] &amp;#43; solve(i - 1, k - weights[i])&#xA;&#xA;        return max(skip, take)&#xA;&#xA;    return solve(len(weights) - 1, k)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: 0/1 Knapsack&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Neighborhood Burglary
      </title>
      <link>https://perrotta.dev/2025/11/bytebytego-neighborhood-burglary/</link>
      <pubDate>Wed, 26 Nov 2025 21:17:39 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/11/bytebytego-neighborhood-burglary/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/dynamic-programming/neighborhood-burglary&#34;&gt;ByteByteGo: Neighborhood Burglary&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;&#xA;def neighborhood_burglary(houses: List[int]) -&amp;gt; int:&#xA;    from functools import lru_cache&#xA;&#xA;    @lru_cache(maxsize=None)&#xA;    def solve(i):&#xA;        # 0 to n - 1&#xA;        assert i &amp;gt;= 0&#xA;&#xA;        if i == 0:&#xA;            return houses[0]&#xA;&#xA;        if i == 1:&#xA;            return max(houses[0], houses[1])&#xA;&#xA;        return max(&#xA;            houses[i] &amp;#43; solve(i - 2),&#xA;            solve(i - 1),&#xA;        )&#xA;&#xA;    return solve(len(houses) - 1)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Neighborhood Burglary&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Matrix Pathways
      </title>
      <link>https://perrotta.dev/2025/11/bytebytego-matrix-pathways/</link>
      <pubDate>Wed, 26 Nov 2025 21:01:40 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/11/bytebytego-matrix-pathways/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/dynamic-programming/matrix-pathways&#34;&gt;ByteByteGo: Matrix Pathways&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;def matrix_pathways(m: int, n: int) -&amp;gt; int:&#xA;    from functools import lru_cache&#xA;&#xA;    @lru_cache(maxsize=None)&#xA;    def solve(m, n):&#xA;        # from 1 to m, from 1 to n&#xA;        assert (m, n) &amp;gt;= (0, 0)&#xA;&#xA;        if m == 0 or n == 0:&#xA;            return 0&#xA;&#xA;        if m == 1 and n == 1:&#xA;            return 1&#xA;&#xA;        return solve(m - 1, n) &amp;#43; solve(m, n - 1)&#xA;&#xA;    return solve(m, n)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Matrix Pathways&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Kth Largest Integer
      </title>
      <link>https://perrotta.dev/2025/11/bytebytego-kth-largest-integer/</link>
      <pubDate>Wed, 26 Nov 2025 18:11:54 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/11/bytebytego-kth-largest-integer/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/sort-and-search/kth-largest-integer&#34;&gt;ByteByteGo: Kth Largest Integer&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;p&gt;Sort with &lt;code&gt;reverse=True&lt;/code&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;&#xA;def kth_largest_integer(nums: List[int], k: int) -&amp;gt; int:&#xA;    s = sorted(set(nums), reverse=True)&#xA;&#xA;    if len(s) == 1:&#xA;        return list(s)[0]&#xA;&#xA;    return list(s)[k - 1 % len(nums)]&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Sort with &lt;code&gt;reversed&lt;/code&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;&#xA;def kth_largest_integer(nums: List[int], k: int) -&amp;gt; int:&#xA;    s = list(reversed(list(sorted(set(nums)))))&#xA;&#xA;    if len(s) == 1:&#xA;        return list(s)[0]&#xA;&#xA;    return list(s)[k - 1 % len(nums)]&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Forego reversion, index from the end of the list instead:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;&#xA;def kth_largest_integer(nums: List[int], k: int) -&amp;gt; int:&#xA;    s = sorted(set(nums))&#xA;&#xA;    if len(s) == 1:&#xA;        return list(s)[0]&#xA;&#xA;    return list(s)[len(nums) - k]&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Kth Largest Integer&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Reverse 32-Bit Integer
      </title>
      <link>https://perrotta.dev/2025/11/bytebytego-reverse-32-bit-integer/</link>
      <pubDate>Wed, 26 Nov 2025 15:06:17 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/11/bytebytego-reverse-32-bit-integer/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/math-and-geometry/reverse-32-bit-integer&#34;&gt;ByteByteGo: Reverse 32-Bit Integer&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;h2 id=&#34;strings&#34;&gt;&#xA;  Strings&#xA;  &lt;a class=&#34;heading-anchor&#34; href=&#34;https://perrotta.dev/2025/11/bytebytego-reverse-32-bit-integer/#strings&#34; aria-label=&#34;Link to this section&#34;&gt;#&lt;/a&gt;&#xA;&lt;/h2&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;def reverse_32_bit_integer(n: int) -&amp;gt; int:&#xA;    ans = &amp;#34;&amp;#34;&#xA;&#xA;    s = str(n)[::-1]&#xA;&#xA;    # 420&#xA;    # 024&#xA;&#xA;    # -15&#xA;    # 51-&#xA;&#xA;    is_negative = n &amp;lt; 0&#xA;    is_beginning = True&#xA;&#xA;    for i, c in enumerate(s):&#xA;        if i == (len(s) - 1) and is_negative:&#xA;            continue&#xA;&#xA;        if c == &amp;#39;0&amp;#39; and is_beginning:&#xA;            continue&#xA;&#xA;        ans &amp;#43;= c&#xA;&#xA;        is_beginning = False&#xA;&#xA;    ans = int(ans) * (-1 if is_negative else 1)&#xA;&#xA;    return ans if (-1 * 2**31 &amp;lt;= ans &amp;lt;= 2**31 - 1) else 0&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;h2 id=&#34;numbers&#34;&gt;&#xA;  Numbers&#xA;  &lt;a class=&#34;heading-anchor&#34; href=&#34;https://perrotta.dev/2025/11/bytebytego-reverse-32-bit-integer/#numbers&#34; aria-label=&#34;Link to this section&#34;&gt;#&lt;/a&gt;&#xA;&lt;/h2&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;def reverse_32_bit_integer(n: int) -&amp;gt; int:&#xA;    ans = 0&#xA;    is_negative = -1 if n &amp;lt; 0 else 1&#xA;    n = abs(n)&#xA;&#xA;    while n != 0:&#xA;        digit = n % 10&#xA;        n //= 10&#xA;&#xA;        # Check for overflow: positive&#xA;        if ans &amp;gt; (2**31 - 1) // 10:&#xA;            return 0&#xA;&#xA;        ans = ans * 10 &amp;#43; digit&#xA;&#xA;    return is_negative * ans&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Reverse 32-Bit Integer&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Dutch National Flag
      </title>
      <link>https://perrotta.dev/2025/11/bytebytego-dutch-national-flag/</link>
      <pubDate>Wed, 26 Nov 2025 14:03:26 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/11/bytebytego-dutch-national-flag/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/sort-and-search/dutch-national-flag&#34;&gt;ByteByteGo: Dutch National Flag&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;p&gt;Cheat, O(n log n) time:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;from collections import defaultdict&#xA;&#xA;def dutch_national_flag(nums: List[int]) -&amp;gt; None:&#xA;    nums.sort()&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;O(n) time, O(1) storage:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;from collections import defaultdict&#xA;&#xA;def dutch_national_flag(nums: List[int]) -&amp;gt; None:&#xA;    c = defaultdict(int)&#xA;&#xA;    for num in nums:&#xA;        c[num] &amp;#43;= 1&#xA;&#xA;    order = sorted(c.keys())&#xA;&#xA;    for i in range(len(nums)):&#xA;        for ins in order:&#xA;            if c[ins] &amp;gt; 0:&#xA;                nums[i] = ins&#xA;                c[ins] -= 1&#xA;                break&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Clean:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;&#xA;def dutch_national_flag(nums: List[int]) -&amp;gt; None:&#xA;    &amp;#34;&amp;#34;&amp;#34;&#xA;    In-place sort of array containing only 0,1,2 using the three-pointer method.&#xA;    Time: O(n), Space: O(1)&#xA;    &amp;#34;&amp;#34;&amp;#34;&#xA;    left = 0&#xA;    mid = 0&#xA;    right = len(nums) - 1&#xA;&#xA;    while mid &amp;lt;= right:&#xA;        if nums[mid] == 0:&#xA;            nums[left], nums[mid] = nums[mid], nums[left]&#xA;            left &amp;#43;= 1&#xA;            mid &amp;#43;= 1&#xA;        elif nums[mid] == 1:&#xA;            mid &amp;#43;= 1&#xA;        else:  # nums[mid] == 2&#xA;            nums[mid], nums[right] = nums[right], nums[mid]&#xA;            right -= 1&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Dutch National Flag&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Find the Insertion Index
      </title>
      <link>https://perrotta.dev/2025/11/bytebytego-find-the-insertion-index/</link>
      <pubDate>Wed, 26 Nov 2025 13:48:52 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/11/bytebytego-find-the-insertion-index/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/binary-search/find-the-insertion-index&#34;&gt;ByteByteGo: Find the Insertion Index&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;&#xA;def find_the_insertion_index(nums: List[int], target: int) -&amp;gt; int:&#xA;    if len(nums) == 0:&#xA;        return 0&#xA;&#xA;    left = 0&#xA;    right = len(nums) - 1&#xA;    assert right &amp;gt;= left&#xA;&#xA;    while left &amp;lt; right:&#xA;        mid = left &amp;#43; ((right - left) // 2)&#xA;&#xA;        if nums[mid] == target:&#xA;            return mid&#xA;&#xA;        elif nums[mid] &amp;lt; target:&#xA;            left = mid &amp;#43; 1&#xA;&#xA;        else:&#xA;            right = mid - 1&#xA;&#xA;    if nums[left] &amp;lt; target:&#xA;        return left &amp;#43; 1&#xA;    else:&#xA;        return left&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Elegant:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;import bisect&#xA;from typing import List&#xA;&#xA;def find_the_insertion_index(nums: List[int], target: int) -&amp;gt; int:&#xA;    return bisect.bisect_left(nums, target)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Find the Insertion Index&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Minimum Coin Combination
      </title>
      <link>https://perrotta.dev/2025/11/bytebytego-minimum-coin-combination/</link>
      <pubDate>Wed, 26 Nov 2025 13:28:33 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/11/bytebytego-minimum-coin-combination/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/dynamic-programming/minimum-coin-combination&#34;&gt;ByteByteGo: Minimum Coin Combination&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;p&gt;The &lt;code&gt;-1&lt;/code&gt; return requirement makes it a bit ugly.&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;from functools import lru_cache&#xA;&#xA;def min_coin_combination(coins: List[int], target: int) -&amp;gt; int:&#xA;    @lru_cache(maxsize=None)&#xA;    def solve(i, target):&#xA;        if i &amp;lt; 0 or target &amp;lt; 0:&#xA;            if target == 0:&#xA;                return 0&#xA;            else:&#xA;                return -1&#xA;&#xA;        q1 = solve(i, target - coins[i])&#xA;        q2 = solve(i - 1, target)&#xA;&#xA;        if q1 != -1 and q2 != -1:&#xA;            return min(&#xA;                q1 &amp;#43; 1,&#xA;                q2,&#xA;            )&#xA;        if q1 != -1:&#xA;            return q1 &amp;#43; 1&#xA;        if q2 != -1:&#xA;            return q2&#xA;&#xA;        return -1&#xA;&#xA;    return solve(len(coins) - 1, target)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Minimum Coin Combination&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Repeated Removal of Adjacent Duplicates
      </title>
      <link>https://perrotta.dev/2025/11/bytebytego-repeated-removal-of-adjacent-duplicates/</link>
      <pubDate>Tue, 25 Nov 2025 21:45:48 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/11/bytebytego-repeated-removal-of-adjacent-duplicates/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/stacks/repeated-removal-of-adjacent-duplicates&#34;&gt;ByteByteGo: Repeated Removal of Adjacent Duplicates&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;def repeated_removal_of_adjacent_duplicates(s: str) -&amp;gt; str:&#xA;    stack = []&#xA;&#xA;    for c in s:&#xA;        if len(stack) == 0 or stack[-1] != c:&#xA;            stack.append(c)&#xA;        elif stack[-1] == c:&#xA;            stack.pop()&#xA;&#xA;    return &amp;#39;&amp;#39;.join(stack)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Repeated Removal of Adjacent Duplicates&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Valid Parenthesis Expression
      </title>
      <link>https://perrotta.dev/2025/11/bytebytego-valid-parenthesis-expression/</link>
      <pubDate>Tue, 25 Nov 2025 21:39:15 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/11/bytebytego-valid-parenthesis-expression/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/stacks/valid-parenthesis-expression&#34;&gt;ByteByteGo: Valid Parenthesis Expression&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;def valid_parenthesis_expression(s: str) -&amp;gt; bool:&#xA;    stack = []&#xA;&#xA;    for c in s:&#xA;        if c in &amp;#39;([{&amp;#39;:&#xA;            stack.append(c)&#xA;&#xA;        if len(stack) == 0:&#xA;            return False&#xA;&#xA;        if c == &amp;#39;)&amp;#39;:&#xA;            if stack.pop() != &amp;#39;(&amp;#39;:&#xA;                return False&#xA;&#xA;        if c == &amp;#39;]&amp;#39;:&#xA;            if stack.pop() != &amp;#39;[&amp;#39;:&#xA;                return False&#xA;&#xA;        if c == &amp;#39;}&amp;#39;:&#xA;            if stack.pop() != &amp;#39;{&amp;#39;:&#xA;                return False&#xA;&#xA;    return len(stack) == 0&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Refinement:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;def valid_parenthesis_expression(s: str) -&amp;gt; bool:&#xA;    stack = []&#xA;&#xA;    ass = {&#xA;        &amp;#39;)&amp;#39;: &amp;#39;(&amp;#39;,&#xA;        &amp;#39;]&amp;#39;: &amp;#39;[&amp;#39;,&#xA;        &amp;#39;}&amp;#39;: &amp;#39;{&amp;#39;,&#xA;    }&#xA;&#xA;    for c in s:&#xA;        if c in &amp;#39;([{&amp;#39;:&#xA;            stack.append(c)&#xA;&#xA;        if len(stack) == 0:&#xA;            return False&#xA;&#xA;        if c in &amp;#39;)]}&amp;#39;:&#xA;            if stack.pop() != ass[c]:&#xA;                return False&#xA;&#xA;    return len(stack) == 0&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Valid Parenthesis Expression&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Linked List Reversal
      </title>
      <link>https://perrotta.dev/2025/11/bytebytego-linked-list-reversal/</link>
      <pubDate>Tue, 25 Nov 2025 21:18:56 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/11/bytebytego-linked-list-reversal/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/linked-lists/linked-list-reversal&#34;&gt;ByteByteGo: Linked List Reversal&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;h2 id=&#34;recursive&#34;&gt;&#xA;  Recursive&#xA;  &lt;a class=&#34;heading-anchor&#34; href=&#34;https://perrotta.dev/2025/11/bytebytego-linked-list-reversal/#recursive&#34; aria-label=&#34;Link to this section&#34;&gt;#&lt;/a&gt;&#xA;&lt;/h2&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from ds import ListNode&#xA;&#xA;&amp;#34;&amp;#34;&amp;#34;&#xA;Definition of ListNode:&#xA;class ListNode:&#xA;    def __init__(self, val=None, next=None):&#xA;        self.val = val&#xA;        self.next = next&#xA;&amp;#34;&amp;#34;&amp;#34;&#xA;&#xA;def linked_list_reversal(head: ListNode) -&amp;gt; ListNode:&#xA;    if head is None or head.next is None:&#xA;        return head&#xA;&#xA;    new_head = linked_list_reversal(head.next)&#xA;&#xA;    head.next.next = head&#xA;    head.next = None&#xA;&#xA;    return new_head&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;h2 id=&#34;iterative&#34;&gt;&#xA;  Iterative&#xA;  &lt;a class=&#34;heading-anchor&#34; href=&#34;https://perrotta.dev/2025/11/bytebytego-linked-list-reversal/#iterative&#34; aria-label=&#34;Link to this section&#34;&gt;#&lt;/a&gt;&#xA;&lt;/h2&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from ds import ListNode&#xA;&#xA;&amp;#34;&amp;#34;&amp;#34;&#xA;Definition of ListNode:&#xA;class ListNode:&#xA;    def __init__(self, val=None, next=None):&#xA;        self.val = val&#xA;        self.next = next&#xA;&amp;#34;&amp;#34;&amp;#34;&#xA;&#xA;def linked_list_reversal(head: ListNode) -&amp;gt; ListNode:&#xA;    if head is None or head.next is None:&#xA;        return head&#xA;&#xA;    prev = None&#xA;&#xA;    while head is not None:&#xA;        next_node = head.next&#xA;&#xA;        head.next = prev&#xA;&#xA;        prev = head&#xA;        head = next_node&#xA;&#xA;    return prev&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Linked List Reversal&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Invert Binary Tree
      </title>
      <link>https://perrotta.dev/2025/11/bytebytego-invert-binary-tree/</link>
      <pubDate>Tue, 25 Nov 2025 00:36:17 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/11/bytebytego-invert-binary-tree/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/trees/invert-binary-tree&#34;&gt;ByteByteGo: Invert Binary Tree&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from ds import TreeNode&#xA;&#xA;&amp;#34;&amp;#34;&amp;#34;&#xA;Definition of TreeNode:&#xA;class TreeNode:&#xA;    def __init__(self, val):&#xA;        self.val = val&#xA;        self.left = None&#xA;        self.right = None&#xA;&amp;#34;&amp;#34;&amp;#34;&#xA;&#xA;def invert_binary_tree(root: TreeNode) -&amp;gt; TreeNode:&#xA;    if root is None:&#xA;        return None&#xA;&#xA;    root.left, root.right = root.right, root.left&#xA;&#xA;    invert_binary_tree(root.left)&#xA;    invert_binary_tree(root.right)&#xA;&#xA;    return root&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;The following &lt;strong&gt;DOES NOT&lt;/strong&gt; work:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from ds import TreeNode&#xA;&#xA;&amp;#34;&amp;#34;&amp;#34;&#xA;Definition of TreeNode:&#xA;class TreeNode:&#xA;    def __init__(self, val):&#xA;        self.val = val&#xA;        self.left = None&#xA;        self.right = None&#xA;&amp;#34;&amp;#34;&amp;#34;&#xA;&#xA;def invert_binary_tree(root: TreeNode) -&amp;gt; TreeNode:&#xA;    if root is None:&#xA;        return None&#xA;&#xA;    root.left = invert_binary_tree(root.right)&#xA;    root.right = invert_binary_tree(root.left)&#xA;&#xA;    return root&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;The swap must happen simultaneously. The following works:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from ds import TreeNode&#xA;&#xA;&amp;#34;&amp;#34;&amp;#34;&#xA;Definition of TreeNode:&#xA;class TreeNode:&#xA;    def __init__(self, val):&#xA;        self.val = val&#xA;        self.left = None&#xA;        self.right = None&#xA;&amp;#34;&amp;#34;&amp;#34;&#xA;&#xA;def invert_binary_tree(root: TreeNode) -&amp;gt; TreeNode:&#xA;    if root is None:&#xA;        return None&#xA;&#xA;    root.left, root.right = invert_binary_tree(root.right), invert_binary_tree(root.left)&#xA;&#xA;    return root&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Invert Binary Tree&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Climbing Stairs
      </title>
      <link>https://perrotta.dev/2025/11/bytebytego-climbing-stairs/</link>
      <pubDate>Tue, 25 Nov 2025 00:27:38 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/11/bytebytego-climbing-stairs/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/dynamic-programming/climbing-stairs&#34;&gt;ByteByteGo: Climbing Stairs&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;p&gt;Top-down DP:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;def climbing_stairs(n: int) -&amp;gt; int:&#xA;    from functools import lru_cache&#xA;&#xA;    @lru_cache(maxsize=None)&#xA;    def cs(n):&#xA;        &amp;#34;&amp;#34;&amp;#34;How many possibilities for n steps?&amp;#34;&amp;#34;&amp;#34;&#xA;        assert n &amp;gt;= 0&#xA;&#xA;        if n in [0, 1, 2]:&#xA;            return n&#xA;&#xA;        return cs(n-1) &amp;#43; cs(n-2)&#xA;&#xA;    return cs(n)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Their Python environment is quite old (3.8). Goodness.&lt;/p&gt;&#xA;&lt;p&gt;We can only do &lt;code&gt;from functools import cache&lt;/code&gt; &lt;a href=&#34;https://stackoverflow.com/questions/66846743/importerror-cannot-import-name-cache-from-functools&#34;&gt;from Python&#xA;3.9+&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Bottom-up DP:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;def climbing_stairs(n: int) -&amp;gt; int:&#xA;    dp = [None] * n&#xA;&#xA;    dp[0] = 1&#xA;    dp[1] = 2&#xA;&#xA;    for i in range(2, n):&#xA;        dp[i] = dp[i - 1] &amp;#43; dp[i - 2]&#xA;&#xA;    return dp[n - 1]&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Climbing Stairs&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Zero Striping
      </title>
      <link>https://perrotta.dev/2025/11/bytebytego-zero-striping/</link>
      <pubDate>Tue, 25 Nov 2025 00:13:43 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/11/bytebytego-zero-striping/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/hash-maps-and-sets/zero-striping&#34;&gt;ByteByteGo: Zero Striping&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;&#xA;def zero_striping(matrix: List[List[int]]) -&amp;gt; None:&#xA;    row_set = set()&#xA;    col_set = set()&#xA;&#xA;    for x in range(len(matrix)):&#xA;        for y in range(len(matrix[0])):&#xA;            c = matrix[x][y]&#xA;&#xA;            if c == 0:&#xA;                row_set.add(x)&#xA;                col_set.add(y)&#xA;&#xA;    for x in range(len(matrix)):&#xA;        for y in range(len(matrix[0])):&#xA;            if x in row_set or y in col_set:&#xA;                matrix[x][y] = 0&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Zero Striping&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Verify Sudoku Board
      </title>
      <link>https://perrotta.dev/2025/11/bytebytego-verify-sudoku-board/</link>
      <pubDate>Mon, 24 Nov 2025 22:09:17 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/11/bytebytego-verify-sudoku-board/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/hash-maps-and-sets/verify-sudoku-board&#34;&gt;ByteByteGo: Verify Sudoku Board&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;&#xA;def verify_sudoku_board(board: List[List[int]]) -&amp;gt; bool:&#xA;    row_sets = [set() for _ in range(9)]&#xA;    col_sets = [set() for _ in range(9)]&#xA;    grid_sets = [[set() for _ in range(3)] for _ in range(3)]&#xA;&#xA;    for x in range(len(board)):&#xA;        for y in range(len(board[0])):&#xA;            c = board[x][y]&#xA;            if c == 0:&#xA;                continue&#xA;&#xA;            if c in row_sets[x]:&#xA;                return False&#xA;            else:&#xA;                row_sets[x].add(c)&#xA;&#xA;            if c in col_sets[y]:&#xA;                return False&#xA;            else:&#xA;                col_sets[y].add(c)&#xA;&#xA;            if c in grid_sets[x // 3][y // 3]:&#xA;                return False&#xA;            else:&#xA;                grid_sets[x // 3][y // 3].add(c)&#xA;&#xA;    return True&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Verify Sudoku Board&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Longest Chain of Consecutive Numbers
      </title>
      <link>https://perrotta.dev/2025/11/bytebytego-longest-chain-of-consecutive-numbers/</link>
      <pubDate>Mon, 24 Nov 2025 00:41:57 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/11/bytebytego-longest-chain-of-consecutive-numbers/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/hash-maps-and-sets/longest-chain-of-consecutive-numbers&#34;&gt;ByteByteGo: Longest Chain of Consecutive Numbers&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;&#xA;def longest_chain_of_consecutive_numbers(nums: List[int]) -&amp;gt; int:&#xA;    nums.sort()&#xA;&#xA;    ans = 0&#xA;&#xA;    size = 0&#xA;    last = None&#xA;&#xA;    for n in nums:&#xA;        if last is None:&#xA;            last = n&#xA;            size = 1&#xA;        elif n == last &amp;#43; 1:&#xA;            last = n&#xA;            size &amp;#43;= 1&#xA;        elif n == last:  # alternatively: put nums in a set&#xA;            continue&#xA;        else:&#xA;            last = n&#xA;            size = 1&#xA;        ans = max(ans, size)&#xA;&#xA;    return ans&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Alternative:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;&#xA;def longest_chain_of_consecutive_numbers(nums: List[int]) -&amp;gt; int:&#xA;    ans = 0&#xA;&#xA;    size = 0&#xA;    last = None&#xA;&#xA;    for n in sorted(set(nums)):&#xA;        if last is None:&#xA;            last = n&#xA;            size = 1&#xA;        elif n == last &amp;#43; 1:&#xA;            last = n&#xA;            size &amp;#43;= 1&#xA;        else:&#xA;            last = n&#xA;            size = 1&#xA;        ans = max(ans, size)&#xA;&#xA;    return ans&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Longest Chain of Consecutive Numbers&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Shift Zeros to the End
      </title>
      <link>https://perrotta.dev/2025/11/bytebytego-shift-zeros-to-the-end/</link>
      <pubDate>Sun, 23 Nov 2025 18:16:40 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/11/bytebytego-shift-zeros-to-the-end/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://perrotta.dev/2025/09/leetcode-%23283-move-zeroes/&#34;&gt;Same as LeetCode #283&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/two-pointers/shift-zeros-to-the-end&#34;&gt;ByteByteGo: Shift Zeros to the End&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;&#xA;def shift_zeros_to_the_end(nums: List[int]) -&amp;gt; None:&#xA;    j = None&#xA;&#xA;    for i, num in enumerate(nums):&#xA;        if num != 0:&#xA;            if j is not None:&#xA;                assert j &amp;lt; i&#xA;                nums[i], nums[j] = nums[j], nums[i]&#xA;                j &amp;#43;= 1&#xA;        else:&#xA;            if j is None:&#xA;                j = i&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Simplified version:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;&#xA;def shift_zeros_to_the_end(nums: List[int]) -&amp;gt; None:&#xA;    j = 0&#xA;&#xA;    for i, num in enumerate(nums):&#xA;        if num != 0:&#xA;            nums[i], nums[j] = nums[j], nums[i]&#xA;            j &amp;#43;= 1&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Shift Zeros to the End&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Largest Container
      </title>
      <link>https://perrotta.dev/2025/11/bytebytego-largest-container/</link>
      <pubDate>Sun, 23 Nov 2025 17:09:59 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/11/bytebytego-largest-container/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/two-pointers/largest-container&#34;&gt;ByteByteGo: Largest Container&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;&#xA;def largest_container(heights: List[int]) -&amp;gt; int:&#xA;    left = 0&#xA;    right = len(heights) - 1&#xA;&#xA;    ans = 0&#xA;&#xA;    def area(left, right):&#xA;        assert 0 &amp;lt;= left &amp;lt; len(heights)&#xA;        assert 0 &amp;lt;= right &amp;lt; len(heights)&#xA;&#xA;        return (right - left) * min(heights[left], heights[right])&#xA;&#xA;    while left &amp;lt; right:&#xA;        myarea = area(left, right)&#xA;        ans = max(ans, myarea)&#xA;&#xA;        if heights[left] &amp;lt; heights[left &amp;#43; 1]:&#xA;            left &amp;#43;= 1&#xA;        elif heights[right - 1] &amp;gt; heights[right]:&#xA;            right -= 1&#xA;        else:&#xA;            left &amp;#43;= 1&#xA;            right -= 1&#xA;&#xA;    return ans&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Converge two pointers inwards. No mystery.&lt;/p&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Largest Container&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Triplet Sum
      </title>
      <link>https://perrotta.dev/2025/11/bytebytego-triplet-sum/</link>
      <pubDate>Sun, 23 Nov 2025 17:00:21 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/11/bytebytego-triplet-sum/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/two-pointers/triplet-sum&#34;&gt;ByteByteGo: Triplet Sum&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;&#xA;def triplet_sum(nums: List[int]) -&amp;gt; List[List[int]]:&#xA;    sequence = sorted(nums)&#xA;&#xA;    ans = []&#xA;&#xA;    for ia, a in enumerate(sequence):&#xA;        if a &amp;gt; 0:  # positive numbers cannot add up to zero&#xA;            break&#xA;&#xA;        if ia &amp;gt; 0 and sequence[ia - 1] == sequence[ia]:  # prevent duplicates&#xA;            continue&#xA;&#xA;        ib = ia &amp;#43; 1&#xA;        ic = len(sequence) - 1&#xA;&#xA;        while ib &amp;lt; ic:&#xA;            b = sequence[ib]&#xA;            c = sequence[ic]&#xA;&#xA;            target = -a&#xA;            if (b &amp;#43; c) == target:&#xA;                ans.append([a, b, c])&#xA;                ib &amp;#43;= 1&#xA;                ic -= 1&#xA;&#xA;                # keep going, preventing duplicates&#xA;                while ib &amp;lt; ic and sequence[ib - 1] == sequence[ib]:&#xA;                    ib &amp;#43;= 1&#xA;                while ib &amp;lt; ic and sequence[ic &amp;#43; 1] == sequence[ic]:&#xA;                    ic -= 1&#xA;            elif (b &amp;#43; c) &amp;lt; target:&#xA;                ib &amp;#43;= 1&#xA;            else:&#xA;                ic -=1&#xA;&#xA;    return ans&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Time complexity: O(n^2)&lt;/p&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Triplet Sum&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>ByteByteGo: Pair Sum - Unsorted
      </title>
      <link>https://perrotta.dev/2025/11/bytebytego-pair-sum-unsorted/</link>
      <pubDate>Sat, 22 Nov 2025 19:28:06 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>bytebytego</category>
      <category>dev</category>
      <guid>https://perrotta.dev/2025/11/bytebytego-pair-sum-unsorted/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://bytebytego.com/exercises/coding-patterns/hash-maps-and-sets/pair-sum-unsorted&#34;&gt;ByteByteGo: Pair Sum — Unsorted&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from typing import List&#xA;&#xA;def pair_sum_unsorted(nums: List[int], target: int) -&amp;gt; List[int]:&#xA;    # Option 1: sort it and do two pointers.&#xA;    # Option 2: hash map.&#xA;    d = {}&#xA;    for i, num in enumerate(nums):&#xA;        d[num] = i&#xA;&#xA;    for i, num in enumerate(nums):&#xA;        if (target - num) in d.keys():&#xA;            j = d[target - num]&#xA;            if i != j:&#xA;                return [i, j]&#xA;&#xA;    return []&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Be careful not to pick the same index twice.&lt;/p&gt;&#xA;&lt;p&gt;Simpler way to construct the hash map: a dict comprehension:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;d = {num: i for i, num in enumerate(nums)}&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Fancier:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;d = dict(zip(nums, range(len(nums))))&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;— § —&lt;/p&gt;&lt;p&gt;Reply via &lt;a href=&#34;mailto:serendipity@perrotta.dev?subject=Reply to: ByteByteGo: Pair Sum - Unsorted&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/bytebytego/&#34;&gt;#bytebytego&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/categories/coding/&#34;&gt;%coding&lt;/a&gt;&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
