<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>LeetCode on Chen Kai Blog</title><link>https://www.chenk.top/en/leetcode/</link><description>Recent content in LeetCode on Chen Kai Blog</description><generator>Hugo</generator><language>en</language><lastBuildDate>Tue, 13 Sep 2022 09:00:00 +0000</lastBuildDate><atom:link href="https://www.chenk.top/en/leetcode/index.xml" rel="self" type="application/rss+xml"/><item><title>LeetCode Patterns: Greedy Algorithms</title><link>https://www.chenk.top/en/leetcode/09-greedy-algorithms/</link><pubDate>Tue, 13 Sep 2022 09:00:00 +0000</pubDate><guid>https://www.chenk.top/en/leetcode/09-greedy-algorithms/</guid><description>&lt;p>Greedy is the algorithm paradigm that feels too good to be true: at every step, take the choice that looks best right now, never look back, and somehow end up at the global optimum. When it works, the code is almost embarrassingly short. When it doesn&amp;rsquo;t, it produces confidently wrong answers — which is why the real skill is not writing greedy code, but recognising &lt;strong>when greedy is allowed&lt;/strong>.&lt;/p>
&lt;p>This article walks through the structural reason greedy is correct on some problems and broken on others, then applies that lens to seven LeetCode classics: &lt;strong>Jump Game&lt;/strong>, &lt;strong>Jump Game II&lt;/strong>, &lt;strong>Gas Station&lt;/strong>, &lt;strong>Best Time to Buy and Sell Stock II&lt;/strong>, &lt;strong>Non-overlapping Intervals&lt;/strong>, &lt;strong>Task Scheduler&lt;/strong>, and &lt;strong>Partition Labels&lt;/strong>.&lt;/p></description></item><item><title>LeetCode Patterns: Stack and Queue</title><link>https://www.chenk.top/en/leetcode/stack-and-queue/</link><pubDate>Mon, 29 Aug 2022 09:00:00 +0000</pubDate><guid>https://www.chenk.top/en/leetcode/stack-and-queue/</guid><description>&lt;p>Stacks and queues look unassuming next to graphs or DP, but they sit underneath an astonishing fraction of interview problems. The reason is simple: most algorithmic questions are really questions about &lt;em>order of access&lt;/em>. Stacks give you LIFO (last in, first out); queues give you FIFO (first in, first out); and once you add the variants — monotonic stack, deque, priority queue — you have efficient answers for bracket matching, next-greater-element, sliding-window extrema, top-K, BFS, and a long tail of &amp;ldquo;implement X using Y&amp;rdquo; puzzles.&lt;/p></description></item><item><title>LeetCode Patterns: Backtracking Algorithms</title><link>https://www.chenk.top/en/leetcode/backtracking/</link><pubDate>Sun, 14 Aug 2022 09:00:00 +0000</pubDate><guid>https://www.chenk.top/en/leetcode/backtracking/</guid><description>&lt;p>Backtracking is the algorithm you reach for whenever a problem asks you to &lt;em>enumerate&lt;/em> something — every permutation, every subset, every legal board, every path through a grid. It is brute force with a brain: you build a candidate solution one decision at a time, abandon it the moment a constraint says &amp;ldquo;this cannot work&amp;rdquo;, and undo your last move so the next branch sees a clean slate. The whole technique fits in three lines:&lt;/p></description></item><item><title>LeetCode Patterns: Dynamic Programming Basics</title><link>https://www.chenk.top/en/leetcode/dynamic-programming-basics/</link><pubDate>Sat, 30 Jul 2022 09:00:00 +0000</pubDate><guid>https://www.chenk.top/en/leetcode/dynamic-programming-basics/</guid><description>&lt;p>Dynamic programming has a reputation for being the algorithm topic that
separates &amp;ldquo;competent coder&amp;rdquo; from &amp;ldquo;interview wizard&amp;rdquo;. A lot of that
reputation is unearned. DP is not a bag of clever tricks; it is a single
recipe applied to problems that happen to have repeated subproblems. If
you can answer three questions cleanly, you can solve almost any DP
problem on LeetCode:&lt;/p>
&lt;ol>
&lt;li>&lt;strong>What does &lt;code>dp[i]&lt;/code> actually mean?&lt;/strong> (state)&lt;/li>
&lt;li>&lt;strong>How do I build &lt;code>dp[i]&lt;/code> from smaller answers?&lt;/strong> (transition)&lt;/li>
&lt;li>&lt;strong>What are the smallest answers I already know?&lt;/strong> (base case)&lt;/li>
&lt;/ol>
&lt;p>This article walks through that recipe, then applies it to the seven
problems every DP study list eventually converges on: Climbing Stairs,
House Robber, Coin Change, Longest Increasing Subsequence, 0/1 Knapsack,
Longest Common Subsequence, and Edit Distance.&lt;/p></description></item><item><title>LeetCode Patterns: Binary Tree Traversal and Construction</title><link>https://www.chenk.top/en/leetcode/binary-tree-traversal/</link><pubDate>Fri, 15 Jul 2022 09:00:00 +0000</pubDate><guid>https://www.chenk.top/en/leetcode/binary-tree-traversal/</guid><description>&lt;p>A binary tree problem is rarely about the tree. It is about &lt;em>the order in which you touch nodes&lt;/em> and &lt;em>what you remember from the children before deciding what to do at the parent&lt;/em>. Once those two ideas click, the four traversal orders, the iterative rewrites, the construction problems, and even classics like Validate BST and Maximum Depth all collapse into a handful of variations on the same recipe. This article builds that recipe end to end.&lt;/p></description></item><item><title>LeetCode Patterns: Binary Search</title><link>https://www.chenk.top/en/leetcode/binary-search/</link><pubDate>Thu, 30 Jun 2022 09:00:00 +0000</pubDate><guid>https://www.chenk.top/en/leetcode/binary-search/</guid><description>&lt;p>Binary search is the algorithm everyone thinks they understand until they have to write it under interview pressure. The idea is one sentence — &lt;em>halve the search space at every step&lt;/em> — but the implementation is a minefield of off-by-one errors, infinite loops, and subtly wrong return values. The goal of this article is not to give you yet another recitation of the standard template; it is to give you a &lt;strong>mental model&lt;/strong> that explains why each template looks the way it does, and a small toolkit (three templates plus the answer-space pattern) that covers the vast majority of LeetCode problems.&lt;/p></description></item><item><title>LeetCode Patterns: Sliding Window Technique</title><link>https://www.chenk.top/en/leetcode/sliding-window/</link><pubDate>Wed, 15 Jun 2022 09:00:00 +0000</pubDate><guid>https://www.chenk.top/en/leetcode/sliding-window/</guid><description>&lt;p>If you have ever caught yourself writing a double &lt;code>for&lt;/code> loop to inspect every contiguous subarray, &lt;strong>sliding window&lt;/strong> is probably the optimisation you are missing. It turns an $O(nk)$ or $O(n^2)$ scan into a single linear pass by &lt;em>reusing the work&lt;/em> it has already done. This article walks through the technique from first principles, then drills four canonical LeetCode problems plus a monotonic-deque variant.&lt;/p>
&lt;h2 id="1-the-idea-in-one-picture">1. The Idea in One Picture&lt;/h2>
&lt;p>A sliding window is a contiguous range &lt;code>[left, right]&lt;/code> over an array or string. Instead of recomputing everything when the range moves, we &lt;strong>add the element entering on the right&lt;/strong> and &lt;strong>remove the element leaving on the left&lt;/strong>. Each element is touched at most twice, so the total cost is $O(n)$.&lt;/p></description></item><item><title>LeetCode Patterns: Linked List Operations</title><link>https://www.chenk.top/en/leetcode/linked-list-operations/</link><pubDate>Tue, 31 May 2022 09:00:00 +0000</pubDate><guid>https://www.chenk.top/en/leetcode/linked-list-operations/</guid><description>&lt;p>A linked list is the simplest data structure that forces you to &lt;strong>think in pointers&lt;/strong>. Arrays let you index in $O(1)$ and forget about layout; linked lists hand you a head pointer and ask, &lt;em>&amp;ldquo;now what?&amp;rdquo;&lt;/em> That single shift — from indices to references — is what makes linked-list problems so common in interviews. They are short to state, brutal to get right, and reward exactly the habits good engineers build: drawing pictures, naming pointers, and &lt;strong>never dereferencing without checking for &lt;code>None&lt;/code>&lt;/strong>.&lt;/p></description></item><item><title>LeetCode Patterns: Two Pointers</title><link>https://www.chenk.top/en/leetcode/two-pointers/</link><pubDate>Mon, 16 May 2022 09:00:00 +0000</pubDate><guid>https://www.chenk.top/en/leetcode/two-pointers/</guid><description>&lt;p>Hash tables buy you speed by spending memory. Two pointers is the opposite trade: spend a little structural assumption — the array is sorted, the list might have a cycle, the answer lives in a contiguous window — and you get $O(n)$ time with $O(1)$ extra space. The pattern looks trivial in code (two indices and a &lt;code>while&lt;/code> loop) but it has more failure modes than any other beginner technique: off-by-one indices, infinite loops, missed duplicates, wrong pointer moved on tie. The cure is to think in &lt;strong>invariants&lt;/strong> rather than in moves.&lt;/p></description></item><item><title>LeetCode Patterns: Hash Tables</title><link>https://www.chenk.top/en/leetcode/hash-tables/</link><pubDate>Sun, 01 May 2022 09:00:00 +0000</pubDate><guid>https://www.chenk.top/en/leetcode/hash-tables/</guid><description>&lt;p>A hash table is the cheapest superpower in your toolbox. You spend a constant amount of memory per stored item, and in return every &amp;ldquo;is &lt;em>x&lt;/em> in here?&amp;rdquo; question costs roughly one CPU instruction. Whole families of &lt;code>O(n²)&lt;/code> brute-force solutions collapse into a single &lt;code>O(n)&lt;/code> pass once you reach for one.&lt;/p>
&lt;p>This article is the first installment of the &lt;strong>LeetCode Patterns&lt;/strong> series. We will build hash table intuition from scratch, then work through four template problems — &lt;strong>Two Sum&lt;/strong>, &lt;strong>Group Anagrams&lt;/strong>, &lt;strong>Longest Substring Without Repeating Characters&lt;/strong>, and &lt;strong>Top K Frequent Elements&lt;/strong> — each illustrating a reusable pattern you will see again and again on harder problems.&lt;/p></description></item></channel></rss>