# fucking-algorithm | DonglaiFu | Knowledge Base | Instagit

刷算法全靠套路，认准 labuladong 就够了！English version supported! Crack LeetCode, not only how, but also why. 

GitHub Stars: 133k

Repository: https://github.com/labuladong/fucking-algorithm

---

## Articles

### [When Is a Greedy Algorithm Appropriate Versus Dynamic Programming?](/labuladong/fucking-algorithm/when-is-a-greedy-algorithm-appropriate-versus-dynamic-programming)

Learn when to use greedy algorithms versus dynamic programming. Discover the greedy-choice property and overlapping sub-problems to optimize your solutions.

- Tags: deep-dive
- Published: 2026-02-25

### [How to Solve Interval Scheduling Problems Using a Greedy Approach](/labuladong/fucking-algorithm/how-to-solve-interval-scheduling-problems-using-a-greedy-approach)

Master interval scheduling problems with a greedy approach. Learn to efficiently select non-overlapping intervals by sorting and picking based on end times. Maximize your interval selections.

- Tags: how-to-guide
- Published: 2026-02-25

### [How to Implement a Trie for Efficient Prefix Matching and Autocomplete](/labuladong/fucking-algorithm/how-to-implement-a-trie-for-efficient-prefix-matching-and-autocomplete)

Implement a Trie for lightning-fast prefix matching and autocomplete. Discover O(L) lookup times for efficient string operations and boost your application's performance.

- Tags: how-to-guide
- Published: 2026-02-25

### [How to Reverse a Linked List in Groups of k Nodes: Recursive and Iterative Solutions](/labuladong/fucking-algorithm/how-to-reverse-a-linked-list-in-groups-of-k-nodes)

Learn to reverse a linked list in groups of k nodes with recursive and iterative solutions. Master reversing segments and reconnecting them efficiently.

- Tags: how-to-guide
- Published: 2026-02-25

### [Data Structures and Algorithms for Designing a Twitter Feed: A Complete Implementation Guide](/labuladong/fucking-algorithm/what-are-the-data-structures-and-algorithms-for-designing-a-twitter-feed)

Learn how to design a Twitter feed using data structures like linked lists and hash sets, plus algorithms like max-heaps. Implement a unified timeline efficiently.

- Tags: how-to-guide
- Published: 2026-02-25

### [How to Use Heaps and Priority Queues for Median Finding in a Data Stream](/labuladong/fucking-algorithm/how-to-use-heaps-and-priority-queues-for-median-finding)

Find the median of a data stream efficiently using two heaps. Learn how heaps and priority queues enable O(log n) insertion and O(1) median retrieval for dynamic data.

- Tags: how-to-guide
- Published: 2026-02-25

### [How to Validate, Search, and Insert Elements in a Binary Search Tree (BST)](/labuladong/fucking-algorithm/how-to-validate-search-and-insert-elements-in-a-binary-search-tree-bst)

Master binary search tree BST validation, search, and insertion in O(h) time. Learn efficient recursive traversal techniques for rapid data management. Unlock BST performance now.

- Tags: how-to-guide
- Published: 2026-02-25

### [How to Calculate Edit Distance Between Two Strings Using DP](/labuladong/fucking-algorithm/how-to-calculate-the-edit-distance-between-two-strings-using-dp)

Learn to calculate edit distance between two strings using dynamic programming. This guide explains DP transitions and O(m*n) complexity for efficient string comparison.

- Tags: how-to-guide
- Published: 2026-02-25

### [Dynamic Programming Approaches for Stock Trading Problems: A Unified Framework](/labuladong/fucking-algorithm/what-dynamic-programming-approaches-can-be-used-for-stock-trading-problems)

Master dynamic programming for stock trading with labuladong's unified framework. Solve all variations using a 3D DP state, optimizing to O(1) space for common constraints.

- Tags: deep-dive
- Published: 2026-02-25

### [How to Solve Knapsack Problems (0-1, Unbounded, Subset) Using Dynamic Programming](/labuladong/fucking-algorithm/how-to-solve-knapsack-problems-using-dynamic-programming)

Master knapsack problems like 0-1, unbounded, and subset using dynamic programming. Learn optimal O(N·W) time and O(W) space solutions. Explore the DP state and recurrence relations used.

- Tags: tutorial
- Published: 2026-02-25

### [How Prefix Sums Optimize Array Range Queries: O(1) Range Sum with O(n) Preprocessing](/labuladong/fucking-algorithm/how-can-prefix-sums-optimize-array-range-queries)

Learn how prefix sums optimize array range queries achieving O(1) range sums after O(n) preprocessing. Discover constant-time lookups with simple subtraction.

- Tags: tutorial
- Published: 2026-02-25

### [Union-Find Data Structure: Implementation, Optimizations, and Use Cases](/labuladong/fucking-algorithm/what-are-the-use-cases-and-implementation-of-the-union-find-data-structure)

