Skip to content

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

🎯 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

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

🎯 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

🎯 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

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

🎯 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

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

🎯 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

🎯 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

IntervalMerge

🎯 Base Template:LeetCode 56 - Merge Intervals · Solution

Base template for interval merging. Sort by start, merge adjacent overlaps.

Derived Problems

IntervalScheduling

🎯 Base Template:LeetCode 435 - Non-overlapping Intervals · Solution

Base template for interval scheduling. Sort by end, greedily select non-overlapping.

Derived Problems

LinkedListInPlaceReversal

🎯 Base Template:LeetCode 206 - Reverse Linked List · Solution

Iterative three-pointer reversal. Canonical base template for in-place linked list reversal.

Derived Problems

MonotonicStack

🎯 Base Template:LeetCode 84 - Largest Rectangle in Histogram · Solution

Single-pass with sentinel for cleaner termination.

Derived Problems

🎯 Base Template:LeetCode 496 - Next Greater Element I · Solution

Canonical monotonic stack template with hash map for lookup.

Derived Problems

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

PrefixSumSubarraySum

🎯 Base Template:LeetCode 560 - Subarray Sum Equals K · Solution

Prefix sum + hash map for counting subarrays. Key: initialize {0: 1}.

Derived Problems

ShortestPath

🎯 Base Template:LeetCode 743 - Network Delay Time · Solution

Base template for Dijkstra's shortest path algorithm.

Derived Problems

StringDP

🎯 Base Template:LeetCode 10 - Regular Expression Matching · Solution

Bottom-up DP handling . and * wildcards

Derived Problems

🎯 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

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

TopologicalSort

🎯 Base Template:LeetCode 207 - Course Schedule · Solution

Base template for topological sort using Kahn's algorithm.

Derived Problems

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

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

Trie

🎯 Base Template:LeetCode 208 - Implement Trie (Prefix Tree) · Solution(Prefix Tree)

Base template for Trie operations.

Derived Problems

UnionFindConnectivity

🎯 Base Template:LeetCode 547 - Number of Provinces · Solution

Base template for Union-Find connectivity.

Derived Problems

🎯 Base Template:LeetCode 684 - Redundant Connection · Solution

Base template for cycle detection with Union-Find.

Derived Problems

  • LeetCode 0685 (metadata not yet added)