Pattern Hierarchy¶
Algorithmic API Kernels and their pattern instantiations with example problems.
SubstringSlidingWindow¶
Sliding Window Unique¶
- π‘ LeetCode 3 - Longest Substring Without Repeating Characters Β· Solution(n) time, O(min(n, Ξ£)) space`
Sliding Window At Most K Distinct¶
- π‘ LeetCode 340 - Longest Substring with At Most K Distinct Characters Β· Solution(n) time, O(K) space`
Sliding Window Freq Cover¶
- π΄ LeetCode 76 - Minimum Window Substring Β· Solution(|s| + |t|) time, O(|t|) space`
- π‘ LeetCode 438 - Find All Anagrams in a String Β· Solution(|s| + |p|) time, O(1) space for maps, O(k) for output`
- π‘ LeetCode 567 - Permutation in String Β· Solution(|s1| + |s2|) time, O(1) space`
Sliding Window Cost Bounded¶
- π‘ LeetCode 209 - Minimum Size Subarray Sum Β· Solution(n) time, O(1) space`
Sliding Window Fixed Size¶
- (No problems tagged yet)
GridBFSMultiSource¶
Graph Bfs Multi Source¶
- π‘ LeetCode 417 - Pacific Atlantic Water Flow Β· Solution(m*n) time, O(m*n) space`
- π‘ LeetCode 994 - Rotting Oranges Β· Solution(m*n) time, O(m*n) space`
Grid Bfs Propagation¶
- π‘ LeetCode 286 - Walls and Gates Β· Solution(m*n) time, O(m*n) space`
- π‘ LeetCode 542 - 01 Matrix Β· Solution(m*n) time, O(m*n) space`
Bfs Shortest Path¶
- π΄ LeetCode 2612 - Minimum Reverse Operations Β· Solution(n log n) time, O(n) space`
KWayMerge¶
Merge K Sorted Heap¶
- π΄ LeetCode 23 - Merge k Sorted Lists Β· Solution(N log k) time, O(k) space`
Merge K Sorted Divide¶
- π΄ LeetCode 23 - Merge k Sorted Lists Β· Solution(N log k) time, O(k) space`
Merge Two Sorted¶
- π΄ LeetCode 4 - Median of Two Sorted Arrays Β· Solution(m+n) time, O(1) space`
MonotonicStack¶
Next Greater Element¶
- π’ LeetCode 496 - Next Greater Element I Β· Solution(n + m) time, O(n) space`
Next Smaller Element¶
- (No problems tagged yet)
Monotonic Stack Circular¶
- π‘ LeetCode 503 - Next Greater Element II Β· Solution(n) time, O(n) space`
Histogram Max Rectangle¶
- π΄ LeetCode 84 - Largest Rectangle in Histogram Β· Solution(n) time, O(n) space`
Maximal Rectangle Matrix¶
- π΄ LeetCode 85 - Maximal Rectangle Β· Solution(rows * cols) time, O(cols) space`
Monotonic Stack Span¶
- π‘ LeetCode 739 - Daily Temperatures Β· Solution(n) time, O(n) space`
- π‘ LeetCode 901 - Online Stock Span Β· Solution(1) amortized per call, O(n) space`
Monotonic Stack Contribution¶
- π‘ LeetCode 907 - Sum of Subarray Minimums Β· Solution(n) time, O(n) space`
- π‘ LeetCode 2104 - Sum of Subarray Ranges Β· Solution(n) time, O(n) space`
Monotonic Stack Container¶
- π΄ LeetCode 42 - Trapping Rain Water Β· Solution(n) time, O(n) space`
Monotonic Stack Greedy Selection¶
- π΄ LeetCode 321 - Create Maximum Number Β· Solution(k^2 * (m + n)) time, O(k) space`
- π‘ LeetCode 402 - Remove K Digits Β· Solution(n) time, O(n) space`
Monotonic Stack Lexicographic¶
- π‘ LeetCode 316 - Remove Duplicate Letters Β· Solution(n) time, O(26) = O(1) space`
BinarySearchBoundary¶
Binary Search First True¶
- (No problems tagged yet)
Binary Search Last True¶
- (No problems tagged yet)
Binary Search Lower Bound¶
- π‘ LeetCode 34 - Find First and Last Position of Element in Sorted Array Β· Solution(log n) time, O(1) space`
- π’ LeetCode 35 - Search Insert Position Β· Solution(log n) time, O(1) space`
Binary Search Upper Bound¶
- π‘ LeetCode 34 - Find First and Last Position of Element in Sorted Array Β· Solution(log n) time, O(1) space`
Binary Search Exact Match¶
- (No problems tagged yet)
Binary Search Insert Position¶
- π’ LeetCode 35 - Search Insert Position Β· Solution(log n) time, O(1) space`
Binary Search On Answer¶
- π΄ LeetCode 4 - Median of Two Sorted Arrays Β· Solution(m+n) time, O(1) space`
- π΄ LeetCode 778 - Swim in Rising Water Β· Solution(n^2 log n) time, O(n^2) space`
- π‘ LeetCode 875 - Koko Eating Bananas Β· Solution(n log m) time, O(1) space, where m = max(piles)`
- π‘ LeetCode 1011 - Capacity To Ship Packages Within D Days Β· Solution(n log S) time, O(1) space, where S = sum(weights)`
Binary Search Minimize Maximum¶
- π‘ LeetCode 875 - Koko Eating Bananas Β· Solution(n log m) time, O(1) space, where m = max(piles)`
- π‘ LeetCode 1011 - Capacity To Ship Packages Within D Days Β· Solution(n log S) time, O(1) space, where S = sum(weights)`
Binary Search Maximize Minimum¶
- (No problems tagged yet)
Binary Search Rotated¶
- π‘ LeetCode 33 - Search in Rotated Sorted Array Β· Solution(log n) time, O(1) space`
- π‘ LeetCode 153 - Find Minimum in Rotated Sorted Array Β· Solution(log n) time, O(1) space`
Binary Search Rotated Duplicates¶
- π‘ LeetCode 81 - Search in Rotated Sorted Array II Β· Solution(n) worst case, O(log n) average time, O(1) space`
Binary Search Peak Finding¶
- π‘ LeetCode 162 - Find Peak Element Β· Solution(log n) time, O(1) space`
UnionFindConnectivity¶
Union Find Connected Components¶
- π‘ LeetCode 547 - Number of Provinces Β· Solution(nΒ² Γ Ξ±(n)) time, O(n) space`
- π‘ LeetCode 1319 - Number of Operations to Make Network Connected Β· Solution(E Γ Ξ±(n)) time, O(n) space`
Union Find Cycle Detection¶
- π‘ LeetCode 684 - Redundant Connection Β· Solution(n Γ Ξ±(n)) time, O(n) space`
Union Find Connected Components¶
- π‘ LeetCode 547 - Number of Provinces Β· Solution(nΒ² Γ Ξ±(n)) time, O(n) space`
- π‘ LeetCode 1319 - Number of Operations to Make Network Connected Β· Solution(E Γ Ξ±(n)) time, O(n) space`
Union Find Cycle Detection¶
- π‘ LeetCode 684 - Redundant Connection Β· Solution(n Γ Ξ±(n)) time, O(n) space`
Union Find Equivalence Grouping¶
- π‘ LeetCode 721 - Accounts Merge Β· Solution(n Γ k Γ Ξ±(n)) time, O(n Γ k) space`
Union Find Constraint Satisfaction¶
- π‘ LeetCode 990 - Satisfiability of Equality Equations Β· Solution(n Γ Ξ±(26)) time, O(1) space`
BacktrackingExploration¶
Backtracking Permutation¶
- π‘ LeetCode 46 - Permutations Β· Solution(n! Γ n) time, O(n) space`
- π‘ LeetCode 47 - Permutations II Β· Solution(n! Γ n) time, O(n) space`
Backtracking Combination¶
- π‘ LeetCode 39 - Combination Sum Β· Solution(n^(t/m)) time, O(t/m) space`
- π‘ LeetCode 40 - Combination Sum II Β· Solution(2^n) time, O(n) space`
- π‘ LeetCode 77 - Combinations Β· Solution(k Γ C(n,k)) time, O(k) space`
- π‘ LeetCode 216 - Combination Sum III Β· Solution(C(9,k) Γ k) time, O(k) space`
Backtracking Subset¶
- π‘ LeetCode 78 - Subsets Β· Solution(n Γ 2^n) time, O(n) space`
- π‘ LeetCode 90 - Subsets II Β· Solution(n Γ 2^n) time, O(n) space`
Backtracking N Queens¶
- π΄ LeetCode 51 - N-Queens Β· Solution(n!) time`
- π΄ LeetCode 52 - N-Queens II Β· Solution(n!) time, O(n) space`
Backtracking Sudoku¶
- (No problems tagged yet)
Backtracking Combination Sum¶
- (No problems tagged yet)
Backtracking Combination Dedup¶
- (No problems tagged yet)
Backtracking Permutation Dedup¶
- (No problems tagged yet)
Backtracking Subset Dedup¶
- (No problems tagged yet)
Backtracking String Segmentation¶
- π‘ LeetCode 93 - Restore IP Addresses Β· Solution(3^4 Γ n) = O(n) time, O(1) space`
- π‘ LeetCode 131 - Palindrome Partitioning Β· Solution(n Γ 2^n) time, O(n^2) space`
Backtracking Grid Path¶
- π‘ LeetCode 79 - Word Search Β· Solution(m Γ n Γ 4^L) time, O(L) space`
LinkedListInPlaceReversal¶
Linked List K Group Reversal¶
- π΄ LeetCode 25 - Reverse Nodes in k-Group Β· Solution(N) time, O(1) space`
Linked List Full Reversal¶
- (No problems tagged yet)
Linked List Partial Reversal¶
- (No problems tagged yet)
TwoPointerPartition¶
Dutch Flag Partition¶
- π‘ LeetCode 75 - Sort Colors Β· Solution(n) time, O(1) space`
Two Way Partition¶
- π’ LeetCode 905 - Sort Array By Parity Β· Solution(n) time, O(1) space`
- π’ LeetCode 922 - Sort Array By Parity II Β· Solution(n) time, O(1) space`
Quickselect Partition¶
- π‘ LeetCode 215 - Kth Largest Element in an Array Β· Solution(n) average, O(nΒ²) worst case`
- π‘ LeetCode 973 - K Closest Points to Origin Β· Solution(n log k) time, O(k) space`
TwoPointersTraversal¶
Two Pointer Opposite¶
- π’ LeetCode 1 - Two Sum Β· Solution(n) time, O(n) space`
Two Pointer Opposite Search¶
- (No problems tagged yet)
Two Pointer Opposite Palindrome¶
- π’ LeetCode 125 - Valid Palindrome Β· Solution(n) time, O(1) space`
- π’ LeetCode 680 - Valid Palindrome II Β· Solution(n) time, O(1) space`
Two Pointer Opposite Maximize¶
- π‘ LeetCode 11 - Container With Most Water Β· Solution(n) time, O(1) space`
Two Pointer Same Direction¶
- (No problems tagged yet)
Two Pointer Writer Dedup¶
- π’ LeetCode 26 - Remove Duplicates from Sorted Array Β· Solution(n) time, O(1) space`
- π‘ LeetCode 80 - Remove Duplicates from Sorted Array II Β· Solution(n) time, O(1) space`
Two Pointer Writer Remove¶
- π’ LeetCode 27 - Remove Element Β· Solution(n) time, O(1) space`
Two Pointer Writer Compact¶
- π’ LeetCode 283 - Move Zeroes Β· Solution(n) time, O(1) space`
Two Pointer Three Sum¶
- π‘ LeetCode 15 - 3Sum Β· Solution(nΒ²) time, O(1) extra space`
- π‘ LeetCode 16 - 3Sum Closest Β· Solution(nΒ²) time, O(1) extra space`
Two Pointer K Sum¶
- (No problems tagged yet)
FastSlowPointers¶
Fast Slow Cycle Detect¶
- π’ LeetCode 141 - Linked List Cycle Β· Solution(n) time, O(1) space`
Fast Slow Cycle Start¶
- π‘ LeetCode 142 - Linked List Cycle II Β· Solution(n) time, O(1) space`
Fast Slow Midpoint¶
- π’ LeetCode 876 - Middle of the Linked List Β· Solution(n) time, O(1) space`
Fast Slow Implicit Cycle¶
- π’ LeetCode 202 - Happy Number Β· Solution(log n) iterations, O(log n) per iteration`
MergeSortedSequences¶
Merge Two Sorted Lists¶
- π’ LeetCode 21 - Merge Two Sorted Lists Β· Solution(m + n) time, O(1) space`
Merge Two Sorted Arrays¶
- π’ LeetCode 88 - Merge Sorted Array Β· Solution(m + n) time, O(1) space`
Merge Sorted From Ends¶
- π’ LeetCode 977 - Squares of a Sorted Array Β· Solution(n) time, O(n) space`
PrefixSumRangeQuery¶
Prefix Sum Range Query¶
- π’ LeetCode 303 - Range Sum Query - Immutable Β· Solution(n) init, O(1) query`
TreeTraversalDFS¶
Tree Dfs Recursive¶
- (No problems tagged yet)
Tree Dfs Iterative¶
- (No problems tagged yet)
Tree Dfs Inorder¶
- π’ LeetCode 94 - Binary Tree Inorder Traversal Β· Solution(n) time, O(h) space`
Tree Dfs Preorder¶
- (No problems tagged yet)
Tree Dfs Postorder¶
- (No problems tagged yet)
Tree Property Computation¶
- π’ LeetCode 104 - Maximum Depth of Binary Tree Β· Solution(n) time, O(h) space`
Tree Property Validation¶
- π’ LeetCode 110 - Balanced Binary Tree Β· Solution(n) time, O(h) space`
Tree Path Computation¶
- π’ LeetCode 543 - Diameter of Binary Tree Β· Solution(n) time, O(h) space`
Tree Path Sum¶
- π΄ LeetCode 124 - Binary Tree Maximum Path Sum Β· Solution(n) time, O(h) space`
TreeTraversalBFS¶
Bfs Level Order¶
- π΄ LeetCode 297 - Serialize and Deserialize Binary Tree Β· Solution(n) time, O(n) space`
Tree Bfs Level Order¶
- π‘ LeetCode 102 - Binary Tree Level Order Traversal Β· Solution(n) time, O(w) space`
DPSequence¶
Dp Fibonacci Style¶
- (No problems tagged yet)
Dp Longest Increasing¶
- (No problems tagged yet)
Dp Knapsack¶
- (No problems tagged yet)
DPInterval¶
Dp Palindrome¶
- (No problems tagged yet)
TopologicalSort¶
TriePrefixSearch¶
HeapTopK¶
Heap Top K¶
- π‘ LeetCode 347 - Top K Frequent Elements Β· Solution(n + m log k) time, O(m + k) space`
Heap Kth Element¶
- π‘ LeetCode 215 - Kth Largest Element in an Array Β· Solution(n) average, O(nΒ²) worst case`
Heap Median Stream¶
- π΄ LeetCode 295 - Find Median from Data Stream Β· Solution(log n) add, O(1) find`
Heap Interval Scheduling¶
- π‘ LeetCode 253 - Meeting Rooms II Β· Solution(n log n) time, O(n) space`
Heap Task Scheduler¶
- π‘ LeetCode 621 - Task Scheduler Β· Solution(n) time, O(1) space`
Heap Greedy Simulation¶
- π’ LeetCode 1046 - Last Stone Weight Β· Solution(n log n) time, O(n) space`
GraphDFS¶
Graph Dfs Connected Components¶
- π‘ LeetCode 200 - Number of Islands Β· Solution(m*n) time, O(m*n) space`
Graph Dfs Reachability¶
- π‘ LeetCode 841 - Keys and Rooms Β· Solution(V+E) time, O(V) space`
- π’ LeetCode 1971 - Find if Path Exists in Graph Β· Solution(V+E) time, O(V+E) space`
Graph Clone¶
- π‘ LeetCode 133 - Clone Graph Β· Solution(V+E) time, O(V) space`
GraphBFS¶
Graph Bfs Reachability¶
- (No problems tagged yet)
Graph Bfs Connected Components¶
- (No problems tagged yet)
Graph Bipartite¶
- π‘ LeetCode 785 - Is Graph Bipartite? Β· Solution(V+E) time, O(V) space`
UnionFind¶
IntervalMerge¶
Interval Merge¶
- π‘ LeetCode 56 - Merge Intervals Β· Solution(n log n) time, O(n) space`
Interval Insert¶
- π‘ LeetCode 57 - Insert Interval Β· Solution(n) time, O(n) space`
Interval Intersection¶
- π‘ LeetCode 986 - Interval List Intersections Β· Solution(m+n) time, O(min(m,n)) space`
IntervalScheduling¶
Interval Scheduling¶
- π’ LeetCode 252 - Meeting Rooms Β· Solution(n log n) time, O(1) space`
- π‘ LeetCode 435 - Non-overlapping Intervals Β· Solution(n log n) time, O(1) space`
- π‘ LeetCode 452 - Minimum Number of Arrows to Burst Balloons Β· Solution(n log n) time, O(1) space`
GreedyCore¶
Reachability¶
- π‘ LeetCode 45 - Jump Game II Β· Solution(n) time, O(1) space`
- π‘ LeetCode 55 - Jump Game Β· Solution(n) time, O(1) space`
Min Jumps¶
- π‘ LeetCode 45 - Jump Game II Β· Solution(n) time, O(1) space`
Prefix Min Reset¶
- π‘ LeetCode 134 - Gas Station Β· Solution(n) time, O(1) space`
Two Pass Greedy¶
- π΄ LeetCode 135 - Candy Β· Solution(n) time, O(n) space`
Sort Match¶
- π’ LeetCode 455 - Assign Cookies Β· Solution(n log n + m log m) time, O(1) space`
- π‘ LeetCode 1029 - Two City Scheduling Β· Solution(n log n) time, O(1) space`
Cost Difference¶
- π‘ LeetCode 1029 - Two City Scheduling Β· Solution(n log n) time, O(1) space`
DP1DLinear¶
Fibonacci Style¶
- π’ LeetCode 70 - Climbing Stairs Β· Solution(n) time, O(1) space`
Count Ways¶
- π’ LeetCode 70 - Climbing Stairs Β· Solution(n) time, O(1) space`
Min Cost Path¶
- π’ LeetCode 746 - Min Cost Climbing Stairs Β· Solution(n) time, O(1) space`
Include Exclude¶
- π‘ LeetCode 198 - House Robber Β· Solution(n) time, O(1) space`
- π‘ LeetCode 213 - House Robber II Β· Solution(n) time, O(1) space`
Max Non Adjacent¶
- π‘ LeetCode 198 - House Robber Β· Solution(n) time, O(1) space`
Circular Decomposition¶
- π‘ LeetCode 213 - House Robber II Β· Solution(n) time, O(1) space`
Running Min Max¶
- π’ LeetCode 121 - Best Time to Buy and Sell Stock Β· Solution(n) time, O(1) space`
Kadane Style¶
- π’ LeetCode 121 - Best Time to Buy and Sell Stock Β· Solution(n) time, O(1) space`
DPKnapsackSubset¶
Knapsack 01 Boolean¶
- π‘ LeetCode 416 - Partition Equal Subset Sum Β· Solution(n * target) time, O(target) space`
Knapsack 01 Count¶
- π‘ LeetCode 494 - Target Sum Β· Solution(n * target) time, O(target) space`
Target Transformation¶
- π‘ LeetCode 494 - Target Sum Β· Solution(n * target) time, O(target) space`
Unbounded Knapsack Min¶
- π‘ LeetCode 322 - Coin Change Β· Solution(n * amount) time, O(amount) space`
Unbounded Knapsack Count¶
- π‘ LeetCode 518 - Coin Change II Β· Solution(n * amount) time, O(amount) space`
MathNumberTheory¶
Gcd Euclidean¶
- π’ LeetCode 1979 - Find Greatest Common Divisor of Array Β· Solution(n + log(min)) time, O(1) space`
Prime Sieve¶
- π‘ LeetCode 204 - Count Primes Β· Solution(n log log n) time, O(n) space`
Base Conversion¶
- π’ LeetCode 168 - Excel Sheet Column Title Β· Solution(log n) time, O(log n) space`
SegmentTreeFenwick¶
Range Sum Update¶
- π‘ LeetCode 307 - Range Sum Query - Mutable Β· Solution(n log n) build, O(log n) ops`
Fenwick Tree¶
- (No problems tagged yet)
Segment Tree¶
- (No problems tagged yet)
Count Inversions¶
- π΄ LeetCode 315 - Count of Smaller Numbers After Self Β· Solution(n log n) time, O(n) space`
Merge Sort Counting¶
- π΄ LeetCode 327 - Count of Range Sum Β· Solution(n log n) time, O(n) space`
Coordinate Compression¶
- π΄ LeetCode 315 - Count of Smaller Numbers After Self Β· Solution(n log n) time, O(n) space`
LineSweep¶
Line Sweep Event Counting¶
- (No problems tagged yet)
Line Sweep Capacity Tracking¶
- (No problems tagged yet)
Line Sweep Height Tracking¶
- π΄ LeetCode 218 - The Skyline Problem Β· Solution(n log n) time, O(n) space`
Difference Array Sweep¶
- (No problems tagged yet)
TreeDP¶
Tree Dp Include Exclude¶
- π‘ LeetCode 337 - House Robber III Β· Solution(n) time, O(h) space`
Tree Dp Path Contribution¶
- (No problems tagged yet)
Tree Dp Multi State¶
- π΄ LeetCode 968 - Binary Tree Cameras Β· Solution(n) time, O(h) space`
Tree Dp Coverage¶
- π΄ LeetCode 968 - Binary Tree Cameras Β· Solution(n) time, O(h) space`
BitmaskDP¶
Bitmask Dp Enumeration¶
- π‘ LeetCode 78 - Subsets Β· Solution(n Γ 2^n) time, O(n) space`
Bitmask Dp Bfs¶
- π΄ LeetCode 847 - Shortest Path Visiting All Nodes Β· Solution(n Γ 2^n) time, O(n Γ 2^n) space`
Bitmask Dp Set Cover¶
- π΄ LeetCode 1125 - Smallest Sufficient Team Β· Solution(people Γ 2^skills) time, O(2^skills) space`
Bitmask Dp Tsp¶
- (No problems tagged yet)
StringDP¶
String Dp Lcs¶
- π‘ LeetCode 1143 - Longest Common Subsequence Β· Solution(m*n) time, O(m*n) space`
String Dp Edit Distance¶
- π‘ LeetCode 72 - Edit Distance Β· Solution(m*n) time, O(m*n) space`
String Dp Palindrome¶
- π‘ LeetCode 516 - Longest Palindromic Subsequence Β· Solution(n^2) time, O(n^2) space`
String Dp Matching¶
- π΄ LeetCode 10 - Regular Expression Matching Β· Solution(m*n) time, O(m*n) space`
MonotonicDeque¶
Monotonic Deque Sliding Max¶
- π΄ LeetCode 239 - Sliding Window Maximum Β· Solution(n) time, O(k) space`
Monotonic Deque Bounded Range¶
- (No problems tagged yet)
Monotonic Deque Prefix Sum¶
- π΄ LeetCode 862 - Shortest Subarray with Sum at Least K Β· Solution(n) time, O(n) space`
Monotonic Deque Optimization¶
- (No problems tagged yet)
IntervalDP¶
Interval Dp Burst¶
Interval Dp Polygon¶
Interval Dp Cut¶
Interval Dp Printer¶
StringMatching¶
String Matching Kmp¶
String Matching Rabin Karp¶
- (No problems tagged yet)
String Matching Kmp Concatenation¶
String Matching Kmp Period¶
String Matching Kmp Direct¶
GameTheoryDP¶
Game Theory Interval¶
Game Theory Linear¶
Game Theory Bitmask¶
- (No problems tagged yet)