<?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>Leetcode on ¬ just serendipity 🍀</title>
    <link>https://perrotta.dev/</link>
    <description>Recent content in Leetcode 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>Mon, 06 Apr 2026 01:17:10 +0200</lastBuildDate>
    <atom:link href="https://perrotta.dev/tags/leetcode/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>LeetCode #355: Design Twitter
      </title>
      <link>https://perrotta.dev/2026/04/leetcode-%23355-design-twitter/</link>
      <pubDate>Mon, 06 Apr 2026 00:54:49 +0200</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/04/leetcode-%23355-design-twitter/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/design-twitter&#34;&gt;LeetCode #355: Design Twitter&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;p&gt;My original solution:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from collections import defaultdict&#xA;from itertools import chain&#xA;&#xA;class Twitter:&#xA;&#xA;    def __init__(self):&#xA;        # [userId] -&amp;gt; {userId, ...}&#xA;        self.f = defaultdict(set)&#xA;&#xA;        self.tweets = defaultdict(list)&#xA;&#xA;        self.ts = 0&#xA;&#xA;&#xA;    def postTweet(self, userId: int, tweetId: int) -&amp;gt; None:&#xA;        self.tweets[userId].append((self.ts, tweetId))&#xA;        self.ts &amp;#43;= 1&#xA;&#xA;    def getNewsFeed(self, userId: int) -&amp;gt; List[int]:&#xA;        tweets_with_ts = []&#xA;&#xA;        ## for followee in list(self.f[userId]) &amp;#43; [userId]:&#xA;        for followee in chain(self.f[userId], [userId]):&#xA;            tweets_with_ts.extend(self.tweets[followee][::-1])&#xA;&#xA;        # [(1, 3), (0, 5)]&#xA;        tweets_with_ts.sort(reverse=True)&#xA;&#xA;        tweets = [id for (_, id) in tweets_with_ts]&#xA;&#xA;        return tweets[:10]&#xA;&#xA;    def follow(self, followerId: int, followeeId: int) -&amp;gt; None:&#xA;        self.f[followerId].add(followeeId)&#xA;&#xA;&#xA;    def unfollow(self, followerId: int, followeeId: int) -&amp;gt; None:&#xA;        self.f[followerId].discard(followeeId)&#xA;&#xA;&#xA;&#xA;# Your Twitter object will be instantiated and called as such:&#xA;# obj = Twitter()&#xA;# obj.postTweet(userId,tweetId)&#xA;# param_2 = obj.getNewsFeed(userId)&#xA;# obj.follow(followerId,followeeId)&#xA;# obj.unfollow(followerId,followeeId)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;With &lt;code&gt;deque&lt;/code&gt; (slow):&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from collections import defaultdict, deque&#xA;from itertools import chain&#xA;&#xA;class Twitter:&#xA;&#xA;    def __init__(self):&#xA;        self.f = defaultdict(set)&#xA;        self.tweets = deque()&#xA;&#xA;    def postTweet(self, userId: int, tweetId: int) -&amp;gt; None:&#xA;        self.tweets.appendleft((userId, tweetId))&#xA;&#xA;    def getNewsFeed(self, userId: int) -&amp;gt; List[int]:&#xA;        return [tweetId for (user, tweetId) in self.tweets if user == userId or user in self.f[userId]][:10]&#xA;&#xA;    def follow(self, followerId: int, followeeId: int) -&amp;gt; None:&#xA;        self.f[followerId].add(followeeId)&#xA;&#xA;&#xA;    def unfollow(self, followerId: int, followeeId: int) -&amp;gt; None:&#xA;        self.f[followerId].discard(followeeId)&#xA;&#xA;&#xA;&#xA;# Your Twitter object will be instantiated and called as such:&#xA;# obj = Twitter()&#xA;# obj.postTweet(userId,tweetId)&#xA;# param_2 = obj.getNewsFeed(userId)&#xA;# obj.follow(followerId,followeeId)&#xA;# obj.unfollow(followerId,followeeId)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;With heap:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from collections import defaultdict, deque&#xA;from itertools import chain&#xA;import heapq&#xA;&#xA;class Twitter:&#xA;&#xA;    def __init__(self):&#xA;        self.f = defaultdict(set)&#xA;        self.tweets = defaultdict(list)&#xA;        self.ts = 0&#xA;&#xA;    def postTweet(self, userId: int, tweetId: int) -&amp;gt; None:&#xA;        self.tweets[userId].append((-self.ts, tweetId))&#xA;        self.ts &amp;#43;= 1&#xA;&#xA;    def getNewsFeed(self, userId: int) -&amp;gt; List[int]:&#xA;        tweets = []&#xA;        heap = []&#xA;&#xA;        for followee in chain(self.f[userId], [userId]):&#xA;            heap.extend(self.tweets[followee])&#xA;&#xA;        heapq.heapify(heap)&#xA;&#xA;        for _ in range(min(10, len(heap))):&#xA;            tweets.append(heapq.heappop(heap)[1])&#xA;&#xA;        return tweets&#xA;&#xA;    def follow(self, followerId: int, followeeId: int) -&amp;gt; None:&#xA;        self.f[followerId].add(followeeId)&#xA;&#xA;&#xA;    def unfollow(self, followerId: int, followeeId: int) -&amp;gt; None:&#xA;        self.f[followerId].discard(followeeId)&#xA;&#xA;&#xA;&#xA;# Your Twitter object will be instantiated and called as such:&#xA;# obj = Twitter()&#xA;# obj.postTweet(userId,tweetId)&#xA;# param_2 = obj.getNewsFeed(userId)&#xA;# obj.follow(followerId,followeeId)&#xA;# obj.unfollow(followerId,followeeId)&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: LeetCode #355: Design Twitter&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #3822: Design Order Management System
      </title>
      <link>https://perrotta.dev/2026/03/leetcode-%233822-design-order-management-system/</link>
      <pubDate>Sun, 01 Mar 2026 18:21:49 +0100</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/03/leetcode-%233822-design-order-management-system/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/design-order-management-system&#34;&gt;LeetCode #3822: Design Order Management System&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from dataclasses import dataclass&#xA;&#xA;@dataclass&#xA;class Entry():&#xA;    orderType: OrderType&#xA;    price: int&#xA;&#xA;class OrderManagementSystem:&#xA;&#xA;    def __init__(self):&#xA;        self.orders = {}&#xA;&#xA;    def addOrder(self, orderId: int, orderType: str, price: int) -&amp;gt; None:&#xA;        assert orderType in [&amp;#34;buy&amp;#34;, &amp;#34;sell&amp;#34;], f&amp;#34;invalid orderType: {orderType}&amp;#34;&#xA;&#xA;        self.orders[orderId] = Entry(orderType, price)&#xA;&#xA;&#xA;    def modifyOrder(self, orderId: int, newPrice: int) -&amp;gt; None:&#xA;        assert orderId in self.orders&#xA;&#xA;        orderType = self.orders[orderId].orderType&#xA;        self.orders[orderId] = Entry(orderType, newPrice)&#xA;&#xA;&#xA;    def cancelOrder(self, orderId: int) -&amp;gt; None:&#xA;        assert orderId in self.orders&#xA;&#xA;        del self.orders[orderId]&#xA;&#xA;    def getOrdersAtPrice(self, orderType: str, price: int) -&amp;gt; List[int]:&#xA;        return [orderId for (orderId, entry) in self.orders.items() if entry.orderType == orderType and entry.price == price]&#xA;&#xA;&#xA;# Your OrderManagementSystem object will be instantiated and called as such:&#xA;# obj = OrderManagementSystem()&#xA;# obj.addOrder(orderId,orderType,price)&#xA;# obj.modifyOrder(orderId,newPrice)&#xA;# obj.cancelOrder(orderId)&#xA;# param_4 = obj.getOrdersAtPrice(orderType,price)&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: LeetCode #3822: Design Order Management System&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #146: LRU Cache
      </title>
      <link>https://perrotta.dev/2026/03/leetcode-%23146-lru-cache/</link>
      <pubDate>Sun, 01 Mar 2026 18:09:17 +0100</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/03/leetcode-%23146-lru-cache/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/lru-cache&#34;&gt;LeetCode #146: LRU Cache&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from collections import OrderedDict&#xA;&#xA;class LRUCache:&#xA;&#xA;    def __init__(self, capacity: int):&#xA;        self.m = OrderedDict()&#xA;        self.max_size = capacity&#xA;&#xA;&#xA;    def get(self, key: int) -&amp;gt; int:&#xA;        if key in self.m:&#xA;            self.m.move_to_end(key)&#xA;            return self.m[key]&#xA;&#xA;        return -1&#xA;&#xA;&#xA;    def put(self, key: int, value: int) -&amp;gt; None:&#xA;        if key in self.m:&#xA;            self.m[key] = value&#xA;            self.m.move_to_end(key)&#xA;        else:&#xA;            if len(self.m) &amp;gt;= self.max_size:&#xA;                self.m.popitem(False)  # LRU / FIFO&#xA;            self.m[key] = value&#xA;&#xA;&#xA;# Your LRUCache object will be instantiated and called as such:&#xA;# obj = LRUCache(capacity)&#xA;# param_1 = obj.get(key)&#xA;# obj.put(key,value)&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: LeetCode #146: LRU Cache&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #379: Design Phone Directory
      </title>
      <link>https://perrotta.dev/2026/02/leetcode-%23379-design-phone-directory/</link>
      <pubDate>Mon, 23 Feb 2026 01:25:36 +0100</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/02/leetcode-%23379-design-phone-directory/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/design-phone-directory&#34;&gt;LeetCode #379: Design Phone Directory&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;class PhoneDirectory:&#xA;&#xA;    def __init__(self, maxNumbers: int):&#xA;        self.available = set(range(maxNumbers))&#xA;&#xA;    def get(self) -&amp;gt; int:&#xA;        if not self.available:&#xA;            return -1&#xA;&#xA;        return self.available.pop()&#xA;&#xA;&#xA;    def check(self, number: int) -&amp;gt; bool:&#xA;        return number in self.available&#xA;&#xA;&#xA;    def release(self, number: int) -&amp;gt; None:&#xA;        self.available.add(number)&#xA;&#xA;&#xA;# Your PhoneDirectory object will be instantiated and called as such:&#xA;# obj = PhoneDirectory(maxNumbers)&#xA;# param_1 = obj.get()&#xA;# param_2 = obj.check(number)&#xA;# obj.release(number)&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: LeetCode #379: Design Phone Directory&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #2166: Design Bitset
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%232166-design-bitset/</link>
      <pubDate>Tue, 20 Jan 2026 22:20:59 +0100</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%232166-design-bitset/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/design-bitset&#34;&gt;LeetCode #2166: Design Bitset&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;p&gt;Time limit exceeded:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;class Bitset:&#xA;&#xA;    def __init__(self, size: int):&#xA;        self.bits = [False] * size&#xA;&#xA;&#xA;    def fix(self, idx: int) -&amp;gt; None:&#xA;        self.bits[idx] = 1&#xA;&#xA;&#xA;    def unfix(self, idx: int) -&amp;gt; None:&#xA;        self.bits[idx] = 0&#xA;&#xA;&#xA;    def flip(self) -&amp;gt; None:&#xA;        self.bits = [not bit for bit in self.bits]&#xA;&#xA;&#xA;    def all(self) -&amp;gt; bool:&#xA;        return all(self.bits)&#xA;&#xA;&#xA;    def one(self) -&amp;gt; bool:&#xA;        return any(self.bits)&#xA;&#xA;&#xA;    def count(self) -&amp;gt; int:&#xA;        return len([bit for bit in self.bits if bit])&#xA;&#xA;&#xA;    def toString(self) -&amp;gt; str:&#xA;        return &amp;#39;&amp;#39;.join(&amp;#39;1&amp;#39; if bit else &amp;#39;0&amp;#39; for bit in self.bits)&#xA;&#xA;&#xA;&#xA;# Your Bitset object will be instantiated and called as such:&#xA;# obj = Bitset(size)&#xA;# obj.fix(idx)&#xA;# obj.unfix(idx)&#xA;# obj.flip()&#xA;# param_4 = obj.all()&#xA;# param_5 = obj.one()&#xA;# param_6 = obj.count()&#xA;# param_7 = obj.toString()&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Optimized:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;class Bitset:&#xA;&#xA;    def __init__(self, size: int):&#xA;        self.bits = [False] * size&#xA;        self.ones = 0&#xA;        self.flipped = False&#xA;&#xA;&#xA;    def fix(self, idx: int) -&amp;gt; None:&#xA;        if self.flipped:&#xA;            # When flipped, fixing means setting underlying bit to 0&#xA;            if self.bits[idx]:&#xA;                self.bits[idx] = False&#xA;                self.ones -= 1&#xA;        else:&#xA;            # Normal case: set to 1&#xA;            if not self.bits[idx]:&#xA;                self.bits[idx] = True&#xA;                self.ones &amp;#43;= 1&#xA;&#xA;&#xA;    def unfix(self, idx: int) -&amp;gt; None:&#xA;        if self.flipped:&#xA;            # When flipped, unfixing means setting underlying bit to 1&#xA;            if not self.bits[idx]:&#xA;                self.bits[idx] = True&#xA;                self.ones &amp;#43;= 1&#xA;        else:&#xA;            # Normal case: set to 0&#xA;            if self.bits[idx]:&#xA;                self.bits[idx] = False&#xA;                self.ones -= 1&#xA;&#xA;&#xA;    def flip(self) -&amp;gt; None:&#xA;        self.flipped = not self.flipped&#xA;&#xA;&#xA;    def all(self) -&amp;gt; bool:&#xA;        if self.flipped:&#xA;            return self.ones == 0&#xA;        else:&#xA;            return self.ones == len(self.bits)&#xA;&#xA;&#xA;    def one(self) -&amp;gt; bool:&#xA;        if self.flipped:&#xA;            # When flipped, we need at least one 0 in underlying array (which becomes 1)&#xA;            return self.ones &amp;lt; len(self.bits)&#xA;        else:&#xA;            return self.ones &amp;gt; 0&#xA;&#xA;&#xA;    def count(self) -&amp;gt; int:&#xA;        if self.flipped:&#xA;            return len(self.bits) - self.ones&#xA;        else:&#xA;            return self.ones&#xA;&#xA;&#xA;    def toString(self) -&amp;gt; str:&#xA;        if self.flipped:&#xA;            return &amp;#39;&amp;#39;.join(&amp;#39;0&amp;#39; if bit else &amp;#39;1&amp;#39; for bit in self.bits)&#xA;        else:&#xA;            return &amp;#39;&amp;#39;.join(&amp;#39;1&amp;#39; if bit else &amp;#39;0&amp;#39; for bit in self.bits)&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: LeetCode #2166: Design Bitset&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #1047: Remove All Adjacent Duplicates In String
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%231047-remove-all-adjacent-duplicates-in-string/</link>
      <pubDate>Sun, 18 Jan 2026 13:37:46 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%231047-remove-all-adjacent-duplicates-in-string/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string&#34;&gt;LeetCode #1047: Remove All Adjacent Duplicates In String&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;p&gt;Initial:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;class Solution:&#xA;    def removeDuplicates(self, s: str) -&amp;gt; str:&#xA;        stack = []&#xA;        ans = []&#xA;&#xA;        for c in s:&#xA;            if not stack:&#xA;                stack.append(c)&#xA;                ans.append(c)&#xA;            else:&#xA;                if stack[-1] != c:&#xA;                    stack.append(c)&#xA;                    ans.append(c)&#xA;                else:&#xA;                    stack.pop()&#xA;                    ans.pop()&#xA;&#xA;        return &amp;#39;&amp;#39;.join(ans)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Optimized:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;class Solution:&#xA;    def removeDuplicates(self, s: str) -&amp;gt; str:&#xA;        stack = []&#xA;        ans = []&#xA;&#xA;        for c in s:&#xA;            if not stack or stack[-1] != c:&#xA;                stack.append(c)&#xA;                ans.append(c)&#xA;            else:&#xA;                stack.pop()&#xA;                ans.pop()&#xA;&#xA;        return &amp;#39;&amp;#39;.join(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: LeetCode #1047: Remove All Adjacent Duplicates In String&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #380: Insert Delete GetRandom O(1)
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%23380-insert-delete-getrandom-o1/</link>
      <pubDate>Sun, 18 Jan 2026 02:54:25 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%23380-insert-delete-getrandom-o1/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/insert-delete-getrandom-o%281%29&#34;&gt;LeetCode #380: Insert Delete GetRandom O(1)&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;p&gt;Initial solution:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;import random&#xA;&#xA;class RandomizedSet:&#xA;&#xA;    def __init__(self):&#xA;        self.l = []&#xA;&#xA;        self.d = {}&#xA;        ## self.d = dict()&#xA;&#xA;    def insert(self, val: int) -&amp;gt; bool:&#xA;        if val not in self.d:&#xA;            self.d[val] = len(self.l)&#xA;            self.l.append(val)&#xA;            return True&#xA;&#xA;        return False&#xA;&#xA;&#xA;    def remove(self, val: int) -&amp;gt; bool:&#xA;        if val in self.d:&#xA;            del self.d[val]&#xA;            ## self.l: do not touch. Removal is O(n)&#xA;            return True&#xA;&#xA;        return False&#xA;&#xA;&#xA;    def getRandom(self) -&amp;gt; int:&#xA;        return random.choice(list(self.d.keys()))&#xA;&#xA;&#xA;# Your RandomizedSet object will be instantiated and called as such:&#xA;# obj = RandomizedSet()&#xA;# param_1 = obj.insert(val)&#xA;# param_2 = obj.remove(val)&#xA;# param_3 = obj.getRandom()&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;However, &lt;code&gt;getRandom()&lt;/code&gt; is &lt;code&gt;O(n)&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Better solution:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;import random&#xA;&#xA;class RandomizedSet:&#xA;&#xA;    def __init__(self):&#xA;        self.l = []&#xA;&#xA;        self.d = {}&#xA;        ## self.d = dict()&#xA;&#xA;    def insert(self, val: int) -&amp;gt; bool:&#xA;        if val not in self.d:&#xA;            self.d[val] = len(self.l)&#xA;            self.l.append(val)&#xA;            return True&#xA;&#xA;        return False&#xA;&#xA;&#xA;    def remove(self, val: int) -&amp;gt; bool:&#xA;        if val in self.d:&#xA;            i = self.d[val]&#xA;            del self.d[val]&#xA;&#xA;            del self.l[i]&#xA;            return True&#xA;&#xA;        return False&#xA;&#xA;&#xA;    def getRandom(self) -&amp;gt; int:&#xA;        return random.choice(self.l)&#xA;&#xA;&#xA;# Your RandomizedSet object will be instantiated and called as such:&#xA;# obj = RandomizedSet()&#xA;# param_1 = obj.insert(val)&#xA;# param_2 = obj.remove(val)&#xA;# param_3 = obj.getRandom()&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;But it&amp;rsquo;s still &lt;code&gt;O(n)&lt;/code&gt; because of the list deletion.&lt;/p&gt;&#xA;&lt;p&gt;Even better:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;import random&#xA;&#xA;class RandomizedSet:&#xA;&#xA;    def __init__(self):&#xA;        self.l = []&#xA;&#xA;        self.d = {}&#xA;        ## self.d = dict()&#xA;&#xA;    def insert(self, val: int) -&amp;gt; bool:&#xA;        if val not in self.d:&#xA;            self.d[val] = len(self.l)&#xA;            self.l.append(val)&#xA;            return True&#xA;&#xA;        return False&#xA;&#xA;&#xA;    def remove(self, val: int) -&amp;gt; bool:&#xA;        if val in self.d:&#xA;            i = self.d[val]&#xA;            del self.d[val]&#xA;&#xA;            last_val = self.l[-1]&#xA;&#xA;            # Swap with the last element&#xA;            self.l[i] = last_val&#xA;            self.l.pop()&#xA;&#xA;            # Update the dictionary for the swapped element only if it&amp;#39;s not the same as removed valA&#xA;            # i.e. not the last element&#xA;            if i &amp;lt; len(self.l):&#xA;            ## if i != len(self.l):&#xA;                self.d[last_val] = i&#xA;&#xA;            return True&#xA;&#xA;        return False&#xA;&#xA;&#xA;    def getRandom(self) -&amp;gt; int:&#xA;        return random.choice(self.l)&#xA;&#xA;&#xA;# Your RandomizedSet object will be instantiated and called as such:&#xA;# obj = RandomizedSet()&#xA;# param_1 = obj.insert(val)&#xA;# param_2 = obj.remove(val)&#xA;# param_3 = obj.getRandom()&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: LeetCode #380: Insert Delete GetRandom O(1)&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #288: Unique Word Abbreviation
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%23288-unique-word-abbreviation/</link>
      <pubDate>Sat, 17 Jan 2026 03:00:13 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%23288-unique-word-abbreviation/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/unique-word-abbreviation&#34;&gt;LeetCode #288: Unique Word Abbreviation&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from collections import Counter&#xA;&#xA;def abbrev(word):&#xA;    if len(word) &amp;lt; 3:&#xA;        return word&#xA;&#xA;    return f&amp;#39;{word[0]}{len(word) - 2}{word[-1]}&amp;#39;&#xA;&#xA;assert abbrev(&amp;#34;dg&amp;#34;) == &amp;#34;dg&amp;#34;&#xA;assert abbrev(&amp;#34;dog&amp;#34;) == &amp;#34;d1g&amp;#34;&#xA;assert abbrev(&amp;#34;a&amp;#34;) == &amp;#34;a&amp;#34;&#xA;&#xA;&#xA;class ValidWordAbbr:&#xA;&#xA;    def __init__(self, words: List[str]):&#xA;        self.words = words&#xA;&#xA;        ## self.mappings = Counter()  # abbrev -&amp;gt; word&#xA;        ## for word in set(words):&#xA;        ##     self.mappings[abbrev(word)] &amp;#43;= 1&#xA;&#xA;        self.mappings = Counter(abbrev(word) for word in set(words))&#xA;&#xA;    def isUnique(self, word: str) -&amp;gt; bool:&#xA;        ab = abbrev(word)&#xA;&#xA;        return self.mappings[ab] &amp;lt; 1 or (self.mappings[ab] == 1 and word in self.words)&#xA;&#xA;&#xA;# Your ValidWordAbbr object will be instantiated and called as such:&#xA;# obj = ValidWordAbbr(dictionary)&#xA;# param_1 = obj.isUnique(word)&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: LeetCode #288: Unique Word Abbreviation&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #23: Merge k Sorted Lists
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%2323-merge-k-sorted-lists/</link>
      <pubDate>Thu, 15 Jan 2026 23:20:52 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%2323-merge-k-sorted-lists/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/merge-k-sorted-lists&#34;&gt;LeetCode #23: Merge k Sorted Lists&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;p&gt;Heap of ListNodes:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;import heapq&#xA;&#xA;# Definition for singly-linked list.&#xA;# class ListNode:&#xA;#     def __init__(self, val=0, next=None):&#xA;#         self.val = val&#xA;#         self.next = next&#xA;class HeapNode:&#xA;    def __init__(self, node: ListNode):&#xA;        self.node = node&#xA;&#xA;    def __lt__(self, other: HeapNode):&#xA;        return self.node.val &amp;lt; other.node.val&#xA;&#xA;class Solution:&#xA;    def mergeKLists(self, lists: List[Optional[ListNode]]) -&amp;gt; Optional[ListNode]:&#xA;        prehead = ListNode(None)&#xA;&#xA;        curr = prehead&#xA;        heap = []&#xA;&#xA;        for lst in lists:&#xA;            if lst:&#xA;                heapq.heappush(heap, HeapNode(lst))&#xA;&#xA;        while heap:&#xA;            # find next element: val&#xA;            next_heap_node = heapq.heappop(heap)&#xA;            next_node = next_heap_node.node&#xA;&#xA;            curr.next = ListNode(next_node.val)&#xA;            curr = curr.next&#xA;&#xA;            next_node = next_node.next&#xA;            if next_node:&#xA;                heapq.heappush(heap, HeapNode(next_node))&#xA;&#xA;&#xA;        head = prehead.next&#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: LeetCode #23: Merge k Sorted Lists&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #362: Design Hit Counter
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%23362-design-hit-counter/</link>
      <pubDate>Thu, 15 Jan 2026 23:10:19 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%23362-design-hit-counter/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/design-hit-counter&#34;&gt;LeetCode #362: Design Hit Counter&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;p&gt;With &lt;code&gt;Counter&lt;/code&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;import bisect&#xA;from collections import Counter&#xA;&#xA;class HitCounter:&#xA;&#xA;    def __init__(self):&#xA;        self.hits = Counter()&#xA;        ## timestamp -&amp;gt; hits&#xA;&#xA;    def hit(self, timestamp: int) -&amp;gt; None:&#xA;        self.hits[timestamp] &amp;#43;= 1&#xA;&#xA;    def getHits(self, timestamp: int) -&amp;gt; int:&#xA;        ## 3 * 60 -&amp;gt; 2&#xA;        ## 4 * 60 -&amp;gt; 1&#xA;        ## getHits(8 * 60) -&amp;gt; 3&#xA;&#xA;        ## (exclusive, inclusive)&#xA;        ## (self.hits[timestamp - 300], self.hits[timestamp])&#xA;&#xA;        items = list(sorted(self.hits.items()))&#xA;        ## (timestamp, counter)&#xA;&#xA;        ## inclusive&#xA;        ts_left = bisect.bisect_right(items, timestamp - 300, key=lambda item: item[0])&#xA;        ts_right = bisect.bisect_left(items, timestamp, key=lambda item: item[0])&#xA;&#xA;        ans = 0&#xA;&#xA;        for (timestamp, hits) in items[ts_left:]:&#xA;            ans &amp;#43;= hits&#xA;&#xA;        return ans&#xA;&#xA;&#xA;# Your HitCounter object will be instantiated and called as such:&#xA;# obj = HitCounter()&#xA;# obj.hit(timestamp)&#xA;# param_2 = obj.getHits(timestamp)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Alternatively, maintain our own sorted list of tuple:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;import bisect&#xA;&#xA;class HitCounter:&#xA;&#xA;    def __init__(self):&#xA;        self.hits = []&#xA;        ## (timestamp, hits)&#xA;&#xA;    def hit(self, timestamp: int) -&amp;gt; None:&#xA;        i = bisect.bisect_left(self.hits, timestamp, key=lambda item: item[0])&#xA;        if i &amp;lt; len(self.hits) and self.hits[i][0] == timestamp:&#xA;            self.hits[i] = (timestamp, self.hits[i][1] &amp;#43; 1)&#xA;        else:&#xA;            self.hits.insert(i, (timestamp, 1))&#xA;&#xA;    def getHits(self, timestamp: int) -&amp;gt; int:&#xA;        ## 3 * 60 -&amp;gt; 2&#xA;        ## 4 * 60 -&amp;gt; 1&#xA;        ## getHits(8 * 60) -&amp;gt; 3&#xA;&#xA;        ## (exclusive, inclusive)&#xA;        ## (self.hits[timestamp - 300], self.hits[timestamp])&#xA;&#xA;        ## (timestamp, counter)&#xA;&#xA;        ## inclusive&#xA;        ts_left = bisect.bisect_right(self.hits, timestamp - 300, key=lambda item: item[0])&#xA;        ts_right = bisect.bisect_left(self.hits, timestamp, key=lambda item: item[0])&#xA;&#xA;        ans = 0&#xA;&#xA;        for (timestamp, hits) in self.hits[ts_left:]:&#xA;            ans &amp;#43;= hits&#xA;&#xA;        return ans&#xA;&#xA;&#xA;# Your HitCounter object will be instantiated and called as such:&#xA;# obj = HitCounter()&#xA;# obj.hit(timestamp)&#xA;# param_2 = obj.getHits(timestamp)&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: LeetCode #362: Design Hit Counter&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #692: Top K Frequent Words
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%23692-top-k-frequent-words/</link>
      <pubDate>Sun, 11 Jan 2026 10:26:15 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%23692-top-k-frequent-words/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/top-k-frequent-words&#34;&gt;LeetCode #692: Top K Frequent Words&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from collections import Counter, defaultdict&#xA;&#xA;class Solution:&#xA;    def topKFrequent(self, words: List[str], k: int) -&amp;gt; List[str]:&#xA;        ## return [word for (word, freq) in Counter(words).most_common()][:k]&#xA;&#xA;        by_freq = defaultdict(list)&#xA;&#xA;        for (word, freq) in Counter(words).most_common():&#xA;            by_freq[freq].append(word)&#xA;&#xA;        ans = []&#xA;&#xA;        for (freq, words) in sorted(by_freq.items(), reverse=True):&#xA;            ans.extend(sorted(words))&#xA;            if len(ans) &amp;gt;= k:&#xA;                break&#xA;&#xA;        return ans[: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: LeetCode #692: Top K Frequent Words&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #170: Two Sum III - Data structure design
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%23170-two-sum-iii-data-structure-design/</link>
      <pubDate>Sun, 11 Jan 2026 08:08:13 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%23170-two-sum-iii-data-structure-design/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/two-sum-iii---data-structure-design&#34;&gt;LeetCode #170: Two Sum III — Data structure design&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from collections import Counter&#xA;&#xA;class TwoSum:&#xA;&#xA;    def __init__(self):&#xA;        self.s = Counter()&#xA;&#xA;&#xA;    def add(self, number: int) -&amp;gt; None:&#xA;        self.s[number] &amp;#43;= 1&#xA;&#xA;&#xA;    def find(self, value: int) -&amp;gt; bool:&#xA;        for num in self.s:&#xA;            if (value - num) == num:&#xA;                if self.s[num] &amp;gt; 1:&#xA;                    return True&#xA;            elif value - num in self.s:&#xA;                return True&#xA;&#xA;        return False&#xA;&#xA;&#xA;# Your TwoSum object will be instantiated and called as such:&#xA;# obj = TwoSum()&#xA;# obj.add(number)&#xA;# param_2 = obj.find(value)&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: LeetCode #170: Two Sum III - Data structure design&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #347: Top K Frequent Elements
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%23347-top-k-frequent-elements/</link>
      <pubDate>Sat, 10 Jan 2026 02:09:59 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%23347-top-k-frequent-elements/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/top-k-frequent-elements&#34;&gt;LeetCode #347: Top K Frequent Elements&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;p&gt;With &lt;code&gt;Counter&lt;/code&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from collections import Counter&#xA;&#xA;class Solution:&#xA;    def topKFrequent(self, nums: List[int], k: int) -&amp;gt; List[int]:&#xA;        return [k for (k, v) in Counter(nums).most_common()[:k]]&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;With &lt;code&gt;defaultdict(int)&lt;/code&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from collections import defaultdict&#xA;&#xA;class Solution:&#xA;    def topKFrequent(self, nums: List[int], k: int) -&amp;gt; List[int]:&#xA;        c = defaultdict(int)&#xA;        for num in nums:&#xA;            c[num] &amp;#43;= 1&#xA;&#xA;        return [k for (k, v) in (sorted(c.items(), key=lambda item: item[1], reverse=True))[:k]]&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;With max heap:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from collections import Counter&#xA;import heapq&#xA;&#xA;class Solution:&#xA;    def topKFrequent(self, nums: List[int], k: int) -&amp;gt; List[int]:&#xA;        # c = defaultdict(int)&#xA;        # for num in nums:&#xA;        #     c[num] &amp;#43;= 1&#xA;&#xA;        # return [k for (k, v) in (sorted(c.items(), key=lambda item: item[1], reverse=True))[:k]]&#xA;&#xA;        counter = Counter(nums)&#xA;&#xA;        class Pair:&#xA;            def __init__(self, num, freq):&#xA;                self.num = num&#xA;                self.freq = freq&#xA;&#xA;            def __lt__(self, other):&#xA;                if not isinstance(other, Pair):&#xA;                    raise ValueError&#xA;                # note: &amp;gt; instead of &amp;lt; because we want the TOP elements – max heap&#xA;                return self.freq &amp;gt; other.freq&#xA;&#xA;        pq = []&#xA;&#xA;        for (num, freq) in counter.items():&#xA;            pq.append(Pair(num, freq))&#xA;&#xA;        heapq.heapify(pq)&#xA;&#xA;        ans = []&#xA;&#xA;        for _ in range(k):&#xA;            pair = heapq.heappop(pq)&#xA;            ans.append(pair.num)&#xA;&#xA;        return ans&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Simpler:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from collections import Counter&#xA;import heapq&#xA;&#xA;class Solution:&#xA;    def topKFrequent(self, nums: List[int], k: int) -&amp;gt; List[int]:&#xA;        # c = defaultdict(int)&#xA;        # for num in nums:&#xA;        #     c[num] &amp;#43;= 1&#xA;&#xA;        # return [k for (k, v) in (sorted(c.items(), key=lambda item: item[1], reverse=True))[:k]]&#xA;&#xA;        counter = Counter(nums)&#xA;&#xA;        class Pair:&#xA;            def __init__(self, num, freq):&#xA;                self.num = num&#xA;                self.freq = freq&#xA;&#xA;            def __lt__(self, other):&#xA;                if not isinstance(other, Pair):&#xA;                    raise ValueError&#xA;                return self.freq &amp;lt; other.freq&#xA;&#xA;        pq = []&#xA;&#xA;        for (num, freq) in counter.items():&#xA;            pq.append(Pair(num, freq))&#xA;&#xA;        heapq.heapify(pq)&#xA;&#xA;        ans = []&#xA;&#xA;        return [pair.num for pair in heapq.nlargest(k, pq)]&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: LeetCode #347: Top K Frequent Elements&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #694: Number of Distinct Islands
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%23694-number-of-distinct-islands/</link>
      <pubDate>Sat, 10 Jan 2026 01:50:25 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%23694-number-of-distinct-islands/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/number-of-distinct-islands&#34;&gt;LeetCode #694: Number of Distinct Islands&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;Wrong answer&lt;/strong&gt;: 696 / 760 testcases passed. Directions do not uniquely&#xA;identify shapes. Counterexample:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-plaintext&#34;&gt;1 1   1 1&#xA;1 0   0 1&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;These produce the same directions.&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from collections import deque&#xA;&#xA;class Solution:&#xA;    def numDistinctIslands(self, grid: List[List[int]]) -&amp;gt; int:&#xA;        m = len(grid)&#xA;        n = len(grid[0])&#xA;&#xA;        ans: set[str] = set()&#xA;&#xA;        def within_bounds(a, b):&#xA;            return 0 &amp;lt;= a &amp;lt; m and 0 &amp;lt;= b &amp;lt; n&#xA;&#xA;        def bfs(x, y):&#xA;            queue = deque([(x, y)])&#xA;            h = [str((0, 0))] # hash&#xA;&#xA;            while queue:&#xA;                (x, y) = queue.popleft()&#xA;&#xA;                # if not within_bounds(x, y):&#xA;                #     continue&#xA;&#xA;                ## if grid[x][y] == -1:&#xA;                ##    continue&#xA;&#xA;                grid[x][y] = -1&#xA;&#xA;                for dir in [(0, 1), (1, 0), (0, -1), (-1, 0)]:&#xA;                    neighbor = (&#xA;                        x &amp;#43; dir[0],&#xA;                        y &amp;#43; dir[1],&#xA;                    )&#xA;&#xA;                    if within_bounds(neighbor[0], neighbor[1]) and grid[neighbor[0]][neighbor[1]] == 1:&#xA;                        queue.append((neighbor[0], neighbor[1]))&#xA;                        h.append(str(dir))&#xA;&#xA;            ans.add(&amp;#39;&amp;#39;.join(h))&#xA;&#xA;&#xA;        for x, row in enumerate(grid):&#xA;            for y, cell in enumerate(row):&#xA;                if cell == 1:&#xA;                    bfs(x, y)&#xA;&#xA;        return len(ans)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Instead, store relative positions. Memory limit exceeded: 714 / 760 testcases&#xA;passed.&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from collections import deque&#xA;&#xA;class Solution:&#xA;    def numDistinctIslands(self, grid: List[List[int]]) -&amp;gt; int:&#xA;        m = len(grid)&#xA;        n = len(grid[0])&#xA;&#xA;        ans: set[str] = set()&#xA;&#xA;        def within_bounds(a, b):&#xA;            return 0 &amp;lt;= a &amp;lt; m and 0 &amp;lt;= b &amp;lt; n&#xA;&#xA;        def bfs(initial_x, initial_y):&#xA;            queue = deque([(initial_x, initial_y)])&#xA;            h = [str((0, 0))] # hash&#xA;&#xA;            while queue:&#xA;                (x, y) = queue.popleft()&#xA;&#xA;                # if not within_bounds(x, y):&#xA;                #     continue&#xA;&#xA;                ## if grid[x][y] == -1:&#xA;                ##    continue&#xA;&#xA;                grid[x][y] = -1&#xA;&#xA;                for dir in [(0, 1), (1, 0), (0, -1), (-1, 0)]:&#xA;                    neighbor = (&#xA;                        x &amp;#43; dir[0],&#xA;                        y &amp;#43; dir[1],&#xA;                    )&#xA;&#xA;                    if within_bounds(neighbor[0], neighbor[1]) and grid[neighbor[0]][neighbor[1]] == 1:&#xA;                        queue.append((neighbor[0], neighbor[1]))&#xA;                        h.append(str((neighbor[0] - initial_x, neighbor[1] - initial_y)))&#xA;&#xA;            ans.add(&amp;#39;&amp;#39;.join(h))&#xA;&#xA;&#xA;        for x, row in enumerate(grid):&#xA;            for y, cell in enumerate(row):&#xA;                if cell == 1:&#xA;                    bfs(x, y)&#xA;&#xA;        return len(ans)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;We need to mark cells as visited right away, before queueing them:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from collections import deque&#xA;&#xA;class Solution:&#xA;    def numDistinctIslands(self, grid: List[List[int]]) -&amp;gt; int:&#xA;        m = len(grid)&#xA;        n = len(grid[0])&#xA;&#xA;        ans: set[str] = set()&#xA;&#xA;        def within_bounds(a, b):&#xA;            return 0 &amp;lt;= a &amp;lt; m and 0 &amp;lt;= b &amp;lt; n&#xA;&#xA;        def bfs(initial_x, initial_y):&#xA;            queue = deque([(initial_x, initial_y)])&#xA;            h = [(0, 0)] # hash&#xA;            grid[initial_x][initial_y] = -1&#xA;&#xA;            while queue:&#xA;                (x, y) = queue.popleft()&#xA;&#xA;                # if not within_bounds(x, y):&#xA;                #     continue&#xA;&#xA;                ## if grid[x][y] == -1:&#xA;                ##    continue&#xA;&#xA;                ## grid[x][y] = -1&#xA;&#xA;                for dir in [(0, 1), (1, 0), (0, -1), (-1, 0)]:&#xA;                    neighbor = (&#xA;                        x &amp;#43; dir[0],&#xA;                        y &amp;#43; dir[1],&#xA;                    )&#xA;&#xA;                    if within_bounds(neighbor[0], neighbor[1]) and grid[neighbor[0]][neighbor[1]] == 1:&#xA;                        queue.append((neighbor[0], neighbor[1]))&#xA;                        grid[neighbor[0]][neighbor[1]] = -1&#xA;                        h.append((neighbor[0] - initial_x, neighbor[1] - initial_y))&#xA;&#xA;            ## ans.add(frozenset(h))&#xA;            ans.add(tuple(sorted(h))) # supposedly faster&#xA;&#xA;&#xA;        for x, row in enumerate(grid):&#xA;            for y, cell in enumerate(row):&#xA;                if cell == 1:&#xA;                    bfs(x, y)&#xA;&#xA;        return len(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: LeetCode #694: Number of Distinct Islands&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #1254: Number of Closed Islands
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%231254-number-of-closed-islands/</link>
      <pubDate>Sat, 10 Jan 2026 01:23:06 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%231254-number-of-closed-islands/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/number-of-closed-islands&#34;&gt;LeetCode #1254: Number of Closed Islands&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;p&gt;Recursive:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;class Solution:&#xA;    def closedIsland(self, grid: List[List[int]]) -&amp;gt; int:&#xA;        m = len(grid)&#xA;        n = len(grid[0])&#xA;&#xA;        def within_bounds(a, b):&#xA;            return 0 &amp;lt;= a &amp;lt; m and 0 &amp;lt;= b &amp;lt; n&#xA;&#xA;        def dfs(x, y, poisoned=False):&#xA;            if not within_bounds(x, y):&#xA;                return True&#xA;&#xA;            if grid[x][y] in [-1, 1]:&#xA;                return False&#xA;&#xA;            grid[x][y] = -1&#xA;&#xA;            for dir in [(0, 1), (1, 0), (0, -1), (-1, 0)]:&#xA;                neighbor = (&#xA;                    x &amp;#43; dir[0],&#xA;                    y &amp;#43; dir[1],&#xA;                )&#xA;                if dfs(neighbor[0], neighbor[1]):&#xA;                    poisoned = True&#xA;&#xA;            return poisoned&#xA;&#xA;        ans = 0&#xA;&#xA;        for x, row in enumerate(grid):&#xA;            for y, cell in enumerate(row):&#xA;                if grid[x][y] == 0:&#xA;                    if not dfs(x, y):&#xA;                        ans &amp;#43;= 1&#xA;&#xA;        return ans&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Iterative:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;class Solution:&#xA;    def closedIsland(self, grid: List[List[int]]) -&amp;gt; int:&#xA;        m = len(grid)&#xA;        n = len(grid[0])&#xA;&#xA;        def within_bounds(a, b):&#xA;            return 0 &amp;lt;= a &amp;lt; m and 0 &amp;lt;= b &amp;lt; n&#xA;&#xA;        def dfs(x, y):&#xA;            valid = True&#xA;&#xA;            stack = [(x, y)]&#xA;&#xA;            while stack:&#xA;                (x, y) = stack.pop()&#xA;&#xA;                if not within_bounds(x, y):&#xA;                    valid = False&#xA;                    continue&#xA;&#xA;                if grid[x][y] in [-1, 1]:&#xA;                    continue&#xA;&#xA;                ## assert grid[x][y] == 0&#xA;                grid[x][y] = -1&#xA;&#xA;                for dir in [(0, 1), (1, 0), (0, -1), (-1, 0)]:&#xA;                    neighbor = (&#xA;                        x &amp;#43; dir[0],&#xA;                        y &amp;#43; dir[1],&#xA;                    )&#xA;                    ## if not within_bounds(neighbor[0], neighbor[1]):&#xA;                    ##     return False&#xA;                    stack.append((neighbor[0], neighbor[1]))&#xA;&#xA;            return valid&#xA;&#xA;        ans = 0&#xA;&#xA;        for x, row in enumerate(grid):&#xA;            for y, cell in enumerate(row):&#xA;                if grid[x][y] == 0:&#xA;                    ans &amp;#43;= dfs(x, y)&#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: LeetCode #1254: Number of Closed Islands&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #200: Number of Islands
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%23200-number-of-islands/</link>
      <pubDate>Sat, 10 Jan 2026 00:24:09 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%23200-number-of-islands/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/number-of-islands&#34;&gt;LeetCode #200: Number of Islands&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;class Solution:&#xA;    def numIslands(self, grid: List[List[str]]) -&amp;gt; int:&#xA;        m = len(grid)&#xA;        n = len(grid[0])&#xA;&#xA;        def within_bounds(a, b):&#xA;            return 0 &amp;lt;= a &amp;lt; m and 0 &amp;lt;= b &amp;lt; n&#xA;&#xA;        def dfs(x, y):&#xA;            if not within_bounds(x, y):&#xA;                return&#xA;&#xA;            if grid[x][y] in [&amp;#34;-1&amp;#34;, &amp;#34;0&amp;#34;]:&#xA;                return&#xA;&#xA;            grid[x][y] = &amp;#34;-1&amp;#34;&#xA;&#xA;            for dir in [(0, 1), (1, 0), (0, -1), (-1, 0)]:&#xA;                neighbor = (&#xA;                    x &amp;#43; dir[0],&#xA;                    y &amp;#43; dir[1],&#xA;                )&#xA;                dfs(neighbor[0], neighbor[1])&#xA;&#xA;        ans = 0&#xA;&#xA;        for x, row in enumerate(grid):&#xA;            for y, cell in enumerate(row):&#xA;                if grid[x][y] == &amp;#34;1&amp;#34;:&#xA;                    dfs(x, y)&#xA;                    ans &amp;#43;= 1&#xA;&#xA;        return ans&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Pass tuples around:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;class Solution:&#xA;    def numIslands(self, grid: List[List[str]]) -&amp;gt; int:&#xA;        m = len(grid)&#xA;        n = len(grid[0])&#xA;&#xA;        def within_bounds(coords):&#xA;            (a, b) = coords&#xA;            return 0 &amp;lt;= a &amp;lt; m and 0 &amp;lt;= b &amp;lt; n&#xA;&#xA;        def dfs(coords):&#xA;            (x, y) = coords&#xA;            if not within_bounds(coords):&#xA;                return&#xA;&#xA;            if grid[x][y] in [&amp;#34;-1&amp;#34;, &amp;#34;0&amp;#34;]:&#xA;                return&#xA;&#xA;            grid[x][y] = &amp;#34;-1&amp;#34;&#xA;&#xA;            for dir in [(0, 1), (1, 0), (0, -1), (-1, 0)]:&#xA;                neighbor = (&#xA;                    x &amp;#43; dir[0],&#xA;                    y &amp;#43; dir[1],&#xA;                )&#xA;                dfs(neighbor)&#xA;&#xA;        ans = 0&#xA;&#xA;        for x, row in enumerate(grid):&#xA;            for y, cell in enumerate(row):&#xA;                if grid[x][y] == &amp;#34;1&amp;#34;:&#xA;                    dfs((x, y))&#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: LeetCode #200: Number of Islands&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #1244: Design A Leaderboard
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%231244-design-a-leaderboard/</link>
      <pubDate>Sat, 10 Jan 2026 00:12:45 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%231244-design-a-leaderboard/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/design-a-leaderboard&#34;&gt;LeetCode #1244: Design A Leaderboard&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;p&gt;With Counter:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from collections import Counter&#xA;&#xA;class Leaderboard:&#xA;&#xA;    def __init__(self):&#xA;        self.counter = Counter()&#xA;&#xA;    def addScore(self, playerId: int, score: int) -&amp;gt; None:&#xA;        self.counter[playerId] &amp;#43;= score&#xA;&#xA;    def top(self, K: int) -&amp;gt; int:&#xA;        return sum(v for (k, v) in self.counter.most_common()[:K])&#xA;&#xA;    def reset(self, playerId: int) -&amp;gt; None:&#xA;        del self.counter[playerId]&#xA;        ## self.counter[playerId] = 0&#xA;&#xA;&#xA;# Your Leaderboard object will be instantiated and called as such:&#xA;# obj = Leaderboard()&#xA;# obj.addScore(playerId,score)&#xA;# param_2 = obj.top(K)&#xA;# obj.reset(playerId)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;With priority queue (heap):&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from collections import defaultdict&#xA;&#xA;class Leaderboard:&#xA;&#xA;    def __init__(self):&#xA;        self.d = defaultdict(int)&#xA;&#xA;    def addScore(self, playerId: int, score: int) -&amp;gt; None:&#xA;        self.d[playerId] &amp;#43;= score&#xA;&#xA;    def top(self, K: int) -&amp;gt; int:&#xA;        pq = []&#xA;&#xA;        for score in self.d.values():&#xA;            heapq.heappush(pq, score)&#xA;            if len(pq) &amp;gt; K:&#xA;                heapq.heappop(pq)&#xA;&#xA;        return sum(pq)&#xA;&#xA;    def reset(self, playerId: int) -&amp;gt; None:&#xA;        del self.d[playerId]&#xA;&#xA;&#xA;# Your Leaderboard object will be instantiated and called as such:&#xA;# obj = Leaderboard()&#xA;# obj.addScore(playerId,score)&#xA;# param_2 = obj.top(K)&#xA;# obj.reset(playerId)&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: LeetCode #1244: Design A Leaderboard&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #2490: Circular Sentence
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%232490-circular-sentence/</link>
      <pubDate>Sat, 10 Jan 2026 00:05:44 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%232490-circular-sentence/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/circular-sentence&#34;&gt;LeetCode #2490: Circular Sentence&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;class Solution:&#xA;    def isCircularSentence(self, sentence: str) -&amp;gt; bool:&#xA;        # case sensitive&#xA;&#xA;        words = sentence.strip().split(&amp;#39; &amp;#39;)&#xA;&#xA;        for word1, word2 in zip(&#xA;            words,&#xA;            words[1:] &amp;#43; [words[0]]&#xA;        ):&#xA;            if word1[-1] != word2[0]:&#xA;                return False&#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: LeetCode #2490: Circular Sentence&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #286: Walls and Gates
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%23286-walls-and-gates/</link>
      <pubDate>Fri, 09 Jan 2026 23:57:56 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%23286-walls-and-gates/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/walls-and-gates&#34;&gt;LeetCode #286: Walls and Gates&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;p&gt;Level order traversal:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from collections import deque&#xA;&#xA;class Solution:&#xA;    def wallsAndGates(self, rooms: List[List[int]]) -&amp;gt; None:&#xA;        &amp;#34;&amp;#34;&amp;#34;&#xA;        Do not return anything, modify rooms in-place instead.&#xA;        &amp;#34;&amp;#34;&amp;#34;&#xA;        m = len(rooms)&#xA;        n = len(rooms[0])&#xA;&#xA;        INF = 2 ** 31 - 1&#xA;&#xA;        def bfs(x, y):&#xA;            queue = deque([(x, y)])&#xA;&#xA;            dirs = [&#xA;                (0, 1),&#xA;                (1, 0),&#xA;                (0, -1),&#xA;                (-1, 0),&#xA;            ]&#xA;&#xA;            def within_bounds(a, b):&#xA;                return 0 &amp;lt;= a &amp;lt; m and 0 &amp;lt;= b &amp;lt; n&#xA;&#xA;            level = 0&#xA;            while queue:&#xA;                level &amp;#43;= 1&#xA;                num_elements = len(queue)&#xA;&#xA;                for _ in range(num_elements):&#xA;                    (x, y) = queue.popleft()&#xA;&#xA;                    for dir in dirs:&#xA;                        neighbor = (&#xA;                            x &amp;#43; dir[0],&#xA;                            y &amp;#43; dir[1],&#xA;                        )&#xA;&#xA;                        if within_bounds(neighbor[0], neighbor[1]):&#xA;                            value = rooms[neighbor[0]][neighbor[1]]&#xA;&#xA;                            if value == 0 or value == -1:&#xA;                                continue&#xA;&#xA;                            if level &amp;lt; value:&#xA;                                rooms[neighbor[0]][neighbor[1]] = level&#xA;                                queue.append((neighbor[0], neighbor[1]))&#xA;&#xA;&#xA;&#xA;        for x, row in enumerate(rooms):&#xA;            for y, cell in enumerate(row):&#xA;                if cell == 0:&#xA;                    bfs(x, y)&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: LeetCode #286: Walls and Gates&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #1472: Design Browser History
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%231472-design-browser-history/</link>
      <pubDate>Fri, 09 Jan 2026 22:07:44 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%231472-design-browser-history/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/design-browser-history&#34;&gt;LeetCode #1472: Design Browser History&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;class BrowserHistory:&#xA;&#xA;    def __init__(self, homepage: str):&#xA;        self.history = [homepage]&#xA;        self.pos = 0&#xA;&#xA;        # example.com &amp;lt;-&#xA;        #   example.org&#xA;        #     foo.example.com &amp;lt;=&#xA;&#xA;&#xA;    def visit(self, url: str) -&amp;gt; None:&#xA;        if self.pos == (len(self.history) - 1):&#xA;            self.history.append(url)&#xA;            self.pos &amp;#43;= 1&#xA;        else:&#xA;            self.pos &amp;#43;= 1&#xA;            self.history[self.pos] = url&#xA;            self.history = self.history[:self.pos &amp;#43; 1]&#xA;&#xA;&#xA;    def back(self, steps: int) -&amp;gt; str:&#xA;        steps = min(steps, self.pos)&#xA;        self.pos -= steps&#xA;        return self.history[self.pos]&#xA;&#xA;&#xA;    def forward(self, steps: int) -&amp;gt; str:&#xA;        steps = min(steps, len(self.history) - 1 - self.pos)&#xA;        self.pos &amp;#43;= steps&#xA;        return self.history[self.pos]&#xA;&#xA;&#xA;# Your BrowserHistory object will be instantiated and called as such:&#xA;# obj = BrowserHistory(homepage)&#xA;# obj.visit(url)&#xA;# param_2 = obj.back(steps)&#xA;# param_3 = obj.forward(steps)&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: LeetCode #1472: Design Browser History&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #1188: Design Bounded Blocking Queue
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%231188-design-bounded-blocking-queue/</link>
      <pubDate>Fri, 09 Jan 2026 21:58:29 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%231188-design-bounded-blocking-queue/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/design-bounded-blocking-queue&#34;&gt;LeetCode #1188: Design Bounded Blocking Queue&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from collections import deque&#xA;from threading import Lock, Condition&#xA;&#xA;class BoundedBlockingQueue(object):&#xA;&#xA;    def __init__(self, capacity: int):&#xA;        self.q = deque()&#xA;        self.capacity = capacity&#xA;        self.lock = Lock()&#xA;        self.not_full = Condition(self.lock)&#xA;        self.not_empty = Condition(self.lock)&#xA;&#xA;&#xA;    def enqueue(self, element: int) -&amp;gt; None:&#xA;        self.not_full.acquire()&#xA;&#xA;        while self.size() == self.capacity:&#xA;            self.not_full.wait()&#xA;&#xA;        self.q.append(element)&#xA;        self.not_empty.notify()&#xA;        self.not_full.release()&#xA;&#xA;    def dequeue(self) -&amp;gt; int:&#xA;        self.not_empty.acquire()&#xA;&#xA;        while self.size() == 0:&#xA;            self.not_empty.wait()&#xA;&#xA;        element = self.q.popleft()&#xA;        self.not_full.notify()&#xA;        self.not_empty.release()&#xA;&#xA;        return element&#xA;&#xA;&#xA;    def size(self) -&amp;gt; int:&#xA;        return len(self.q)&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: LeetCode #1188: Design Bounded Blocking Queue&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #1756: Design Most Recently Used Queue
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%231756-design-most-recently-used-queue/</link>
      <pubDate>Fri, 09 Jan 2026 21:53:51 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%231756-design-most-recently-used-queue/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/design-most-recently-used-queue&#34;&gt;LeetCode #1756: Design Most Recently Used Queue&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;class MRUQueue:&#xA;&#xA;    def __init__(self, n: int):&#xA;        self.arr = list(range(1, n &amp;#43; 1))&#xA;&#xA;    def fetch(self, k: int) -&amp;gt; int:&#xA;        # 0-indexed&#xA;        k -= 1&#xA;&#xA;        assert 0 &amp;lt;= k &amp;lt; len(self.arr)&#xA;&#xA;        el = self.arr[k]&#xA;&#xA;        del self.arr[k]&#xA;        self.arr.append(el)&#xA;&#xA;        return el&#xA;&#xA;# Your MRUQueue object will be instantiated and called as such:&#xA;# obj = MRUQueue(n)&#xA;# param_1 = obj.fetch(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: LeetCode #1756: Design Most Recently Used Queue&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #622: Design Circular Queue
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%23622-design-circular-queue/</link>
      <pubDate>Fri, 09 Jan 2026 04:35:00 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%23622-design-circular-queue/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/design-circular-queue&#34;&gt;LeetCode #622: Design Circular Queue&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;class MyCircularQueue:&#xA;&#xA;    def __init__(self, k: int):&#xA;        self.q = [None] * k&#xA;        self.k = k&#xA;&#xA;        self.start = 0&#xA;        self.end = 0&#xA;        self.count = 0&#xA;&#xA;    ## -- queue&#xA;&#xA;    ## .   .  .&#xA;    ## se&#xA;&#xA;    ## 1   .  .&#xA;    ## s      e&#xA;&#xA;    ## 1   .  2&#xA;    ## s   e&#xA;&#xA;    ## --  dequeue&#xA;&#xA;    ## 1 . 2&#xA;    ## s e&#xA;&#xA;    ## . . 2&#xA;    ##   e s&#xA;&#xA;    def enQueue(self, value: int) -&amp;gt; bool:&#xA;        if self.isFull():&#xA;            return False&#xA;&#xA;        self.q[self.end] = value&#xA;        self.end = (self.end - 1) % self.k&#xA;        self.count &amp;#43;= 1&#xA;        return True&#xA;&#xA;&#xA;    def deQueue(self) -&amp;gt; bool:&#xA;        if self.isEmpty():&#xA;            return False&#xA;&#xA;        self.start = (self.start - 1) % self.k&#xA;        self.count -= 1&#xA;        return True&#xA;&#xA;&#xA;    def Front(self) -&amp;gt; int:&#xA;        if self.isEmpty():&#xA;            return -1&#xA;&#xA;        return self.q[self.start]&#xA;&#xA;&#xA;    def Rear(self) -&amp;gt; int:&#xA;        if self.isEmpty():&#xA;            return -1&#xA;&#xA;        return self.q[(self.end &amp;#43; 1) % self.k]&#xA;&#xA;&#xA;    def isEmpty(self) -&amp;gt; bool:&#xA;        return self.count == 0&#xA;&#xA;&#xA;    def isFull(self) -&amp;gt; bool:&#xA;        return self.count == self.k&#xA;&#xA;&#xA;# Your MyCircularQueue object will be instantiated and called as such:&#xA;# obj = MyCircularQueue(k)&#xA;# param_1 = obj.enQueue(value)&#xA;# param_2 = obj.deQueue()&#xA;# param_3 = obj.Front()&#xA;# param_4 = obj.Rear()&#xA;# param_5 = obj.isEmpty()&#xA;# param_6 = obj.isFull()&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: LeetCode #622: Design Circular Queue&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #641: Design Circular Deque
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%23641-design-circular-deque/</link>
      <pubDate>Fri, 09 Jan 2026 04:20:38 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%23641-design-circular-deque/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/design-circular-deque&#34;&gt;LeetCode #641: Design Circular Deque&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;class MyCircularDeque:&#xA;&#xA;    def __init__(self, k: int):&#xA;        self.q = [None] * k&#xA;        self.k = k&#xA;&#xA;        self.start = 0&#xA;        self.end = 0&#xA;        self.count = 0&#xA;&#xA;        ##  .  . . .&#xA;        ## se&#xA;&#xA;        ## . 1 . .&#xA;        ## s   e&#xA;&#xA;        ## . 2 3 .&#xA;        ## s     e&#xA;&#xA;    def insertFront(self, value: int) -&amp;gt; bool:&#xA;        if self.isEmpty():&#xA;            self.q[self.start] = value&#xA;            self.start = (self.start - 1) % self.k&#xA;            self.end = (self.end &amp;#43; 1) % self.k&#xA;            self.count &amp;#43;= 1&#xA;            return True&#xA;&#xA;        if self.isFull():&#xA;            return False&#xA;&#xA;        self.q[self.start] = value&#xA;        self.start = (self.start - 1) % self.k&#xA;        self.count &amp;#43;= 1&#xA;        return True&#xA;&#xA;&#xA;    def insertLast(self, value: int) -&amp;gt; bool:&#xA;        if self.isEmpty():&#xA;            self.q[self.end] = value&#xA;            self.start = (self.start - 1) % self.k&#xA;            self.end = (self.end &amp;#43; 1) % self.k&#xA;            self.count &amp;#43;= 1&#xA;            return True&#xA;&#xA;        if self.isFull():&#xA;            return False&#xA;&#xA;        self.q[self.end] = value&#xA;        self.end = (self.end &amp;#43; 1) % self.k&#xA;        self.count &amp;#43;= 1&#xA;        return True&#xA;&#xA;&#xA;    def deleteFront(self) -&amp;gt; bool:&#xA;        if self.isEmpty():&#xA;            return False&#xA;&#xA;        self.start = (self.start &amp;#43; 1) % self.k&#xA;        self.count -= 1&#xA;        ## self.q[self.start] = None&#xA;&#xA;        if self.count == 0:&#xA;            self.end = self.start&#xA;&#xA;        return True&#xA;&#xA;&#xA;    def deleteLast(self) -&amp;gt; bool:&#xA;        if self.isEmpty():&#xA;            return False&#xA;&#xA;        self.end = (self.end - 1) % self.k&#xA;        self.count -= 1&#xA;        ## self.q[self.end] = None&#xA;&#xA;        if self.count == 0:&#xA;            self.start = self.end&#xA;&#xA;        return True&#xA;&#xA;    def getFront(self) -&amp;gt; int:&#xA;        if self.isEmpty():&#xA;            return -1&#xA;        return self.q[(self.start &amp;#43; 1) % self.k]&#xA;&#xA;&#xA;    def getRear(self) -&amp;gt; int:&#xA;        if self.isEmpty():&#xA;            return -1&#xA;        return self.q[self.end - 1 % self.k]&#xA;&#xA;&#xA;    def isEmpty(self) -&amp;gt; bool:&#xA;        return self.count == 0&#xA;&#xA;&#xA;    def isFull(self) -&amp;gt; bool:&#xA;        return self.count == self.k&#xA;&#xA;&#xA;&#xA;# Your MyCircularDeque object will be instantiated and called as such:&#xA;# obj = MyCircularDeque(k)&#xA;# param_1 = obj.insertFront(value)&#xA;# param_2 = obj.insertLast(value)&#xA;# param_3 = obj.deleteFront()&#xA;# param_4 = obj.deleteLast()&#xA;# param_5 = obj.getFront()&#xA;# param_6 = obj.getRear()&#xA;# param_7 = obj.isEmpty()&#xA;# param_8 = obj.isFull()&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: LeetCode #641: Design Circular Deque&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #211: Design Add and Search Words Data Structure
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%23211-design-add-and-search-words-data-structure/</link>
      <pubDate>Thu, 08 Jan 2026 03:43:06 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%23211-design-add-and-search-words-data-structure/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/design-add-and-search-words-data-structure&#34;&gt;LeetCode #211: Design Add and Search Words Data Structure&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from collections import defaultdict&#xA;&#xA;class Node:&#xA;    def __init__(self):&#xA;        self.children = {}&#xA;        self.end = False&#xA;&#xA;class WordDictionary:&#xA;&#xA;    def __init__(self):&#xA;        self.root = Node()&#xA;&#xA;    def addWord(self, word: str) -&amp;gt; None:&#xA;        node = self.root&#xA;        ## &amp;#34;hello&amp;#34;&#xA;        for c in word:&#xA;            if c not in node.children:&#xA;                node.children[c] = Node()&#xA;            node = node.children[c]&#xA;        node.end = True&#xA;&#xA;    def search(self, word: str) -&amp;gt; bool:&#xA;        def node_search(node, word: str) -&amp;gt; bool:&#xA;            for i, c in enumerate(word):&#xA;                if c == &amp;#39;.&amp;#39;:&#xA;                    for child in node.children.values():&#xA;                        if node_search(child, word[i &amp;#43; 1:]):&#xA;                            return True&#xA;                    return False&#xA;&#xA;                if c not in node.children:&#xA;                    return False&#xA;                node = node.children[c]&#xA;&#xA;            return node.end&#xA;&#xA;        return node_search(self.root, word)&#xA;&#xA;&#xA;# Your WordDictionary object will be instantiated and called as such:&#xA;# obj = WordDictionary()&#xA;# obj.addWord(word)&#xA;# param_2 = obj.search(word)&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: LeetCode #211: Design Add and Search Words Data Structure&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #2965: Find Missing and Repeated Values
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%232965-find-missing-and-repeated-values/</link>
      <pubDate>Thu, 08 Jan 2026 00:24:02 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%232965-find-missing-and-repeated-values/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/find-missing-and-repeated-values&#34;&gt;LeetCode #2965: Find Missing and Repeated Values&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;p&gt;Two sets:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;class Solution:&#xA;    def findMissingAndRepeatedValues(self, grid: List[List[int]]) -&amp;gt; List[int]:&#xA;        seen = set()&#xA;        numbers = set(range(1, len(grid) ** 2 &amp;#43; 1))&#xA;&#xA;        a = b = None&#xA;&#xA;        for row in grid:&#xA;            for num in row:&#xA;                if num in seen:&#xA;                    a = num&#xA;                    continue&#xA;                seen.add(num)&#xA;                numbers.discard(num)&#xA;&#xA;        assert len(numbers) == 1&#xA;        b = numbers.pop()&#xA;&#xA;        return [a, b]&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: LeetCode #2965: Find Missing and Repeated Values&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #1033: Moving Stones Until Consecutive
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%231033-moving-stones-until-consecutive/</link>
      <pubDate>Wed, 07 Jan 2026 23:53:36 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%231033-moving-stones-until-consecutive/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/moving-stones-until-consecutive&#34;&gt;LeetCode #1033: Moving Stones Until Consecutive&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;p&gt;Convoluted:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;class Solution:&#xA;    def numMovesStones(self, a: int, b: int, c: int) -&amp;gt; List[int]:&#xA;        (a, b, c) = sorted([a, b, c])&#xA;&#xA;        ans_max = ((c - b) - 1) &amp;#43; ((b - a) - 1)&#xA;&#xA;        ans_min = 0&#xA;        while not ((b - a) == 1 and (c - b) == 1): ## 3 4 5&#xA;            ## 3 10 50&#xA;&#xA;            if (b - a) == 1 or (c - b) == 1:&#xA;                ans_min &amp;#43;= 1&#xA;                break&#xA;&#xA;            # find smallest interval with a gap&#xA;            ## (3, 10)&#xA;            if (c - b == 1) or (b - a) &amp;lt;= (c - b): # left&#xA;                e1 = a&#xA;                e2 = b&#xA;                ms  = c&#xA;            else: ## right&#xA;                e1 = b&#xA;                e2 = c&#xA;                ms = a&#xA;&#xA;            # move the other stone (ms) to the middle of (e1, e2)&#xA;            ms = (e1 &amp;#43; e2) // 2&#xA;&#xA;            # update a, b, c; then keep going&#xA;            a, b, c = e1, ms, e2&#xA;            assert a &amp;lt; b&#xA;            assert b &amp;lt; c&#xA;&#xA;            ans_min &amp;#43;= 1&#xA;            if ans_min == 2:&#xA;                break&#xA;&#xA;        return [ans_min, ans_max]&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;&lt;code&gt;min_ans&lt;/code&gt; will be up to 2. That will significantly simplify everything!&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;class Solution:&#xA;    def numMovesStones(self, a: int, b: int, c: int) -&amp;gt; List[int]:&#xA;        (a, b, c) = sorted([a, b, c])&#xA;&#xA;        ans_max = ((c - b) - 1) &amp;#43; ((b - a) - 1)&#xA;&#xA;        ans_min = 0&#xA;        if ((b - a) == 1 and (c - b) == 1): ## 3 4 5&#xA;            ans_min = 0&#xA;        elif (b - a) in [1, 2] or (c - b) in [1, 2]: ## 3 4 6&#xA;            ans_min = 1&#xA;        else:&#xA;            ans_min = 2&#xA;&#xA;        return [ans_min, ans_max]&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: LeetCode #1033: Moving Stones Until Consecutive&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #771: Jewels and Stones
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%23771-jewels-and-stones/</link>
      <pubDate>Wed, 07 Jan 2026 01:18:30 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%23771-jewels-and-stones/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/jewels-and-stones&#34;&gt;LeetCode #771: Jewels and Stones&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;p&gt;Cozy:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;class Solution:&#xA;    def numJewelsInStones(self, jewels: str, stones: str) -&amp;gt; int:&#xA;        s = set(jewels)&#xA;&#xA;        ans = 0&#xA;&#xA;        for stone in stones:&#xA;            if stone in s:&#xA;                ans &amp;#43;= 1&#xA;&#xA;        return ans&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;One-liner:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;class Solution:&#xA;    def numJewelsInStones(self, jewels: str, stones: str) -&amp;gt; int:&#xA;        s = set(jewels)&#xA;        return sum(stone in s for stone in stones)&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: LeetCode #771: Jewels and Stones&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #652: Find Duplicate Subtrees
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%23652-find-duplicate-subtrees/</link>
      <pubDate>Wed, 07 Jan 2026 00:49:44 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%23652-find-duplicate-subtrees/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/find-duplicate-subtrees&#34;&gt;LeetCode #652: Find Duplicate Subtrees&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;p&gt;Original, 119 / 175 testcases passed:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from collections import defaultdict&#xA;&#xA;# Definition for a binary tree node.&#xA;# class TreeNode:&#xA;#     def __init__(self, val=0, left=None, right=None):&#xA;#         self.val = val&#xA;#         self.left = left&#xA;#         self.right = right&#xA;class Solution:&#xA;    def findDuplicateSubtrees(self, root: Optional[TreeNode]) -&amp;gt; List[Optional[TreeNode]]:&#xA;        mappings = defaultdict(list)&#xA;&#xA;        def dfs(node):&#xA;            if not node:&#xA;                return&#xA;&#xA;            mappings[node.val].append(node)&#xA;&#xA;            dfs(node.left)&#xA;            dfs(node.right)&#xA;&#xA;        dfs(root)&#xA;&#xA;        ans = set()&#xA;&#xA;        def duplicate_subtree(node1, node2):&#xA;            if not node1 and not node2:&#xA;                return True&#xA;&#xA;            if not node1 and node2:&#xA;                return False&#xA;&#xA;            if node1 and not node2:&#xA;                return False&#xA;&#xA;            return node1.val == node2.val and duplicate_subtree(node1.left, node2.left) and duplicate_subtree(node1.right, node2.right)&#xA;&#xA;        for nodes in mappings.values():&#xA;            for i, node1 in enumerate(nodes[:-1]):&#xA;                for node2 in nodes[i &amp;#43; 1:]:&#xA;                    if duplicate_subtree(node1, node2):&#xA;                        ans.add(node1)&#xA;&#xA;        return list(ans)&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;With proper serialization:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-bash&#34;&gt;from collections import Counter&#xA;&#xA;class Solution:&#xA;    def findDuplicateSubtrees(self, root: Optional[TreeNode]) -&amp;gt; List[Optional[TreeNode]]:&#xA;        # serial -&amp;gt; count&#xA;        seen = Counter()&#xA;&#xA;        # serial -&amp;gt; node&#xA;        node_from_serial = {}&#xA;&#xA;        def dfs(node) -&amp;gt; str:&#xA;            &amp;#34;&amp;#34;&amp;#34;Return the serialized representation of the node.&amp;#34;&amp;#34;&amp;#34;&#xA;            if not node:&#xA;                return &amp;#34;null&amp;#34;&#xA;&#xA;            s = f&amp;#34;{node.val},{dfs(node.left)},{dfs(node.right)}&amp;#34;&#xA;&#xA;            node_from_serial[s] = node&#xA;&#xA;            seen[s] &amp;#43;= 1&#xA;&#xA;            return s&#xA;&#xA;        dfs(root)&#xA;&#xA;        ans = []&#xA;&#xA;        for (serial, count) in seen.items():&#xA;            if count &amp;gt;= 2:&#xA;                ans.append(node_from_serial[serial])&#xA;&#xA;        return ans&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Optimized:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from collections import Counter&#xA;&#xA;class Solution:&#xA;    def findDuplicateSubtrees(self, root: Optional[TreeNode]) -&amp;gt; List[Optional[TreeNode]]:&#xA;        # serial -&amp;gt; count&#xA;        seen = Counter()&#xA;&#xA;        ans = []&#xA;&#xA;        def dfs(node) -&amp;gt; str:&#xA;            &amp;#34;&amp;#34;&amp;#34;Return the serialized representation of the node.&amp;#34;&amp;#34;&amp;#34;&#xA;            if not node:&#xA;                return &amp;#34;null&amp;#34;&#xA;&#xA;            s = f&amp;#34;{node.val},{dfs(node.left)},{dfs(node.right)}&amp;#34;&#xA;&#xA;            seen[s] &amp;#43;= 1&#xA;&#xA;            if seen[s] == 2:&#xA;                ans.append(node)&#xA;&#xA;            return s&#xA;&#xA;        dfs(root)&#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: LeetCode #652: Find Duplicate Subtrees&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #36: Valid Sudoku
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%2336-valid-sudoku/</link>
      <pubDate>Wed, 07 Jan 2026 00:10:01 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%2336-valid-sudoku/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/valid-sudoku&#34;&gt;LeetCode #36: Valid Sudoku&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;class Solution:&#xA;    def isValidSudoku(self, board: List[List[str]]) -&amp;gt; bool:&#xA;        m = len(board)&#xA;        n = len(board[0])&#xA;&#xA;        def is_valid(a):&#xA;            a = [el for el in a if el != &amp;#34;.&amp;#34;]&#xA;            ## [1, 2, 2] -&amp;gt; 3&#xA;            ## {1, 2}    -&amp;gt; 2&#xA;            ##&#xA;            ## [1, 2]&#xA;            ## {1, 2}&#xA;            return len(a) - len(set(a)) == 0&#xA;&#xA;        for row in board:&#xA;            if not is_valid(row):&#xA;                return False&#xA;&#xA;        for col in zip(*board):&#xA;            if not is_valid(col):&#xA;                return False&#xA;&#xA;        subgrids_i = [&#xA;            [0, 0],&#xA;            [0, 3],&#xA;            [0, 6],&#xA;            [3, 0],&#xA;            [3, 3],&#xA;            [3, 6],&#xA;            [6, 0],&#xA;            [6, 3],&#xA;            [6, 6],&#xA;        ]&#xA;&#xA;        for subgrid_i in subgrids_i:&#xA;            subgrid = []&#xA;&#xA;            for x in range(3):&#xA;                for y in range(3):&#xA;                    ix = subgrid_i[0] &amp;#43; x&#xA;                    iy = subgrid_i[1] &amp;#43; y&#xA;                    subgrid.append(board[ix][iy])&#xA;&#xA;            if not is_valid(subgrid):&#xA;                return False&#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: LeetCode #36: Valid Sudoku&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #49: Group Anagrams
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%2349-group-anagrams/</link>
      <pubDate>Tue, 06 Jan 2026 23:55:13 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%2349-group-anagrams/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/group-anagrams&#34;&gt;LeetCode #49: Group Anagrams&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from collections import defaultdict&#xA;from collections.abc import Hashable&#xA;&#xA;def is_hashable(obj) -&amp;gt; bool:&#xA;    return isinstance(obj, Hashable)&#xA;&#xA;class Solution:&#xA;    def groupAnagrams(self, strs: List[str]) -&amp;gt; List[List[str]]:&#xA;        seen = defaultdict(list)&#xA;&#xA;        for s in strs:&#xA;            cs = &amp;#39;&amp;#39;.join(sorted(s)) # canonical&#xA;            seen[cs].append(s)&#xA;&#xA;        return list(seen.values())&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: LeetCode #49: Group Anagrams&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #359: Logger Rate Limiter
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%23359-logger-rate-limiter/</link>
      <pubDate>Tue, 06 Jan 2026 23:45:02 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%23359-logger-rate-limiter/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/logger-rate-limiter&#34;&gt;LeetCode #359: Logger Rate Limiter&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;class Logger:&#xA;&#xA;    def __init__(self):&#xA;        self.log = {}&#xA;&#xA;&#xA;    def shouldPrintMessage(self, timestamp: int, message: str) -&amp;gt; bool:&#xA;        if message not in self.log:&#xA;            self.log[message] = timestamp&#xA;            return True&#xA;&#xA;        if timestamp - self.log[message] &amp;lt; 10:&#xA;            return False&#xA;&#xA;        self.log[message] = timestamp&#xA;        return True&#xA;&#xA;&#xA;&#xA;# Your Logger object will be instantiated and called as such:&#xA;# obj = Logger()&#xA;# param_1 = obj.shouldPrintMessage(timestamp,message)&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: LeetCode #359: Logger Rate Limiter&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #2215: Find the Difference of Two Arrays
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%232215-find-the-difference-of-two-arrays/</link>
      <pubDate>Tue, 06 Jan 2026 23:30:46 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%232215-find-the-difference-of-two-arrays/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/find-the-difference-of-two-arrays&#34;&gt;LeetCode #2215: Find the Difference of Two Arrays&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;class Solution:&#xA;    def findDifference(self, nums1: List[int], nums2: List[int]) -&amp;gt; List[List[int]]:&#xA;        s1 = set(nums1)&#xA;        s2 = set(nums2)&#xA;&#xA;        return [list(s1 - s2), list(s2 - s1)]&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: LeetCode #2215: Find the Difference of Two Arrays&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #219: Contains Duplicate II
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%23219-contains-duplicate-ii/</link>
      <pubDate>Tue, 06 Jan 2026 21:10:50 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%23219-contains-duplicate-ii/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/contains-duplicate-ii&#34;&gt;LeetCode #219: Contains Duplicate II&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from collections import defaultdict&#xA;&#xA;class Solution:&#xA;    def containsNearbyDuplicate(self, nums: List[int], k: int) -&amp;gt; bool:&#xA;        d = defaultdict(list)&#xA;        for i, num in enumerate(nums):&#xA;            d[num].append(i)&#xA;&#xA;        for values in d.values():&#xA;            values.sort()&#xA;            # 1 3 4&#xA;            for a, b in zip(values[:-1], values[1:]):&#xA;                if abs(a - b) &amp;lt;= k:&#xA;                    return True&#xA;&#xA;        return False&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: LeetCode #219: Contains Duplicate II&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #350: Intersection of Two Arrays II
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%23350-intersection-of-two-arrays-ii/</link>
      <pubDate>Tue, 06 Jan 2026 21:03:03 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%23350-intersection-of-two-arrays-ii/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/intersection-of-two-arrays-ii&#34;&gt;LeetCode #350: Intersection of Two Arrays II&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from collections import Counter&#xA;&#xA;class Solution:&#xA;    def intersect(self, nums1: List[int], nums2: List[int]) -&amp;gt; List[int]:&#xA;        c1 = Counter(nums1)&#xA;        c2 = Counter(nums2)&#xA;&#xA;        common = set(c1.keys()) &amp;amp; set(c2.keys())&#xA;&#xA;        ans = []&#xA;&#xA;        for n in common:&#xA;            ans.extend([n] * min(c1[n], c2[n]))&#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: LeetCode #350: Intersection of Two Arrays II&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #387: First Unique Character in a String
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%23387-first-unique-character-in-a-string/</link>
      <pubDate>Tue, 06 Jan 2026 20:59:58 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%23387-first-unique-character-in-a-string/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/first-unique-character-in-a-string&#34;&gt;LeetCode #387: First Unique Character in a String&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from collections import Counter&#xA;&#xA;class Solution:&#xA;    def firstUniqChar(self, s: str) -&amp;gt; int:&#xA;        counter = Counter(s)&#xA;        for i, c in enumerate(s):&#xA;            if counter[c] == 1:&#xA;                return i&#xA;&#xA;        return -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: LeetCode #387: First Unique Character in a String&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #136: Single Number
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%23136-single-number/</link>
      <pubDate>Tue, 06 Jan 2026 20:55:38 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%23136-single-number/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/single-number&#34;&gt;LeetCode #136: Single Number&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;p&gt;XOR everything:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;class Solution:&#xA;    def singleNumber(self, 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;Or with &lt;code&gt;reduce&lt;/code&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from functools import reduce&#xA;&#xA;class Solution:&#xA;    def singleNumber(self, nums: List[int]) -&amp;gt; int:&#xA;        return reduce(lambda a, b: a ^ b, 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: LeetCode #136: Single Number&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #2154: Keep Multiplying Found Values by Two
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%232154-keep-multiplying-found-values-by-two/</link>
      <pubDate>Tue, 06 Jan 2026 20:43:08 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%232154-keep-multiplying-found-values-by-two/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/keep-multiplying-found-values-by-two&#34;&gt;LeetCode #2154: Keep Multiplying Found Values by Two&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;class Solution:&#xA;    def findFinalValue(self, nums: List[int], original: int) -&amp;gt; int:&#xA;        s = set(nums)&#xA;&#xA;        while True:&#xA;            if original in s:&#xA;                original &amp;lt;&amp;lt;= 1&#xA;            else:&#xA;                break&#xA;&#xA;        return original&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: LeetCode #2154: Keep Multiplying Found Values by Two&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #3667: Sort Array By Absolute Value
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%233667-sort-array-by-absolute-value/</link>
      <pubDate>Tue, 06 Jan 2026 20:42:00 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%233667-sort-array-by-absolute-value/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/sort-array-by-absolute-value&#34;&gt;LeetCode #3667: Sort Array By Absolute Value&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;class Solution:&#xA;    def sortByAbsoluteValue(self, nums: List[int]) -&amp;gt; List[int]:&#xA;        return list(sorted(nums, key=lambda x: abs(x)))&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: LeetCode #3667: Sort Array By Absolute Value&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>LeetCode #1018: Binary Prefix Divisible By 5
      </title>
      <link>https://perrotta.dev/2026/01/leetcode-%231018-binary-prefix-divisible-by-5/</link>
      <pubDate>Tue, 06 Jan 2026 20:40:49 -0300</pubDate><author>serendipity@perrotta.dev (Thiago Perrotta)</author>
      <category>coding</category>
      <category>dev</category>
      <category>leetcode</category>
      <guid>https://perrotta.dev/2026/01/leetcode-%231018-binary-prefix-divisible-by-5/</guid>
      <description>&lt;p&gt;♠ &lt;a href=&#34;https://leetcode.com/problems/binary-prefix-divisible-by-5&#34;&gt;LeetCode #1018: Binary Prefix Divisible By 5&lt;/a&gt;:&lt;/p&gt;&#xA;&#xA;&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;class Solution:&#xA;    def prefixesDivBy5(self, nums: List[int]) -&amp;gt; List[bool]:&#xA;        ans = []&#xA;&#xA;        n = 0&#xA;&#xA;        for i, num in enumerate(nums):&#xA;            n = n * 2 &amp;#43; num&#xA;            ans.append(n % 5 == 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: LeetCode #1018: Binary Prefix Divisible By 5&#34;&gt;email&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&#34;https://perrotta.dev/tags/dev/&#34;&gt;#dev&lt;/a&gt; &lt;a href=&#34;https://perrotta.dev/tags/leetcode/&#34;&gt;#leetcode&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>
