Problem Family Derivation¶
Base template problems and their derived variants. Learn the base pattern first, then apply to variants with small modifications.
BacktrackingExploration¶
π― Base Template:LeetCode 46 - Permutations Β· Solution¶
BASE TEMPLATE for permutation enumeration. Track usage with boolean array. At each position, try every unused element. This is the canonical backtracking permutation pattern.
Derived Problems¶
- π‘ LeetCode 47 - Permutations II Β· Solution(n! Γ n) time, O(n) space`
π― Base Template:LeetCode 78 - Subsets Β· Solution¶
BASE TEMPLATE for subset enumeration. Use start_index for canonical ordering. Every node (including empty) is a valid subset. Key difference from permutations: no 'used' array needed, collect at every node.
Derived Problems¶
- π‘ LeetCode 90 - Subsets II Β· Solution(n Γ 2^n) time, O(n) space`
- π‘ LeetCode 77 - Combinations Β· Solution(k Γ C(n,k)) time, O(k) space`
BinarySearchBoundary¶
π― Base Template:LeetCode 33 - Search in Rotated Sorted Array Β· Solution¶
Sorted-half detection: identify which half is sorted, then check if target lies in that range.
Derived Problems¶
- π‘ LeetCode 81 - Search in Rotated Sorted Array II Β· Solution(n) worst case, O(log n) average time, O(1) space`
π― Base Template:LeetCode 34 - Find First and Last Position of Element in Sorted Array Β· Solution¶
Two binary searches: lower_bound for first occurrence, upper_bound - 1 for last. This is the BASE TEMPLATE for boundary search problems.
Derived Problems¶
- π’ LeetCode 35 - Search Insert Position Β· Solution(log n) time, O(1) space`
- LeetCode 0278 (metadata not yet added)
π― Base Template:LeetCode 162 - Find Peak Element Β· Solution¶
Slope direction invariant: if ascending (mid < mid+1), peak is on right; if descending, peak is at mid or left. Boundary condition nums[-1] = nums[n] = -infinity guarantees peak exists.
Derived Problems¶
- LeetCode 0852 (metadata not yet added)
π― Base Template:LeetCode 875 - Koko Eating Bananas Β· Solution¶
Binary search on answer space [1, max(piles)]. Predicate: can_finish(speed) = total_hours <= h. Monotonic property ensures correctness.
Derived Problems¶
- π‘ LeetCode 1011 - Capacity To Ship Packages Within D Days Β· Solution(n log S) time, O(1) space, where S = sum(weights)`
- LeetCode 0410 (metadata not yet added)
BitmaskDP¶
π― Base Template:LeetCode 847 - Shortest Path Visiting All Nodes Β· Solution¶
BASE TEMPLATE for BFS with bitmask state. State = (node, visited_mask). Multi-source BFS from all nodes. Early termination when full_mask reached.
Derived Problems¶
- LeetCode 0943 (metadata not yet added)
- π΄ LeetCode 1494 - Parallel Courses II
π― Base Template:LeetCode 1125 - Smallest Sufficient Team Β· Solution¶
BASE TEMPLATE for set cover with bitmask DP. dp[mask] = smallest team to cover skills in mask. Encode skills as bitmask, iterate people to expand coverage.
Derived Problems¶
- LeetCode 1723 (metadata not yet added)
- LeetCode 1986 (metadata not yet added)
DifferenceArray¶
π― Base Template:LeetCode 1109 - Corporate Flight Bookings Β· Solution¶
Canonical difference array: mark range starts/ends, prefix sum for final values.
Derived Problems¶
- π‘ LeetCode 1094 - Car Pooling Β· Solution(n + m) time, O(m) space`
General¶
π― Base Template:LeetCode 110 - Balanced Binary Tree Β· Solution¶
Early termination pattern: return -1 as sentinel when balance fails. Avoids redundant subtree traversal.
Derived Problems¶
- (No derived problems listed)
π― Base Template:LeetCode 124 - Binary Tree Maximum Path Sum Β· Solution¶
Return vs Update pattern: update global max with path through node as apex, return single-branch gain for parent. Skip negative subtrees to maximize sum.
Derived Problems¶
- LeetCode 0687 (metadata not yet added)
- LeetCode 0129 (metadata not yet added)
π― Base Template:LeetCode 133 - Clone Graph Β· Solution¶
Map oldβnew nodes. On first visit create clone, on revisit return existing clone. Handles cycles via the mapping.
Derived Problems¶
- π‘ LeetCode 138 - Copy List with Random Pointer Β· Solution(n) time, O(n) space`
π― Base Template:LeetCode 253 - Meeting Rooms II Β· Solution¶
Min-heap of end times for greedy room assignment. Classic interval scheduling pattern.
Derived Problems¶
- (No derived problems listed)
π― Base Template:LeetCode 295 - Find Median from Data Stream Β· Solution¶
Two heaps: max-heap for lower half, min-heap for upper half. Classic streaming median pattern.
Derived Problems¶
- LeetCode 0480 (metadata not yet added)
π― Base Template:LeetCode 543 - Diameter of Binary Tree Β· Solution¶
Return vs Update pattern: diameter through node = left_height + right_height. Return single-branch height for parent, update global max for answer.
Derived Problems¶
- π΄ LeetCode 124 - Binary Tree Maximum Path Sum Β· Solution(n) time, O(h) space`
- LeetCode 0687 (metadata not yet added)
π― Base Template:LeetCode 621 - Task Scheduler Β· Solution¶
Mathematical formula: (max_freq - 1) * (n + 1) + max_freq_count.
Derived Problems¶
- LeetCode 0767 (metadata not yet added)
π― Base Template:LeetCode 785 - Is Graph Bipartite? Β· Solution¶
BFS naturally alternates levels. Color each level with opposite color. Conflict = not bipartite.
Derived Problems¶
- LeetCode 0886 (metadata not yet added)
π― Base Template:LeetCode 1046 - Last Stone Weight Β· Solution¶
Max-heap simulation of stone smashing. Repeatedly extract two largest.
Derived Problems¶
- LeetCode 1049 (metadata not yet added)
GraphDFS¶
π― Base Template:LeetCode 200 - Number of Islands Β· Solution¶
Base template for connected components. DFS flood fill marks entire island, count equals number of DFS calls from unvisited cells.
Derived Problems¶
- π‘ LeetCode 695 - Max Area of Island Β· Solution(m*n) time, O(m*n) space`
- LeetCode 0463 (metadata not yet added)
- π‘ LeetCode 130 - Surrounded Regions Β· Solution(m*n) time, O(m*n) space`
IntervalMerge¶
π― Base Template:LeetCode 56 - Merge Intervals Β· Solution¶
Base template for interval merging. Sort by start, merge adjacent overlaps.
Derived Problems¶
- π‘ LeetCode 57 - Insert Interval Β· Solution(n) time, O(n) space`
- π’ LeetCode 252 - Meeting Rooms Β· Solution(n log n) time, O(1) space`
- π‘ LeetCode 253 - Meeting Rooms II Β· Solution(n log n) time, O(n) space`
IntervalScheduling¶
π― Base Template:LeetCode 435 - Non-overlapping Intervals Β· Solution¶
Base template for interval scheduling. Sort by end, greedily select non-overlapping.
Derived Problems¶
- π‘ LeetCode 452 - Minimum Number of Arrows to Burst Balloons Β· Solution(n log n) time, O(1) space`
- LeetCode 0646 (metadata not yet added)
LinkedListInPlaceReversal¶
π― Base Template:LeetCode 206 - Reverse Linked List Β· Solution¶
Iterative three-pointer reversal. Canonical base template for in-place linked list reversal.
Derived Problems¶
- π‘ LeetCode 92 - Reverse Linked List II Β· Solution(N) time, O(1) space`
- π΄ LeetCode 25 - Reverse Nodes in k-Group Β· Solution(N) time, O(1) space`
MonotonicStack¶
π― Base Template:LeetCode 84 - Largest Rectangle in Histogram Β· Solution¶
Single-pass with sentinel for cleaner termination.
Derived Problems¶
- π΄ LeetCode 85 - Maximal Rectangle Β· Solution(rows * cols) time, O(cols) space`
π― Base Template:LeetCode 496 - Next Greater Element I Β· Solution¶
Canonical monotonic stack template with hash map for lookup.
Derived Problems¶
- π‘ LeetCode 503 - Next Greater Element II Β· Solution(n) time, O(n) space`
- π‘ LeetCode 739 - Daily Temperatures Β· Solution(n) time, O(n) space`
- π‘ LeetCode 901 - Online Stock Span Β· Solution(1) amortized per call, O(n) space`
PrefixProduct¶
π― Base Template:LeetCode 238 - Product of Array Except Self Β· Solution¶
Two-pass: prefix products forward, suffix products backward.
Derived Problems¶
- (No derived problems listed)
PrefixSum2D¶
π― Base Template:LeetCode 304 - Range Sum Query 2D - Immutable Β· Solution¶
2D prefix sum with inclusion-exclusion for rectangle queries.
Derived Problems¶
- LeetCode 0308 (metadata not yet added)
PrefixSumRangeQuery¶
π― Base Template:LeetCode 303 - Range Sum Query - Immutable Β· Solution¶
Base template for prefix sum range queries.
Derived Problems¶
- π‘ LeetCode 304 - Range Sum Query 2D - Immutable Β· Solution(m*n) init, O(1) query`
- π‘ LeetCode 560 - Subarray Sum Equals K Β· Solution(n) time, O(n) space`
- π‘ LeetCode 525 - Contiguous Array Β· Solution(n) time, O(n) space`
PrefixSumSubarraySum¶
π― Base Template:LeetCode 560 - Subarray Sum Equals K Β· Solution¶
Prefix sum + hash map for counting subarrays. Key: initialize {0: 1}.
Derived Problems¶
- π‘ LeetCode 525 - Contiguous Array Β· Solution(n) time, O(n) space`
- π‘ LeetCode 523 - Continuous Subarray Sum Β· Solution(n) time, O(min(n, k)) space`
- LeetCode 0974 (metadata not yet added)
ShortestPath¶
π― Base Template:LeetCode 743 - Network Delay Time Β· Solution¶
Base template for Dijkstra's shortest path algorithm.
Derived Problems¶
- π‘ LeetCode 1631 - Path With Minimum Effort Β· Solution(m*n log(m*n)) time, O(m*n) space`
- π‘ LeetCode 787 - Cheapest Flights Within K Stops Β· Solution(K * E) time, O(V) space`
- π΄ LeetCode 1368 - Minimum Cost to Make at Least One Valid Path in a Grid Β· Solution(m*n) time, O(m*n) space`
- π΄ LeetCode 2290 - Minimum Obstacle Removal to Reach Corner Β· Solution(m*n) time, O(m*n) space`
StringDP¶
π― Base Template:LeetCode 10 - Regular Expression Matching Β· Solution¶
Bottom-up DP handling . and * wildcards
Derived Problems¶
- π΄ LeetCode 44 - Wildcard Matching(m*n) time, O(m*n) space`
π― Base Template:LeetCode 72 - Edit Distance Β· Solution¶
Levenshtein distance with insert, delete, replace operations
Derived Problems¶
- LeetCode 0583 (metadata not yet added)
- LeetCode 0712 (metadata not yet added)
π― Base Template:LeetCode 1143 - Longest Common Subsequence Β· Solution¶
Classic 2D DP for comparing two strings
Derived Problems¶
- π‘ LeetCode 516 - Longest Palindromic Subsequence Β· Solution(n^2) time, O(n^2) space`
- LeetCode 0583 (metadata not yet added)
- LeetCode 1092 (metadata not yet added)
SubstringSlidingWindow¶
π― Base Template:LeetCode 3 - Longest Substring Without Repeating Characters Β· Solution¶
Canonical sliding window template with last-seen index array. Uses jump optimization instead of while-loop contraction. This is the BASE TEMPLATE for all SubstringSlidingWindow problems.
Derived Problems¶
- π΄ LeetCode 76 - Minimum Window Substring Β· Solution(|s| + |t|) time, O(|t|) space`
- LeetCode 0159 (metadata not yet added)
- π‘ LeetCode 209 - Minimum Size Subarray Sum Β· Solution(n) time, O(1) space`
- π‘ LeetCode 340 - Longest Substring with At Most K Distinct Characters Β· Solution(n) time, O(K) 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`
TopologicalSort¶
π― Base Template:LeetCode 207 - Course Schedule Β· Solution¶
Base template for topological sort using Kahn's algorithm.
Derived Problems¶
- π‘ LeetCode 210 - Course Schedule II Β· Solution(V+E) time, O(V+E) space`
- π‘ LeetCode 802 - Find Eventual Safe States Β· Solution(V+E) time, O(V) space`
- π΄ LeetCode 1203 - Sort Items by Groups Respecting Dependencies Β· Solution(V+E) time, O(V+E) space`
TreeTraversalBFS¶
π― Base Template:LeetCode 102 - Binary Tree Level Order Traversal Β· Solution¶
Base template for tree BFS. Process level by level using queue size batching.
Derived Problems¶
- LeetCode 0103 (metadata not yet added)
- LeetCode 0107 (metadata not yet added)
- π‘ LeetCode 199 - Binary Tree Right Side View Β· Solution(n) time, O(w) space`
- LeetCode 0637 (metadata not yet added)
TreeTraversalDFS¶
π― Base Template:LeetCode 94 - Binary Tree Inorder Traversal Β· Solution¶
Base template for tree DFS. Inorder: Left β Node β Right. Fundamental for BST operations.
Derived Problems¶
- LeetCode 0144 (metadata not yet added)
- LeetCode 0145 (metadata not yet added)
- π‘ LeetCode 230 - Kth Smallest Element in a BST Β· Solution(H + k) time, O(H) space`
Trie¶
π― Base Template:LeetCode 208 - Implement Trie (Prefix Tree) Β· Solution(Prefix Tree)¶
Base template for Trie operations.
Derived Problems¶
- π‘ LeetCode 211 - Design Add and Search Words Data Structure Β· Solution(L) add, O(26^D * L) search with D wildcards`
- π΄ LeetCode 212 - Word Search II Β· Solution(M*N*4^L) time, O(W*L) space`
- π‘ LeetCode 648 - Replace Words Β· Solution(D*L + W*L) time, O(D*L) space`
- π‘ LeetCode 1268 - Search Suggestions System Β· Solution(N*L*log N + N*L) time, O(N*L) space`
UnionFindConnectivity¶
π― Base Template:LeetCode 547 - Number of Provinces Β· Solution¶
Base template for Union-Find connectivity.
Derived Problems¶
- π‘ LeetCode 1319 - Number of Operations to Make Network Connected Β· Solution(E Γ Ξ±(n)) time, O(n) space`
- LeetCode 0323 (metadata not yet added)
π― Base Template:LeetCode 684 - Redundant Connection Β· Solution¶
Base template for cycle detection with Union-Find.
Derived Problems¶
- LeetCode 0685 (metadata not yet added)