labuladong's Algorithm Notes This repository contains 60+ original articles, all based on LeetCode problems, covering every problem type and technique. The goal is to always teach you to generalize and make things easy to understand — not just dump code. A table of contents follows below. Let me rant for a moment. Grinding LeetCode is about grinding problems, but more importantly, it's about building your thinking. The purpose of this repo is to pass on that algorithmic mindset. What's the point of just having a repo full of LeetCode solutions? No explanation of the approach, no thought framework — at most a time complexity note that anyone could figure out at a glance. If you only want answers, that's easy — the comment sections are full of flashy one-liners in Python with thousands of likes. But ask yourself: are you doing algorithm problems to learn clever language tricks, or to build algorithmic thinking? Does your satisfaction come from copying someone's one-liner to tick off a problem, or from independently deriving a solution through logical reasoning and a framework you understand? People online always criticize me for being too basic, or claim you can't learn algorithms through frameworks. My only response is: most of us grind algorithms to get jobs, not to win competitions. I've been through the grind myself. We want clarity and real takeaways, not mystification and vagueness. If you don't try to make things clear and accessible, are you just going to start by hyping up Introduction to Algorithms (CLRS) and then scare everyone away with reverence? The more you do anything, the more patterns you find. I've summarized various algorithm patterns and frameworks, and I believe they can help others avoid a lot of wasted effort. I'm a self-taught learner who spent a year grinding problems and writing summaries. I've put together my own algorithm cheat sheet — the table of contents is below. Before You Start
- Give this repo a star to satisfy my vanity — the content is absolutely worth one star. I'm still creating, so give me some motivation to keep writing. Thank you.
- I recommend bookmarking my online website. Every article has a link to the corresponding LeetCode problem so you can read and practice at the same time — a total of 500 guided problems: Latest 2024 address: https://labuladong.online/algo/ GitHub Pages: https://labuladong.online/algo/ Gitee Pages: https://labuladong.gitee.io/algo/ Introduction to labuladong's Full Study Kit I. Algorithm Visualization Panel My algorithm website and all companion plugins include an algorithm visualization tool that visualizes data structures and recursion processes, greatly reducing the difficulty of understanding algorithms. Almost every problem's solution has a corresponding visualization panel — see the descriptions below for details. II. Learning Website The website is of course the core of my algorithm tutorial series. All my algorithm tutorials are published on labuladong.online. You'll likely spend a lot of learning time there — not just add it to your bookmarks~ III. Chrome Extension Main features: The Chrome extension lets you quickly view my "solutions" or "hints" on the Chinese LeetCode or English LeetCode, and adds cross-reference links between problems and algorithm techniques. It integrates with my website/newsletter/courses to give readers the smoothest possible problem-solving experience. See the table of contents below for the installation guide. IV. VSCode Extension Main features: Essentially the same as the Chrome extension. Recommended for readers who prefer solving problems in VSCode. See the table of contents below for the installation guide. V. JetBrains Extension Main features: Essentially the same as the Chrome extension. Recommended for readers who prefer JetBrains IDEs (PyCharm, IntelliJ, GoLand, etc.). See the table of contents below for the installation guide. Wishing everyone a happy learning journey — swim freely in the sea of problems! Table of Contents Site Introduction Learning Plans for Beginners and Fast-Trackers Quick-Track Learning Plan Complete Learning Plan Key Points and Pitfalls in Algorithm Practice How to Practice/Review the Exercise Chapters Study Tool Usage Guide AI Teaching Assistant for Q&A Algorithm Visualization Panel Guide Algorithm Games Overview Companion Chrome Extension Companion VSCode/Cursor Extension Companion JetBrains Extension Site Premium Membership Beginner: Programming Language Basics & Practice Chapter Introduction C++ Language Basics Java Language Basics Golang Language Basics Python Language Basics JavaScript Language Basics LeetCode Problem-Solving Essentials Language-Specific LeetCode Practice ACM Mode Code Templates Foundation: Data Structures & Sorting In Depth Chapter Introduction Intro to Time & Space Complexity Step-by-Step: Implement a Dynamic Array Array (Sequential Storage) Fundamentals Dynamic Array Code Implementation Step-by-Step: Implement Singly/Doubly Linked Lists Linked List (Linked Storage) Fundamentals Linked List Code Implementation [Game] Implement Snake Array & Linked List Variations Circular Array Technique & Implementation Skip List Core Principles Bitmap Principles & Implementation Step-by-Step: Implement Queue/Stack Queue/Stack Fundamentals Queue/Stack via Linked List Queue/Stack via Array Deque (Double-Ended Queue) Principles & Implementation Hash Table Principles & Implementation Hash Table Core Principles Hash Table via Chaining Two Key Challenges in Linear Probing Two Implementations of Linear Probing Hash Set Principles & Implementation Hash Table Variations LinkedHashMap: Hash Table Enhanced with Linked List ArrayHashMap: Hash Table Enhanced with Array Bloom Filter Principles & Implementation Binary Tree Structure & Traversal Binary Tree Basics & Common Types Recursive/Level-Order Traversal of Binary Trees When to Use DFS vs BFS Recursive/Level-Order Traversal of N-ary Trees Binary Tree Variations BST Applications & Visualization Red-Black Tree Perfect Balance & Visualization Trie / Prefix Tree Principles & Visualization Binary Heap Core Principles & Visualization Binary Heap / Priority Queue Implementation Segment Tree Core Principles & Visualization Data Compression & Huffman Trees Still updating... Graph Structure Basics & Algorithm Overview Basic Terminology in Graph Theory Generic Graph Code Implementation DFS/BFS Traversal of Graphs Eulerian Graphs & One-Stroke Drawing Graph Shortest Path Algorithms Overview Minimum Spanning Tree Algorithms Overview Union-Find Fundamentals Still updating... Top 10 Sorting Algorithms: Principles & Visualization Chapter Introduction Key Metrics for Sorting Algorithms The Problem with Selection Sort Stability: Bubble Sort Thinking in Reverse: Insertion Sort Breaking O(N²): Shell Sort Leveraging Pre-Order Position: Quick Sort Leveraging Post-Order Position: Merge Sort Applying Binary Heap: Heap Sort A New Sorting Principle: Counting Sort Best of All Worlds: Bucket Sort Radix Sort Still updating... Chapter 0: Core Problem-Solving Frameworks Summary Chapter Introduction Framework Thinking for Learning Data Structures & Algorithms Two-Pointer Technique: Solve 7 Linked List Problems Two-Pointer Technique: Solve 7 Array Problems Sliding Window Algorithm: Core Code Template Binary Tree Algorithm Series: Core Outline One Perspective + Two Thinking Modes for Recursion Dynamic Programming: Problem-Solving Framework Backtracking Algorithm: Problem-Solving Framework BFS Algorithm: Problem-Solving Framework Backtracking: Solve All Permutation/Combination/Subset Problems Greedy Algorithm: Problem-Solving Framework Divide & Conquer: Problem-Solving Framework Practical Guide to Time & Space Complexity Analysis Chapter 1: Classic Data Structure Algorithms Step-by-Step Linked List Problems Two-Pointer Technique: Solve 7 Linked List Problems Classic Linked List Two-Pointer Exercises All Ways to Reverse a Singly Linked List How to Detect a Palindrome Linked List Step-by-Step Array Problems Two-Pointer Technique: Solve 7 Array Problems [Game] Match-3 Game Fancy 2D Array Traversal Techniques Classic Array Two-Pointer Exercises [Game] Game of Life One Method to Solve All nSum Problems Elegant Algorithm Technique: Prefix Sum Array Classic Prefix Sum Exercises Elegant Algorithm Technique: Difference Array Sliding Window Algorithm: Core Code Template Classic Sliding Window Exercises Sliding Window Extension: Rabin-Karp String Matching Binary Search Algorithm: Core Code Template Binary Search: Left-Closed Right-Open Style Mental Framework for Applying Binary Search in Practice Classic Binary Search Exercises Weighted Random Selection Algorithm The Algorithm Behind Tian Ji's Horse Racing Classic Queue/Stack Algorithms Implement a Stack with a Queue and Vice Versa Classic Stack Exercises Parentheses Problem Collection Classic Queue Exercises Monotonic Stack Template: Solving 3 Example Problems Monotonic Stack Variations & Classic Exercises Monotonic Queue: Solving the Sliding Window Problem Generic Monotonic Queue Implementation & Classic Exercises Step-by-Step Binary Tree Problems Binary Tree Series Core Outline Binary Tree Mindset (Thinking Approach) Binary Tree Mindset (Construction) Binary Tree Mindset (Post-Order) Binary Tree Mindset (Serialization) BST Mindset (Properties) BST Mindset (Basic Operations) BST Mindset (Construction) BST Mindset (Post-Order) Binary Tree Exercise Collection Chapter Introduction Solving with "Traversal" Mindset I Solving with "Traversal" Mindset II Solving with "Traversal" Mindset III Solving with "Problem Decomposition" Mindset I Solving with "Problem Decomposition" Mindset II Using Both Mindsets Together Leveraging Post-Order Position I Leveraging Post-Order Position II Leveraging Post-Order Position III Level-Order Traversal Problems I Level-Order Traversal Problems II Classic BST Problems I Classic BST Problems II Binary Tree Extensions Extension: Lowest Common Ancestor Framework Extension: Count Nodes in a Complete Binary Tree Extension: Lazy Expansion of N-ary Trees Extension: Merge Sort In Depth & Applications Extension: Quick Sort In Depth & Applications Extension: Iterative Binary Tree Traversal via Stack Classic Data Structure Design Algorithms Like LEGO: Build LRU Cache Algorithms Like LEGO: Build LFU Cache O(1) Delete/Find Any Element in an Array More Hash Table Exercises Classic Priority Queue Exercises TreeMap/TreeSet Code Implementation Basic Segment Tree Implementation Optimization: Dynamic Segment Tree Optimization: Lazy Update Segment Tree Classic Segment Tree Exercises Trie Code Implementation Trie Algorithm Exercises Design an Exam Room Seat Assignment Algorithm More Classic Design Exercises Implementing Huffman Encoding Compression Consistent Hashing: Principles & Implementation Extension: How to Implement a Calculator Extension: Median Algorithm Using Two Binary Heaps Extension: Array Deduplication (Hard Version) Classic Graph Algorithms Bipartite Graph Detection Algorithm Hierholzer's Algorithm for Eulerian Paths Classic Eulerian Path Exercises Cycle Detection Algorithm Topological Sort Algorithm Union-Find Algorithm Classic Union-Find Exercises Dijkstra's Algorithm: Core Principles & Implementation Dijkstra Extension: Shortest Path with Constraints Classic Dijkstra Exercises A* Algorithm: Core Principles & Implementation Kruskal's MST Algorithm Prim's MST Algorithm Chapter 2: Classic Brute-Force Search Algorithms DFS / Backtracking Backtracking: Problem-Solving Framework Backtracking in Practice: Sudoku & N-Queens [Game] Implement a Sudoku Solver Backtracking: Solve All Permutation/Combination/Subset Problems Answering Common Questions About Backtracking/DFS One Article to Solve All Island Problems [Game] Minesweeper II Ball-Box Model: Two Perspectives on Backtracking Enumeration Backtracking in Practice: Generate Parentheses Backtracking in Practice: Partition into K Equal Subsets Classic Backtracking Exercises I Classic Backtracking Exercises II Classic Backtracking Exercises III BFS Algorithm BFS: Problem-Solving Framework [Game] Solve a Maze [Game] Huarong Road Puzzle [Game] Link-Match Game Classic BFS Exercises I Classic BFS Exercises II Chapter 3: Classic Dynamic Programming Algorithms DP Fundamentals Dynamic Programming: Problem-Solving Framework DP Design: Longest Increasing Subsequence How to Set Base Case and Memo Initial Values Two Perspectives on DP Enumeration Converting Between DP and Backtracking Thinking Space Optimization for Dynamic Programming Optimal Substructure and DP Array Traversal Direction Subsequence Problems Classic DP: Edit Distance DP Design: Maximum Subarray Classic DP: Longest Common Subsequence DP Subsequence Problem Template Knapsack Problems Classic DP: 0-1 Knapsack Classic DP: Subset Knapsack Classic DP: Unbounded Knapsack Knapsack Variant: Target Sum DP for Games DP: Minimum Path Sum DP Helped Me Beat "Magic Tower" DP Helped Me Beat "Fallout 4" Travel Smart: Weighted Shortest Path Multi-Source Shortest Path: Floyd's Algorithm Classic DP: Regular Expression Matching Classic DP: Egg Drop Problem Classic DP: Burst Balloons Classic DP: Game Theory Problems One Method to Solve All House Robber Problems One Method to Solve All Stock Trading Problems DP Exercise Collection House Robber Problem Pattern Classic Knapsack Exercises Classic DP Exercises I Classic DP Exercises II Greedy Problems Greedy Algorithm: Problem-Solving Framework Gas Station Greedy Algorithm Greedy: Interval Scheduling Problem Sweep Line Technique: Meeting Room Scheduling Video Clipping: A Greedy Algorithm Emerges Chapter 4: Other Common Algorithm Techniques Math Tricks Algorithm Problems Solvable in One Line of Code Common Bit Manipulation Techniques Essential Math Techniques [Game] Minesweeper Map Generator Random Algorithms in Games Two Common Factorial Algorithm Problems How to Find Prime Numbers Efficiently Finding Both Missing and Duplicate Elements Simultaneously Several Counter-Intuitive Probability Problems Math Trick Related Exercises Classic Interview Problems How to Efficiently Solve Trapping Rain Water One Article to Solve All Ugly Number Problems One Method to Solve Three Interval Problems Who Knew Card Games Could Inspire Algorithms Pancake Sort Algorithm String Multiplication How to Detect a Perfect Rectangle More Content Computer Science Fundamentals Frontend Development in the AI Era: A Beginner's Guide Introduction to Modern Encryption Deep Dive: Sessions and Cookies Deep Dive: JSON Web Tokens (JWT) Authentication vs. Authorization Deep Dive: OAuth 2.0 Authorization Framework OAuth 2.0 and OIDC Authentication OAuth 2.0 and PKCE Deep Dive: Single Sign-On (SSO) Deep Dive: Digital Certificates and CA Deep Dive: TLS Key Exchange Deep Dive: mTLS Mutual Authentication Introduction to Linux File Systems Linux Processes, Threads, and File Descriptors Explained Pitfalls with Linux Pipe Operators Linux Shell Usage Tips Storage Systems: LSM Tree Design Principles Still updating... Design Patterns Singleton Pattern Factory Method Pattern Abstract Factory Pattern Builder Pattern Prototype Pattern Adapter Pattern Composite Pattern Decorator Pattern Bridge Pattern Observer Pattern Strategy Pattern Still updating... Thanks to the Following Contributors for Translations Listed in alphabetical order by username: ABCpril, andavid, bryceustc, build2645, CarrieOn, cooker, Dong Wang, ExcaliburEX, floatLig, ForeverSolar, Fulin Li, Funnyyanne, GYHHAHA, Hi_archer, Iruze, Jieyixia, Justin, Kevin, Lrc123, lriy, Lyjeeq, MasonShu, Master-cai, miaoxiaozui2017, natsunoyoru97, nettee, PaperJets, qy-yang, realism0331, SCUhzs, Seaworth, shazi4399, ShuozheLi, sinjoywong, sunqiuming526, Tianhao Zhou, timmmGZ, tommytim0515, ucsk, wadegrc, walsvid, warmingkkk, Wonderxie, wsyzxxxx, xiaodp, youyun, yx-tan, Zero, Ziming Donate If this repository has been helpful to you, you're welcome to buy the author a cup of instant coffee ☕ �