Master the Union-Find data structure. Explore its efficient implementation, path compression and union-by-size optimizations, and diverse use cases in algorithms and graph problems.

- Tags: deep-dive
- Published: 2026-02-25

### [How to Implement Dijkstra's Algorithm for Shortest Paths in Weighted Graphs](/labuladong/fucking-algorithm/how-to-implement-dijkstras-algorithm-for-shortest-paths)

Learn how to implement Dijkstra's algorithm for shortest paths in weighted graphs. Discover how this greedy approach with a priority queue efficiently finds the closest unvisited vertex first.

- Tags: how-to-guide
- Published: 2026-02-25

### [How to Perform Topological Sorting on a Directed Acyclic Graph (DAG): DFS vs BFS Methods](/labuladong/fucking-algorithm/how-to-perform-topological-sorting-on-a-directed-acyclic-graph-dag)

Master topological sorting on DAGs using DFS and BFS methods. Learn efficient algorithms for directed acyclic graphs and order vertices correctly for your projects.

- Tags: how-to-guide
- Published: 2026-02-25

### [How to Use a Monotonic Stack for Next Greater Element Problems: A Complete Guide](/labuladong/fucking-algorithm/when-and-how-to-use-a-monotonic-stack-for-problems-like-next-greater-element)

Master monotonic stack for next greater element problems. Learn this O(N) technique to efficiently find next greater elements by scanning right to left and maintaining a decreasing stack.

- Tags: tutorial
- Published: 2026-02-25

### [How to Solve the Sliding Window Maximum Problem Efficiently Using a Monotonic Queue](/labuladong/fucking-algorithm/how-to-solve-the-sliding-window-maximum-problem-efficiently)

Master the sliding window maximum problem with a monotonic queue. Achieve O(N) time complexity and find maximums efficiently by processing each element just twice.

- Tags: how-to-guide
- Published: 2026-02-25

### [Key Considerations for Implementing Binary Search Correctly](/labuladong/fucking-algorithm/what-are-the-key-considerations-for-implementing-binary-search-correctly)

Master binary search implementation with this guide. Learn interval choices, midpoint calculation to avoid overflow, and pointer updates for accuracy. Get it right every time.

- Tags: how-to-guide
- Published: 2026-02-25

### [How to Use Backtracking to Generate All Permutations, Combinations, and Subsets](/labuladong/fucking-algorithm/how-to-use-backtracking-to-generate-all-permutations-combinations-and-subsets)

Master backtracking to generate all permutations combinations and subsets Learn the recursive template start index and used array techniques for efficient algorithm problem solving

- Tags: tutorial
- Published: 2026-02-25

### [How to Apply Dynamic Programming to Solve Optimization Problems](/labuladong/fucking-algorithm/how-to-apply-dynamic-programming-to-solve-optimization-problems)

Learn how to apply dynamic programming to solve complex optimization problems. Transform exponential time solutions into polynomial time using DP tables and caching.

- Tags: how-to-guide
- Published: 2026-02-25

### [Binary Tree Traversals: Recursive and Iterative Best Practices](/labuladong/fucking-algorithm/what-are-the-best-practices-for-binary-tree-traversals)

Master binary tree traversals with recursive and iterative best practices. Optimize for clarity with recursion or gain control with an explicit stack for deep trees. Improve your algorithm skills.

- Tags: best-practices
- Published: 2026-02-25

### [How to Calculate Trapped Rainwater Using Two Pointers and Monotonic Stacks](/labuladong/fucking-algorithm/how-to-calculate-trapped-rainwater-using-two-pointers-or-stacks)

Learn to calculate trapped rainwater in O(N) time with two pointers for O(1) space or a monotonic stack in the labuladong algorithm repository. Master this common coding challenge.

- Tags: how-to-guide
- Published: 2026-02-25

### [How to Solve Island Problems Using DFS: 6 Essential Patterns](/labuladong/fucking-algorithm/what-are-common-approaches-to-solve-island-problems-using-dfs)

Master island problems with DFS. Explore 6 essential patterns for counting islands, calculating areas, detecting closures, and finding unique shapes. Solve grid graph problems efficiently.

- Tags: how-to-guide
- Published: 2026-02-25

### [How to Detect if a Linked List Is a Palindrome: Two Optimal Approaches](/labuladong/fucking-algorithm/how-to-detect-if-a-linked-list-is-a-palindrome)

Detect if a linked list is a palindrome using recursion or fast/slow pointers. Explore O(N) time and O(1) space solutions for this common algorithm problem.

- Tags: how-to-guide
- Published: 2026-02-25

### [How to Implement an LRU Cache in O(1) Time Complexity: A Complete Guide](/labuladong/fucking-algorithm/how-to-implement-lru-cache-in-o-1-time-complexity)

Implement an O(1) LRU cache using a HashMap and doubly-linked list. Learn effective O(1) get and put operations for optimized performance in this complete guide.

- Tags: how-to-guide
- Published: 2026-02-25

