<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Data Structures on Chen Kai Blog</title><link>https://www.chenk.top/en/tags/data-structures/</link><description>Recent content in Data Structures 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/tags/data-structures/index.xml" rel="self" type="application/rss+xml"/><item><title>LeetCode (10): Patterns — Stack and Queue</title><link>https://www.chenk.top/en/leetcode/stack-and-queue/</link><pubDate>Tue, 13 Sep 2022 09:00:00 +0000</pubDate><guid>https://www.chenk.top/en/leetcode/stack-and-queue/</guid><description>&lt;p>&lt;figure class="article-figure">
 &lt;img src="https://blog-pic-ck.oss-cn-beijing.aliyuncs.com/posts/en/leetcode/10-stack-queue/illustration_1.png" alt="LeetCode (10): Patterns: Stack and Queue — Chapter overview" loading="lazy" decoding="async" class="content-image">
 
&lt;/figure>
&lt;/p>
&lt;p>Stacks and queues may seem unassuming next to graphs or dynamic programming, but they underpin a surprising number of interview problems. The reason is simple: most algorithmic questions are really about &lt;em>order of access&lt;/em>. Stacks provide LIFO (last in, first out), while queues offer FIFO (first in, first out). Add variants like monotonic stacks, deques, and priority queues, and you can efficiently solve problems like bracket matching, next-greater-element, sliding-window extrema, top-K, BFS, and a long list of &amp;ldquo;implement X using Y&amp;rdquo; puzzles.&lt;/p></description></item><item><title>LeetCode (9): Patterns — Greedy Algorithms</title><link>https://www.chenk.top/en/leetcode/09-greedy-algorithms/</link><pubDate>Mon, 29 Aug 2022 09:00:00 +0000</pubDate><guid>https://www.chenk.top/en/leetcode/09-greedy-algorithms/</guid><description>&lt;p>&lt;figure class="article-figure">
 &lt;img src="https://blog-pic-ck.oss-cn-beijing.aliyuncs.com/posts/en/leetcode/09-greedy/illustration_1.png" alt="LeetCode (9): Patterns: Greedy Algorithms — Chapter overview" loading="lazy" decoding="async" class="content-image">
 
&lt;/figure>
&lt;/p>
&lt;p>The greedy algorithm paradigm seems too good to be true: at every step, choose the option that looks best right now, never look back, and somehow reach 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 recognizing when greedy is allowed.&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 (8): 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>&lt;figure class="article-figure">
 &lt;img src="https://blog-pic-ck.oss-cn-beijing.aliyuncs.com/posts/en/leetcode/08-backtracking/illustration_1.png" alt="LeetCode (8): Patterns: Backtracking Algorithms — Chapter overview" loading="lazy" decoding="async" class="content-image">
 
&lt;/figure>
&lt;/p>
&lt;p>Backtracking is the algorithm you use when a problem asks you to &lt;em>enumerate&lt;/em> something — every permutation, every subset, every legal board, every path through a grid. It&amp;rsquo;s brute force with a brain: you build a candidate solution one decision at a time, abandon it as soon as a constraint says &amp;ldquo;this won&amp;rsquo;t work,&amp;rdquo; and undo your last move so the next branch starts fresh. The whole technique fits in three lines:&lt;/p></description></item><item><title>LeetCode (7): 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>&lt;figure class="article-figure">
 &lt;img src="https://blog-pic-ck.oss-cn-beijing.aliyuncs.com/posts/en/leetcode/07-dp-grid/illustration_1.png" alt="LeetCode (7): Patterns: Dynamic Programming Basics — Chapter overview" loading="lazy" decoding="async" class="content-image">
 
&lt;/figure>
&lt;/p>
&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></description></item><item><title>LeetCode (6): 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>&lt;figure class="article-figure">
 &lt;img src="https://blog-pic-ck.oss-cn-beijing.aliyuncs.com/posts/en/leetcode/06-binary-tree/illustration_1.png" alt="LeetCode (6): Patterns: Binary Tree Traversal and Construction — Chapter overview" loading="lazy" decoding="async" class="content-image">
 
&lt;/figure>
&lt;/p>
&lt;p>A binary tree problem is rarely about the tree itself. It&amp;rsquo;s about &lt;em>the order in which you visit nodes&lt;/em> and &lt;em>what you remember from the children before deciding what to do at the parent&lt;/em>. Once these two ideas click, the four traversal orders, iterative rewrites, construction problems, and even classics like Validate BST and Maximum Depth all boil down to variations of the same recipe. This article builds that recipe from start to finish.&lt;/p></description></item><item><title>LeetCode (5): 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>&lt;figure class="article-figure">
 &lt;img src="https://blog-pic-ck.oss-cn-beijing.aliyuncs.com/posts/en/leetcode/05-binary-search/illustration_1.png" alt="LeetCode (5): Patterns: Binary Search — Chapter overview" loading="lazy" decoding="async" class="content-image">
 
&lt;/figure>
&lt;/p>
&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. Instead, it aims to provide a &lt;strong>mental model&lt;/strong> that explains why each template looks the way it does, along with 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 (4): 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>&lt;figure class="article-figure">
 &lt;img src="https://blog-pic-ck.oss-cn-beijing.aliyuncs.com/posts/en/leetcode/04-sliding-window/illustration_1.png" alt="LeetCode (4): Patterns: Sliding Window Technique — Chapter overview" loading="lazy" decoding="async" class="content-image">
 
&lt;/figure>
&lt;/p>
&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 &lt;span class="math-inline">$O(nk)$&lt;/span>
 or &lt;span class="math-inline">$O(n^2)$&lt;/span>
 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></description></item><item><title>LeetCode (3): 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>&lt;figure class="article-figure">
 &lt;img src="https://blog-pic-ck.oss-cn-beijing.aliyuncs.com/posts/en/leetcode/03-linked-list/illustration_1.png" alt="LeetCode (3): Patterns: Linked List Operations — Chapter overview" loading="lazy" decoding="async" class="content-image">
 
&lt;/figure>
&lt;/p>
&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 &lt;span class="math-inline">$O(1)$&lt;/span>
 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 (2): 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>&lt;figure class="article-figure">
 &lt;img src="https://blog-pic-ck.oss-cn-beijing.aliyuncs.com/posts/en/leetcode/02-two-pointers/illustration_1.png" alt="LeetCode (2): Patterns: Two Pointers — Chapter overview" loading="lazy" decoding="async" class="content-image">
 
&lt;/figure>
&lt;/p>
&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 &lt;span class="math-inline">$O(n)$&lt;/span>
 time with &lt;span class="math-inline">$O(1)$&lt;/span>
 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 (1): 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>&lt;figure class="article-figure">
 &lt;img src="https://blog-pic-ck.oss-cn-beijing.aliyuncs.com/posts/en/leetcode/01-hash-table/illustration_1.png" alt="LeetCode (1): Patterns: Hash Tables — Chapter overview" loading="lazy" decoding="async" class="content-image">
 
&lt;/figure>
&lt;/p>
&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>