Skip to content

Solution Variants

Problems with multiple solution approaches. Understanding different approaches deepens your algorithmic thinking.

LeetCode 283 - Move Zeroes Β· Solution

🟒 Easy β€” 5 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) time, O(1) space
  • Delta from base: Move non-zero elements to front, then fill remaining with zeros.
  • Notes: Two-phase approach: compact non-zeros, then fill zeros.

🎯 Two Pointers (Base)

  • Complexity: O(n) time, O(1) space
  • Delta from base: Move non-zero elements to front, then fill remaining with zeros.
  • Notes: Same as default. Reader/writer pattern with zero-fill phase.

πŸ”„ Swap

Variant: swap_based

  • Complexity: O(n) time, O(1) space
  • Delta from base: Swap-based approach instead of overwrite + fill.
  • Notes: Swap non-zero elements forward, preserving zeros in place.

πŸ”„ Optimized Swap

Variant: optimized_swap

  • Complexity: O(n) time, O(1) space
  • Delta from base: Optimized swap avoiding unnecessary self-swaps.
  • Notes: Optimized swap avoiding unnecessary self-swaps.

πŸ”„ Snowball

Variant: snowball

  • Complexity: O(n) time, O(1) space
  • Delta from base: Snowball technique: accumulate zeros and swap with non-zero.
  • Notes: Snowball method tracking zeros rolling through array.

LeetCode 23 - Merge k Sorted Lists Β· Solution

πŸ”΄ Hard β€” 4 approaches

πŸ“ View All Solutions

🎯 Default (Base)

Variant: heap

  • Complexity: O(N log k) time, O(k) space
  • Notes: Min-heap based K-way merge. Classic API Kernel for merging K sorted sequences.

🎯 Heap (Base)

Variant: heap

  • Complexity: O(N log k) time, O(k) space
  • Notes: Same as default. Min-heap keeps track of smallest among K heads.

πŸ”„ Divide

Variant: divide_and_conquer

  • Complexity: O(N log k) time, O(log k) space (recursion)
  • Delta from base: Use merge-sort-like tree instead of heap. Pair-wise merge in log(k) rounds.
  • Notes: Divide and conquer: recursively merge pairs until one list remains.

πŸ”„ Greedy

Variant: greedy_comparison

  • Complexity: O(kN) time, O(1) space
  • Delta from base: Compare all K heads each time to find minimum. Less efficient but simpler.
  • Notes: Naive greedy approach for comparison. Demonstrates why heap optimization matters.

LeetCode 80 - Remove Duplicates from Sorted Array II Β· Solution

🟑 Medium β€” 4 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) time, O(1) space
  • Delta from base: Allow up to 2 copies instead of 1. Check nums[write-2] instead of nums[write-1].
  • Notes: Generalized deduplication: allow K copies by checking nums[write-K]. K=2 for this problem.

🎯 Two Pointers (Base)

  • Complexity: O(n) time, O(1) space
  • Delta from base: Allow up to 2 copies instead of 1. Check nums[write-2] instead of nums[write-1].
  • Notes: Same as default. Reader/writer pattern with K=2 lookback check.

πŸ”„ K Copies

Variant: generalized_k

  • Complexity: O(n) time, O(1) space
  • Delta from base: Generalized solution allowing up to K copies (default K=2).
  • Notes: Generalized solution allowing up to K copies (default K=2).

πŸ”„ Counter

Variant: explicit_counter

  • Complexity: O(n) time, O(1) space
  • Delta from base: Explicit counter tracking approach.
  • Notes: Explicit counter tracking approach.

LeetCode 125 - Valid Palindrome Β· Solution

🟒 Easy β€” 4 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) time, O(1) space
  • Notes: Canonical opposite pointers pattern for symmetric/palindrome checking. Skip non-alphanumeric characters.

🎯 Two Pointers (Base)

  • Complexity: O(n) time, O(1) space
  • Notes: Same as default. Two pointers from both ends with inline filtering.

πŸ”„ Filtered

Variant: prefilter

  • Complexity: O(n) time, O(n) space
  • Delta from base: Pre-filter string to remove non-alphanumeric characters.
  • Notes: Two-pass approach: filter first, then check palindrome.

πŸ”„ Filtered Pointers

Variant: filtered_pointers

  • Complexity: O(n) time, O(n) space
  • Delta from base: Filter first, then use two pointers on filtered string.
  • Notes: Hybrid approach: filter first, then use two pointers.

LeetCode 680 - Valid Palindrome II Β· Solution

🟒 Easy β€” 4 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) time, O(1) space
  • Delta from base: Allow one character skip. On mismatch, check both skip-left and skip-right options.
  • Notes: Extension of palindrome check: allow one skip, try both directions on mismatch.

🎯 Two Pointers (Base)

  • Complexity: O(n) time, O(1) space
  • Delta from base: Allow one character skip. On mismatch, check both skip-left and skip-right options.
  • Notes: Same as default. Two pointers with skip option on mismatch.

πŸ”„ Recursive

Variant: recursive

  • Complexity: O(n) time, O(n) space for recursion stack
  • Delta from base: Recursive helper function with explicit skip tracking.
  • Notes: Recursive helper function approach.

πŸ”„ Iterative

Variant: iterative

  • Complexity: O(n) time, O(1) space
  • Delta from base: Fully iterative solution avoiding recursion.
  • Notes: Fully iterative solution avoiding recursion.

LeetCode 3 - Longest Substring Without Repeating Characters Β· Solution

🟑 Medium β€” 3 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) time, O(min(n, Ξ£)) space
  • Notes: 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.

πŸ”„ Dict

Variant: dictionary

  • Complexity: O(n) time, O(min(n, Ξ£)) space
  • Delta from base: Uses dictionary instead of array for last-seen index. More flexible for Unicode.

πŸ”„ Set

Variant: set_while_loop

  • Complexity: O(n) time, O(min(n, Ξ£)) space
  • Delta from base: Uses set + while-loop contraction. More intuitive but no jump optimization.
  • Notes: Demonstrates standard while-loop contraction pattern used in other variants.

LeetCode 5 - Longest Palindromic Substring Β· Solution

🟑 Medium β€” 3 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(nΒ²) time, O(1) space
  • Notes: Expand from each center - optimal interview solution. Handles both odd and even length palindromes.

πŸ”„ Dp

Variant: dp

  • Complexity: O(nΒ²) time, O(nΒ²) space
  • Delta from base: Uses 2D DP table to track palindrome substrings
  • Notes: Classic interval DP approach. Good for understanding DP on substrings.

πŸ”„ Manacher

Variant: manacher

  • Complexity: O(n) time, O(n) space
  • Delta from base: O(n) using Manacher's algorithm with transformed string
  • Notes: Theoretically optimal but complex. Uses palindrome symmetry to skip redundant checks.

LeetCode 11 - Container With Most Water Β· Solution

🟑 Medium β€” 3 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) time, O(1) space
  • Notes: Opposite pointers pattern for maximizing area. Greedily move the pointer with shorter height.

🎯 Two Pointers (Base)

  • Complexity: O(n) time, O(1) space
  • Notes: Same as default. Two pointers from both ends with greedy movement.

πŸ”„ Optimized

Variant: skip_optimization

  • Complexity: O(n) time, O(1) space
  • Delta from base: Skips consecutive heights that cannot improve answer.
  • Notes: Two pointers with skip optimization for consecutive smaller heights.

LeetCode 15 - 3Sum Β· Solution

🟑 Medium β€” 3 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(nΒ²) time, O(1) extra space
  • Notes: Sort array, fix first element, use opposite pointers with deduplication.

🎯 Two Pointers (Base)

  • Complexity: O(nΒ²) time, O(1) extra space
  • Notes: Same as default. Sort + two pointers with duplicate skipping.

πŸ”„ Hashset

Variant: set_dedup

  • Complexity: O(nΒ²) time, O(n) space
  • Delta from base: Uses set for deduplication instead of pointer skipping.
  • Notes: Alternative deduplication approach using hash set.

LeetCode 16 - 3Sum Closest Β· Solution

🟑 Medium β€” 3 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(nΒ²) time, O(1) extra space
  • Delta from base: Track closest sum instead of exact matches; no deduplication needed.
  • Notes: Variant of 3Sum that finds closest sum to target.

🎯 Two Pointers (Base)

  • Complexity: O(nΒ²) time, O(1) extra space
  • Delta from base: Track closest sum instead of exact matches; no deduplication needed.
  • Notes: Same as default. Sort + two pointers tracking closest sum.

πŸ”„ Optimized

Variant: pruning

  • Complexity: O(nΒ²) time, O(1) extra space
  • Delta from base: Additional pruning strategies with bounds checking.
  • Notes: Two pointers with additional pruning strategies.

LeetCode 17 - Letter Combinations of a Phone Number Β· Solution

🟑 Medium β€” 3 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(4^n) time, O(n) space
  • Notes: Classic backtracking - build combinations character by character with choose-explore-unchoose pattern.

πŸ”„ Iterative

Variant: iterative

  • Complexity: O(4^n) time, O(4^n) space
  • Delta from base: BFS-style level expansion instead of DFS backtracking
  • Notes: Iterative approach using queue-like expansion.

πŸ”„ Product

Variant: product

  • Complexity: O(4^n) time, O(4^n) space
  • Delta from base: Uses itertools.product for Cartesian product
  • Notes: Pythonic solution using standard library. Less educational but concise.

LeetCode 21 - Merge Two Sorted Lists Β· Solution

🟒 Easy β€” 3 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(m + n) time, O(1) space
  • Notes: Iterative merge using dummy head. Canonical merge pattern for two sorted sequences.

🎯 Iterative (Base)

  • Complexity: O(m+n) time, O(1) space
  • Notes: Same as default. Iterative merge using dummy head.

πŸ”„ Recursive

Variant: recursive

  • Complexity: O(m + n) time, O(m + n) space for recursion stack
  • Delta from base: Recursive approach instead of iterative.
  • Notes: Recursive merge choosing smaller head.

LeetCode 25 - Reverse Nodes in k-Group Β· Solution

πŸ”΄ Hard β€” 3 approaches

πŸ“ View All Solutions

🎯 Default (Base)

Variant: iterative

  • Complexity: O(N) time, O(1) space
  • Notes: Iterative in-place k-group reversal using pointer manipulation. Constant space.

🎯 Iterative (Base)

Variant: iterative

  • Complexity: O(N) time, O(1) space
  • Notes: Same as default. Process k nodes at a time, reverse in-place.

πŸ”„ Recursive

Variant: recursive

  • Complexity: O(N) time, O(N/k) space (recursion stack)
  • Delta from base: Use recursion to handle groups. Stack space for recursion.
  • Notes: Recursive approach: reverse k nodes, then recurse on remaining list.

LeetCode 26 - Remove Duplicates from Sorted Array Β· Solution

🟒 Easy β€” 3 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) time, O(1) space
  • Notes: Canonical same-direction two-pointer pattern for in-place deduplication. Reader/writer pattern.

🎯 Two Pointers (Base)

  • Complexity: O(n) time, O(1) space
  • Notes: Same as default. Reader/writer pointer pattern for in-place deduplication.

πŸ”„ Enumerate

Variant: enumerate

  • Complexity: O(n) time, O(1) space
  • Delta from base: Uses enumerate for cleaner iteration.
  • Notes: Alternative implementation using enumerate.

LeetCode 27 - Remove Element Β· Solution

🟒 Easy β€” 3 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) time, O(1) space
  • Delta from base: Remove specific value instead of duplicates. Array need not be sorted.
  • Notes: Same-direction two-pointer pattern for removing specific elements in-place.

🎯 Two Pointers (Base)

  • Complexity: O(n) time, O(1) space
  • Delta from base: Remove specific value instead of duplicates. Array need not be sorted.
  • Notes: Same as default. Reader/writer pointer pattern for in-place removal.

πŸ”„ Two Ends

Variant: opposite_pointers

  • Complexity: O(n) time, O(1) space
  • Delta from base: Opposite pointers swapping from both ends. Efficient when val is rare.
  • Notes: Opposite pointers swapping from both ends (efficient when val is rare).

LeetCode 42 - Trapping Rain Water Β· Solution

πŸ”΄ Hard β€” 3 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) time, O(n) space
  • Notes: Monotonic decreasing stack resolves valleys layer by layer.

πŸ”„ Twopointer

Variant: two_pointer

  • Complexity: O(n) time, O(1) space
  • Delta from base: O(1) space using running left/right max.

πŸ”„ Dp

Variant: dp

  • Complexity: O(n) time, O(n) space
  • Delta from base: Precompute left_max and right_max arrays.

LeetCode 44 - Wildcard Matching

πŸ”΄ Hard β€” 3 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(m*n) time, O(m*n) space
  • Notes: 2D DP - simpler than regex since '*' matches any sequence directly

πŸ”„ Space Optimized

Variant: space_optimized

  • Complexity: O(m*n) time, O(n) space
  • Delta from base: Use two rows instead of full table
  • Notes: Rolling array optimization

πŸ”„ Greedy

Variant: greedy_backtracking

  • Complexity: O(m*n) worst, O(m+n) average time, O(1) space
  • Delta from base: Greedy matching with backtracking on '*'
  • Notes: Track last '*' position and backtrack on mismatch

LeetCode 53 - Maximum Subarray Β· Solution

🟑 Medium β€” 3 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) time, O(1) space
  • Notes: Classic Kadane's algorithm. At each position: extend or start fresh.

πŸ”„ Dp

Variant: dp

  • Complexity: O(n) time, O(n) space
  • Delta from base: Same logic with explicit dp array for clarity
  • Notes: dp[i] = max sum ending at i. Easier to understand state transitions.

πŸ”„ Divide Conquer

Variant: divide_conquer

  • Complexity: O(n log n) time, O(log n) space
  • Delta from base: Divide into halves, handle crossing case separately
  • Notes: Follow-up solution. Max is in left half, right half, or crossing middle.

LeetCode 62 - Unique Paths Β· Solution

🟑 Medium β€” 3 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(m*n) time, O(n) space
  • Notes: Each cell = sum of above + left. Single row reused per iteration.

πŸ”„ Dp 2D

Variant: dp_2d

  • Complexity: O(m*n) time, O(m*n) space
  • Delta from base: Full 2D grid for conceptual clarity
  • Notes: Standard DP: dp[i][j] = dp[i-1][j] + dp[i][j-1].

πŸ”„ Math

Variant: math

  • Complexity: O(min(m,n)) time, O(1) space
  • Delta from base: Direct formula instead of building DP table
  • Notes: C(m+n-2, m-1): choose which moves are down from total moves.

LeetCode 75 - Sort Colors Β· Solution

🟑 Medium β€” 3 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) time, O(1) space
  • Notes: Dutch National Flag algorithm. Three-way partition using three pointers (low, mid, high).

🎯 Dutch Flag (Base)

  • Complexity: O(n) time, O(1) space
  • Notes: Same as default. Dutch National Flag algorithm (one-pass three-way partition).

πŸ”„ Counting

Variant: counting_sort

  • Complexity: O(n) time, O(1) space
  • Delta from base: Two-pass counting sort approach.
  • Notes: Two-pass counting sort approach.

LeetCode 88 - Merge Sorted Array Β· Solution

🟒 Easy β€” 3 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(m + n) time, O(1) space
  • Delta from base: In-place merge writing from end to avoid overwriting unprocessed elements.
  • Notes: Key insight: write from end to avoid overwriting nums1 elements before processing them.

🎯 Backward (Base)

  • Complexity: O(m+n) time, O(1) space
  • Delta from base: In-place merge writing from end to avoid overwriting unprocessed elements.
  • Notes: Same as default. Merge from end to avoid overwriting unprocessed elements.

πŸ”„ Forward

Variant: forward_merge

  • Complexity: O(m+n) time, O(m) space
  • Delta from base: Forward merge requiring extra space for nums1 copy.
  • Notes: Forward merge requiring extra space for nums1 copy.

LeetCode 91 - Decode Ways Β· Solution

🟑 Medium β€” 3 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) time, O(1) space
  • Notes: Only prev two values needed, like Fibonacci. Handle zeros carefully.

πŸ”„ Dp Array

Variant: dp_array

  • Complexity: O(n) time, O(n) space
  • Delta from base: Explicit dp array for conceptual clarity
  • Notes: dp[i] = ways to decode first i chars. Clear state definition.

πŸ”„ Memo

Variant: memo

  • Complexity: O(n) time, O(n) space
  • Delta from base: Top-down instead of bottom-up
  • Notes: Recursive thinking: try single or double digit at each position.

LeetCode 92 - Reverse Linked List II Β· Solution

🟑 Medium β€” 3 approaches

πŸ“ View All Solutions

πŸ”„ Default

Variant: segment_reversal

  • Complexity: O(N) time, O(1) space
  • Delta from base: Reverse only segment [left, right]. Requires boundary tracking and reconnection.
  • Notes: Two-pass approach: navigate to segment, reverse, reconnect.

πŸ”„ Two Pass

Variant: segment_reversal

  • Complexity: O(N) time, O(1) space
  • Delta from base: Reverse only segment [left, right]. Requires boundary tracking and reconnection.
  • Notes: Same as default. Navigate, reverse segment, reconnect both ends.

πŸ”„ One Pass

Variant: insert_at_front

  • Complexity: O(N) time, O(1) space
  • Delta from base: One-pass by repeatedly inserting nodes at front of segment.
  • Notes: Tricky but elegant one-pass approach. segment_start stays fixed.

LeetCode 97 - Interleaving String Β· Solution

🟑 Medium β€” 3 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(m*n) time, O(n) space
  • Notes: Satisfies follow-up space requirement. Single row DP.

πŸ”„ Dp 2D

Variant: dp_2d

  • Complexity: O(m*n) time, O(m*n) space
  • Delta from base: Full 2D table for conceptual clarity
  • Notes: dp[i][j] = can s1[0:i] and s2[0:j] form s3[0:i+j]?

πŸ”„ Memo

Variant: memo

  • Complexity: O(m*n) time, O(m*n) space
  • Delta from base: Top-down recursive formulation
  • Notes: Try taking from s1 or s2 at each position.

LeetCode 139 - Word Break Β· Solution

🟑 Medium β€” 3 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n^2 * m) time, O(n) space
  • Notes: dp[i] = can s[0:i] be segmented? Check all j < i where dp[j] and s[j:i] is word.

πŸ”„ Bfs

Variant: bfs

  • Complexity: O(n^2 * m) time, O(n) space
  • Delta from base: Graph perspective: find path from 0 to n via valid words
  • Notes: Nodes are positions, edges connect i to j if s[i:j] in dictionary.

πŸ”„ Memo

Variant: memo

  • Complexity: O(n^2 * m) time, O(n) space
  • Delta from base: Top-down instead of bottom-up
  • Notes: canBreak(i) = can s[i:] be segmented? Natural recursive formulation.

LeetCode 141 - Linked List Cycle Β· Solution

🟒 Easy β€” 3 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) time, O(1) space
  • Notes: Floyd's cycle detection algorithm (Tortoise and Hare). Fast pointer moves 2Γ— speed of slow pointer.

🎯 Floyd (Base)

  • Complexity: O(n) time, O(1) space
  • Notes: Same as default. Floyd's cycle detection (fast-slow pointers).

πŸ”„ Hashset

Variant: hash_set

  • Complexity: O(n) time, O(n) space
  • Delta from base: Use hash set to track visited nodes instead of fast-slow pointers.
  • Notes: Hash set to track visited nodes.

LeetCode 142 - Linked List Cycle II Β· Solution

🟑 Medium β€” 3 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) time, O(1) space
  • Delta from base: Phase 2: Find cycle start node using mathematical property after detecting cycle.
  • Notes: Floyd's algorithm Phase 2: After finding meeting point, move one pointer to head and both move at speed 1 to find cycle start.

🎯 Floyd (Base)

  • Complexity: O(n) time, O(1) space
  • Delta from base: Phase 2: Find cycle start node using mathematical property after detecting cycle.
  • Notes: Same as default. Floyd's algorithm with two phases.

πŸ”„ Hashset

Variant: hash_set

  • Complexity: O(n) time, O(n) space
  • Delta from base: Use hash set to find first revisited node (cycle start).
  • Notes: Hash set to find first revisited node.

LeetCode 202 - Happy Number Β· Solution

🟒 Easy β€” 3 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(log n) iterations, O(log n) per iteration
  • Delta from base: Apply fast-slow cycle detection to number sequence (implicit linked list).
  • Notes: Fast-slow pointers on implicit linked list formed by sum-of-squares sequence. Cycle detection for number sequences.

🎯 Floyd (Base)

  • Complexity: O(log n) time, O(1) space
  • Delta from base: Apply fast-slow cycle detection to number sequence (implicit linked list).
  • Notes: Same as default. Floyd's cycle detection on number sequence.

πŸ”„ Hashset

Variant: hash_set

  • Complexity: O(log n) time, O(log n) space
  • Delta from base: Use hash set to track seen numbers instead of fast-slow pointers.
  • Notes: Hash set to detect cycles in sequence.

LeetCode 206 - Reverse Linked List Β· Solution

🟒 Easy β€” 3 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(N) time, O(1) space
  • Notes: Iterative three-pointer reversal. Canonical base template for in-place linked list reversal.

🎯 Iterative (Base)

  • Complexity: O(N) time, O(1) space
  • Notes: Same as default. Iterative three-pointer reversal.

πŸ”„ Recursive

Variant: recursive

  • Complexity: O(N) time, O(N) space
  • Delta from base: Recursive approach instead of iterative. O(N) stack space.
  • Notes: Recursive reversal - reverse tail first, fix on return.

LeetCode 215 - Kth Largest Element in an Array Β· Solution

🟑 Medium β€” 3 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) average, O(nΒ²) worst case
  • Notes: Quickselect algorithm: partition-based selection. Expected O(n) time using partition from quicksort.

🎯 Quickselect (Base)

  • Complexity: O(n) average time, O(1) space
  • Notes: Same as default. Quickselect algorithm with random pivot.

πŸ”„ Heap

Variant: min_heap

  • Complexity: O(n log k) time, O(k) space
  • Delta from base: Use min-heap of size K to maintain K largest elements.
  • Notes: Min-heap of size k to maintain k largest elements.

LeetCode 647 - Palindromic Substrings Β· Solution

🟑 Medium β€” 3 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(n^2) time, O(1) space
  • Notes: Expand from each of 2n-1 centers; most practical approach

Dp

  • Complexity: O(n^2) time, O(n^2) space
  • Notes: Build full palindrome table dp[i][j]

Manacher

  • Complexity: O(n) time, O(n) space
  • Notes: Linear time using palindrome radius array

LeetCode 695 - Max Area of Island Β· Solution

🟑 Medium β€” 3 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(m*n) time, O(m*n) space
  • Notes: Recursive DFS with in-place marking (sink visited land)

Iterative

  • Complexity: O(m*n) time, O(m*n) space
  • Notes: Iterative DFS with explicit stack; avoids recursion limit

Bfs

  • Complexity: O(m*n) time, O(m*n) space
  • Notes: BFS traversal using deque; explores level by level

LeetCode 778 - Swim in Rising Water Β· Solution

πŸ”΄ Hard β€” 3 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n^2 log n) time, O(n^2) space
  • Notes: Modified Dijkstra minimizing maximum elevation along path instead of sum

Variant: binary_search

  • Complexity: O(n^2 log(n^2)) time, O(n^2) space
  • Delta from base: Binary search on time value with BFS reachability check
  • Notes: Exploits monotonic property: reachable at T implies reachable at all T' > T

πŸ”„ Union Find

Variant: union_find

  • Complexity: O(n^2 log n) time, O(n^2) space
  • Delta from base: Process cells by elevation order, union when adjacent cells both processed
  • Notes: Kruskal-like approach: find minimum elevation when start-end become connected

LeetCode 876 - Middle of the Linked List Β· Solution

🟒 Easy β€” 3 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) time, O(1) space
  • Notes: Fast-slow pointer pattern: when fast reaches end, slow is at middle. Returns second middle for even-length lists.

🎯 Fast Slow (Base)

  • Complexity: O(n) time, O(1) space
  • Notes: Same as default. Fast-slow pointers for midpoint finding.

πŸ”„ Two Pass

Variant: two_pass

  • Complexity: O(n) time, O(1) space
  • Delta from base: Two-pass approach: count length first, then traverse to middle.
  • Notes: Two-pass: count nodes then find middle.

LeetCode 905 - Sort Array By Parity Β· Solution

🟒 Easy β€” 3 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) time, O(1) space
  • Delta from base: Two-way partition (even/odd) instead of three-way. Can use opposite pointers or same-direction writer.
  • Notes: Two-way partition: separate evens and odds. Opposite pointers approach.

🎯 Opposite Pointers (Base)

  • Complexity: O(n) time, O(1) space
  • Delta from base: Two-way partition (even/odd) instead of three-way.
  • Notes: Same as default. Opposite pointers swapping evens and odds.

πŸ”„ Writer

Variant: same_direction

  • Complexity: O(n) time, O(1) space
  • Delta from base: Same-direction writer pattern: move evens to front.
  • Notes: Same-direction reader/writer pattern.

LeetCode 973 - K Closest Points to Origin Β· Solution

🟑 Medium β€” 3 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(n log k) time, O(k) space
  • Notes: Max-heap of size k; efficient when k << n

Sort

  • Complexity: O(n log n) time, O(n) space
  • Notes: Sort by distance, take first k; simple but sorts all

Quickselect

  • Complexity: O(n) average time, O(1) space
  • Notes: Quickselect partitioning; optimal average case

LeetCode 977 - Squares of a Sorted Array Β· Solution

🟒 Easy β€” 3 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) time, O(n) space
  • Delta from base: Merge from ends: largest squares are at array ends. Write result from end.
  • Notes: Key insight: largest squares are at ends. Merge two sorted sequences (negative reversed, positive forward) writing from end.

🎯 Two Pointers (Base)

  • Complexity: O(n) time, O(n) space
  • Delta from base: Merge from ends: largest squares are at array ends. Write result from end.
  • Notes: Same as default. Two pointers from ends merging largest squares first.

πŸ”„ Sort

Variant: sort

  • Complexity: O(n log n) time, O(n) space
  • Delta from base: Square all elements then sort.
  • Notes: Square then sort (suboptimal).

LeetCode 1268 - Search Suggestions System Β· Solution

🟑 Medium β€” 3 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(N*L*log N + N*L) time, O(N*L) space
  • Notes: Trie with precomputed suggestions at each node.
  • Complexity: O(N log N + L * log N) time, O(1) extra space
  • Notes: Binary search on sorted products.

Trie Dfs

  • Complexity: O(N*L + L*K) time per query, O(N*L) space
  • Notes: DFS collection on demand.

LeetCode 1584 - Min Cost to Connect All Points Β· Solution

🟑 Medium β€” 3 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n^2 log n) time, O(n^2) space
  • Notes: Prim's algorithm using min-heap, grows MST by adding minimum edge to unvisited

πŸ”„ Kruskal

Variant: kruskal

  • Complexity: O(n^2 log n) time, O(n^2) space
  • Delta from base: Sort all edges globally, use Union-Find to avoid cycles
  • Notes: Kruskal's algorithm processes edges in sorted order with Union-Find

πŸ”„ Prim Optimized

Variant: prim_optimized

  • Complexity: O(n^2) time, O(n) space
  • Delta from base: Replace heap with linear scan - optimal for complete/dense graphs
  • Notes: Optimal for dense graphs where O(n^2) is unavoidable due to edge count

LeetCode 1971 - Find if Path Exists in Graph Β· Solution

🟒 Easy β€” 3 approaches

πŸ“ View All Solutions

πŸ”„ Default

Variant: dfs_connectivity

  • Complexity: O(V+E) time, O(V+E) space
  • Delta from base: Point-to-point reachability with early termination.
  • Notes: Build adjacency list from edge list, DFS with early exit when destination found.

πŸ”„ Bfs

Variant: bfs_connectivity

  • Complexity: O(V+E) time, O(V+E) space
  • Delta from base: BFS instead of DFS.
  • Notes: Iterative BFS with early termination.

πŸ”„ Union Find

Variant: union_find

  • Complexity: O(E*Ξ±(n)) time, O(V) space
  • Delta from base: Union-Find approach. Good for multiple connectivity queries.
  • Notes: Ξ±(n) is inverse Ackermann, effectively constant. Better for multiple queries on same graph.

LeetCode 7 - Reverse Integer Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(log n) time, O(1) space
  • Notes: Extract digits with mod/div, check overflow before operation

String

  • Complexity: O(log n) time, O(log n) space
  • Notes: String reversal with boundary check

LeetCode 10 - Regular Expression Matching Β· Solution

πŸ”΄ Hard β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(m*n) time, O(m*n) space
  • Notes: Bottom-up DP handling . and * wildcards

πŸ”„ Recursive

Variant: top_down

  • Complexity: O(m*n) time, O(m*n) space
  • Delta from base: Top-down recursion with memoization
  • Notes: Recursive approach with lru_cache

LeetCode 19 - Remove Nth Node From End of List Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) time, O(1) space
  • Notes: Single pass using two pointers with n-gap. Dummy node handles edge case of removing head.

πŸ”„ Two Pass

Variant: two_pass

  • Complexity: O(n) time, O(1) space
  • Delta from base: Two passes: first counts length, second removes node
  • Notes: Simpler logic but requires two traversals.

LeetCode 20 - Valid Parentheses Β· Solution

🟒 Easy β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) time, O(n) space
  • Notes: Classic stack solution. Push opens, pop and match for closes. Hash map for O(1) pair lookup.

πŸ”„ Replace

Variant: replace

  • Complexity: O(nΒ²) time, O(n) space
  • Delta from base: Repeatedly remove matched pairs until string is empty
  • Notes: Simple but inefficient. Good for understanding but not for production.

LeetCode 22 - Generate Parentheses Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(4^n / sqrt(n)) time, O(n) space
  • Notes: Track open/close counts. Add '(' if open < n, add ')' if close < open.

πŸ”„ Dp

Variant: dp

  • Complexity: O(4^n / sqrt(n)) time, O(4^n / sqrt(n)) space
  • Delta from base: Builds solutions from smaller subproblems using Catalan recurrence
  • Notes: dp[n] = '(' + dp[i] + ')' + dp[n-1-i] for all i in 0..n-1

LeetCode 36 - Valid Sudoku Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(1) time, O(1) space
  • Notes: Fixed 9x9 board. Track digits in rows/cols/boxes using sets. Box index = (row//3)*3 + col//3.

πŸ”„ Bitmask

Variant: bitmask

  • Complexity: O(1) time, O(1) space
  • Delta from base: Uses integers as bitmasks instead of sets for compact storage
  • Notes: Bit i represents digit (i+1). More cache-friendly than hash sets.

LeetCode 43 - Multiply Strings Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(m*n) time, O(m+n) space
  • Notes: Grade-school multiplication with position-based accumulation

Optimized

  • Complexity: O(m*n) time, O(m+n) space
  • Notes: Reversed digit arrays with deferred carry propagation

LeetCode 48 - Rotate Image Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(nΒ²) time, O(1) space
  • Notes: 90Β° clockwise = transpose + reverse rows. Elegant decomposition of rotation.

πŸ”„ Layer

Variant: layer

  • Complexity: O(nΒ²) time, O(1) space
  • Delta from base: Directly rotates 4 elements in a cycle, layer by layer
  • Notes: Process from outer layer to inner. Each position: top->right->bottom->left->top.

LeetCode 49 - Group Anagrams Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n * k log k) time, O(n * k) space
  • Notes: Use sorted string as hash key. All anagrams sort to the same string.

πŸ”„ Count

Variant: count

  • Complexity: O(n * k) time, O(n * k) space
  • Delta from base: Use character count tuple as key instead of sorted string
  • Notes: Faster for long strings. Count is O(k) vs sorting O(k log k).

LeetCode 50 - Pow(x, n) Β· Solution(x, n)

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(log n) time, O(1) space
  • Notes: Iterative binary exponentiation

Recursive

  • Complexity: O(log n) time, O(log n) space
  • Notes: Recursive divide and conquer

LeetCode 52 - N-Queens II Β· Solution

πŸ”΄ Hard β€” 2 approaches

πŸ“ View All Solutions

πŸ”„ Default

Variant: count_only

  • Complexity: O(n!) time, O(n) space
  • Delta from base: Only count solutions instead of building board representations. More memory efficient.
  • Notes: Same algorithm as N-Queens but optimized for counting. Uses hash sets for O(1) constraint checking.

πŸ”„ Bitmask

Variant: bitmask_optimization

  • Complexity: O(n!) time, O(n) space
  • Delta from base: Use integers as bitmasks for ultra-fast constraint checking. Bitwise operations are faster than hash lookups.
  • Notes: Optimized version using bitmasks for constraint tracking. Better cache locality and smaller constants.

LeetCode 54 - Spiral Matrix Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(m*n) time, O(1) space
  • Notes: Process layer by layer: right, down, left, up, then shrink boundaries.

πŸ”„ Direction

Variant: direction

  • Complexity: O(m*n) time, O(m*n) space
  • Delta from base: Turn clockwise when hitting boundary or visited cell
  • Notes: More flexible approach using direction vectors and visited tracking.

LeetCode 66 - Plus One Β· Solution

🟒 Easy β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(n) time, O(1) space
  • Notes: Right-to-left with carry propagation

Pythonic

  • Complexity: O(n) time, O(n) space
  • Notes: Convert to int, add, convert back

LeetCode 72 - Edit Distance Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(m*n) time, O(m*n) space
  • Notes: Levenshtein distance with insert, delete, replace operations

πŸ”„ Space Optimized

Variant: space_optimized

  • Complexity: O(m*n) time, O(min(m,n)) space
  • Delta from base: Use rolling array for O(min(m,n)) space
  • Notes: Space-optimized version using two rows

LeetCode 73 - Set Matrix Zeroes Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(m*n) time, O(1) space
  • Notes: Use first row/column as markers. Handle [0][0] overlap carefully.

πŸ”„ Sets

Variant: sets

  • Complexity: O(m*n) time, O(m+n) space
  • Delta from base: Trade space for simplicity using explicit sets
  • Notes: Collect zero positions in first pass, apply in second pass.

LeetCode 74 - Search a 2D Matrix Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(log(m*n)) time, O(1) space
  • Notes: Treat matrix as 1D array. Index i maps to row=i//n, col=i%n.

πŸ”„ Two Binary

Variant: two_binary

  • Complexity: O(log m + log n) time, O(1) space
  • Delta from base: Find row with binary search, then search within row
  • Notes: Two-phase: locate row, then locate column. Same asymptotic complexity.

LeetCode 78 - Subsets Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n Γ— 2^n) time, O(n) space
  • Notes: 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.

🎯 Bitmask (Base)

Variant: bitmask

  • Complexity: O(n Γ— 2^n) time, O(1) extra space
  • Notes: BASE TEMPLATE for bitmask subset enumeration. Each integer 0 to 2^n-1 represents a unique subset. Decode mask using bit check (mask & (1 << i)).

LeetCode 84 - Largest Rectangle in Histogram Β· Solution

πŸ”΄ Hard β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) time, O(n) space
  • Notes: Single-pass with sentinel for cleaner termination.

πŸ”„ Twopass

Variant: two_pass

  • Complexity: O(n) time, O(n) space
  • Delta from base: Precompute left and right boundaries separately.

LeetCode 85 - Maximal Rectangle Β· Solution

πŸ”΄ Hard β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(rows * cols) time, O(cols) space
  • Delta from base: Build histogram row-by-row, apply 0084 algorithm.
  • Notes: Extends histogram solution to 2D matrix.

πŸ”„ Dp

Variant: dp

  • Complexity: O(rows * cols) time, O(cols) space
  • Delta from base: Track height, left, right boundaries with DP.

LeetCode 94 - Binary Tree Inorder Traversal Β· Solution

🟒 Easy β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

Variant: recursive_dfs

  • Complexity: O(n) time, O(h) space
  • Notes: Base template for tree DFS. Inorder: Left β†’ Node β†’ Right. Fundamental for BST operations.

πŸ”„ Iterative

Variant: iterative_stack

  • Complexity: O(n) time, O(h) space
  • Delta from base: Use explicit stack instead of recursion. Better for deep trees.
  • Notes: Iterative approach avoids stack overflow on deep trees.

LeetCode 98 - Validate Binary Search Tree Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(n) time, O(h) space
  • Notes: Propagate valid range bounds down the tree

Inorder

  • Complexity: O(n) time, O(h) space
  • Notes: In-order traversal must be strictly increasing

LeetCode 100 - Same Tree Β· Solution

🟒 Easy β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(n) time, O(h) space
  • Notes: Recursive DFS comparing structure and values

Iterative

  • Complexity: O(n) time, O(w) space
  • Notes: Iterative BFS with parallel node pairs

LeetCode 105 - Construct Binary Tree from Preorder and Inorder Traversal Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(n) time, O(n) space
  • Notes: Hash map for O(1) inorder lookup; optimal time
  • Complexity: O(n^2) time, O(n) space
  • Notes: Simpler implementation with index() search

LeetCode 115 - Distinct Subsequences Β· Solution

πŸ”΄ Hard β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(mn) time, O(mn) space
  • Notes: dp[i][j] = ways to form t[0:j] from s[0:i]; use or skip each char

πŸ”„ Dp 1D

Variant: dp_1d

  • Complexity: O(mn) time, O(n) space
  • Delta from base: Space optimization to O(n) by processing right-to-left
  • Notes: Row-by-row DP with single array, reverse iteration preserves dependencies

LeetCode 127 - Word Ladder Β· Solution

πŸ”΄ Hard β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(n * m^2) time, O(n * m) space
  • Notes: BFS with pattern-based neighbor lookup

Bidirectional

  • Complexity: O(n * m^2) time, O(n * m) space
  • Notes: Bidirectional BFS for reduced search space

LeetCode 128 - Longest Consecutive Sequence Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(n) time, O(n) space
  • Notes: Only count from sequence starts (no predecessor)

Union Find

  • Complexity: O(n) time, O(n) space
  • Notes: Union consecutive elements, find max component

LeetCode 130 - Surrounded Regions Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(m*n) time, O(m*n) space
  • Notes: DFS from borders to mark safe O's, then flip remaining

Bfs

  • Complexity: O(m*n) time, O(m*n) space
  • Notes: BFS from border O's using queue; avoids deep recursion

LeetCode 131 - Palindrome Partitioning Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

Variant: dp_precomputed_palindrome

  • Complexity: O(n Γ— 2^n) time, O(n^2) space
  • Notes: Backtracking with DP-precomputed palindrome table. Precompute is_palindrome[i][j] for O(1) checks. At each position, try all valid (palindrome) prefixes. O(n^2) preprocessing dominated by O(n Γ— 2^n) backtracking.

πŸ”„ Naive

Variant: on_the_fly_checking

  • Complexity: O(n Γ— 2^n Γ— n) time, O(n) space
  • Delta from base: Check palindrome during backtracking (no preprocessing). Simpler code but slower for repeated checks.
  • Notes: Alternative approach with on-the-fly palindrome checking. Demonstrates trade-off between preprocessing and runtime checking.

LeetCode 133 - Clone Graph Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

Variant: dfs_hash_map

  • Complexity: O(V+E) time, O(V) space
  • Notes: Map oldβ†’new nodes. On first visit create clone, on revisit return existing clone. Handles cycles via the mapping.

πŸ”„ Bfs

Variant: bfs_hash_map

  • Complexity: O(V+E) time, O(V) space
  • Delta from base: Iterative BFS instead of recursive DFS.
  • Notes: Create clone when discovered, connect neighbors when processing.

LeetCode 136 - Single Number Β· Solution

🟒 Easy β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(n) time, O(1) space
  • Notes: XOR all elements; pairs cancel to zero

Reduce

  • Complexity: O(n) time, O(1) space
  • Notes: Functional reduce with XOR operator

LeetCode 138 - Copy List with Random Pointer Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(n) time, O(n) space
  • Notes: Hash map for original -> copy mapping

Interweave

  • Complexity: O(n) time, O(1) space
  • Notes: Interweave copies with originals; three-pass algorithm

LeetCode 143 - Reorder List Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(n) time, O(1) space
  • Notes: Find middle with slow/fast, reverse second half, merge

Array

  • Complexity: O(n) time, O(n) space
  • Notes: Store nodes in array for random access reordering

LeetCode 146 - LRU Cache Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(1) per operation
  • Notes: OrderedDict with move_to_end for recency tracking

Manual

  • Complexity: O(1) per operation
  • Notes: HashMap + Doubly linked list with dummy head/tail

LeetCode 150 - Evaluate Reverse Polish Notation Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(n) time, O(n) space
  • Notes: Stack-based with explicit operator handling

Lambda

  • Complexity: O(n) time, O(n) space
  • Notes: Lambda dispatch table for cleaner operator handling

LeetCode 152 - Maximum Product Subarray Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) time, O(1) space
  • Notes: Track both min and max. Negative can flip min to max.

πŸ”„ Prefix Suffix

Variant: prefix_suffix

  • Complexity: O(n) time, O(1) space
  • Delta from base: Compute prefix and suffix products, reset at zeros
  • Notes: Max is either a prefix or suffix. Zeros split into subarrays.

LeetCode 153 - Find Minimum in Rotated Sorted Array Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(log n) time, O(1) space
  • Notes: Compare mid with right. If mid > right, min is in right half.

πŸ”„ Find Pivot

Variant: find_pivot

  • Complexity: O(log n) time, O(1) space
  • Delta from base: Explicitly find the pivot point where rotation occurred
  • Notes: Pivot is where nums[i] > nums[i+1]. Minimum is at pivot+1.

LeetCode 155 - Min Stack Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(1) per operation, O(n) space
  • Notes: Two stacks: values and minimum history

Tuple

  • Complexity: O(1) per operation, O(n) space
  • Notes: Single stack with (value, current_min) pairs

LeetCode 190 - Reverse Bits Β· Solution

🟒 Easy β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(1) time, O(1) space
  • Notes: Iterate through 32 bits extracting and placing each

Divide Conquer

  • Complexity: O(1) time, O(1) space
  • Notes: Swap 16-bit halves, then 8-bit, 4-bit, 2-bit, 1-bit

LeetCode 191 - Number of 1 Bits Β· Solution

🟒 Easy β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(k) time, O(1) space
  • Notes: Brian Kernighan's algorithm; k = number of set bits

Iterate

  • Complexity: O(32) time, O(1) space
  • Notes: Check each bit position; fixed 32 iterations

LeetCode 199 - Binary Tree Right Side View Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(n) time, O(w) space
  • Notes: BFS level-order; rightmost node is last in each level

Dfs

  • Complexity: O(n) time, O(h) space
  • Notes: DFS right-first; first node at each depth is rightmost

LeetCode 200 - Number of Islands Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

Variant: dfs_flood_fill

  • Complexity: O(m*n) time, O(m*n) space
  • Notes: Base template for connected components. DFS flood fill marks entire island, count equals number of DFS calls from unvisited cells.

πŸ”„ Bfs

Variant: bfs_flood_fill

  • Complexity: O(m*n) time, O(min(m,n)) space
  • Delta from base: Use BFS instead of DFS. Better for avoiding stack overflow on large grids.
  • Notes: BFS approach with explicit queue. Queue size bounded by min(m,n).

LeetCode 207 - Course Schedule Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(V+E) time, O(V+E) space
  • Notes: Base template for topological sort using Kahn's algorithm.

Dfs

  • Complexity: O(V+E) time, O(V+E) space
  • Notes: DFS three-color cycle detection.

LeetCode 208 - Implement Trie (Prefix Tree) Β· Solution(Prefix Tree)

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(L) time per operation, O(N*L) space
  • Notes: Base template for Trie operations.

Array

  • Complexity: O(L) time per operation, O(N*L*26) space
  • Notes: Fixed-size array for children (faster lookup).

LeetCode 210 - Course Schedule II Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(V+E) time, O(V+E) space
  • Notes: Collect nodes during Kahn's traversal.

Dfs

  • Complexity: O(V+E) time, O(V+E) space
  • Notes: DFS postorder with reverse at end.

LeetCode 211 - Design Add and Search Words Data Structure Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(L) add, O(26^D * L) search with D wildcards
  • Notes: DFS for '.' wildcard support.

Iterative

  • Complexity: O(L) add, O(26^D * L) search with D wildcards
  • Notes: Iterative with explicit stack.

LeetCode 212 - Word Search II Β· Solution

πŸ”΄ Hard β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(M*N*4^L) time, O(W*L) space
  • Notes: Trie + backtracking with branch pruning.

Simple

  • Complexity: O(M*N*4^L) time, O(W*L) space
  • Notes: Simpler version without pruning optimization.

LeetCode 217 - Contains Duplicate Β· Solution

🟒 Easy β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(n) time, O(n) space
  • Notes: HashSet for O(1) lookup

Sorting

  • Complexity: O(n log n) time, O(1) space
  • Notes: Sort and check adjacent pairs

LeetCode 218 - The Skyline Problem Β· Solution

πŸ”΄ Hard β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n log n) time, O(n) space
  • Notes: Max-heap with lazy deletion for tracking active building heights.

πŸ”„ Sortedlist

Variant: sorted_container

  • Complexity: O(n log n) time, O(n) space
  • Delta from base: Use SortedList for O(log n) add/remove/max instead of lazy heap deletion.
  • Notes: Cleaner implementation with sortedcontainers, same complexity.

LeetCode 226 - Invert Binary Tree Β· Solution

🟒 Easy β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(n) time, O(h) space
  • Notes: Recursive DFS with child swap at each node

Iterative

  • Complexity: O(n) time, O(w) space
  • Notes: Iterative BFS level-order traversal with swap

LeetCode 230 - Kth Smallest Element in a BST Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(H + k) time, O(H) space
  • Notes: Iterative inorder with early termination; optimal when k << n

Recursive

  • Complexity: O(n) time, O(H) space
  • Notes: Recursive inorder collecting all elements

LeetCode 235 - Lowest Common Ancestor of a Binary Search Tree Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(h) time, O(1) space
  • Notes: Iterative traversal to split point; constant space

Recursive

  • Complexity: O(h) time, O(h) space
  • Notes: Recursive BST traversal; implicit call stack

LeetCode 238 - Product of Array Except Self Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) time, O(1) space (excluding output)
  • Notes: Two-pass: prefix products forward, suffix products backward.

πŸ”„ Two Pass

Variant: educational

  • Complexity: O(n) time, O(n) space
  • Delta from base: Separate prefix and suffix arrays for clarity.
  • Notes: More intuitive but uses extra space.

LeetCode 242 - Valid Anagram Β· Solution

🟒 Easy β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(n) time, O(1) space
  • Notes: Counter comparison; O(1) space since alphabet is fixed

Sorting

  • Complexity: O(n log n) time, O(n) space
  • Notes: Sort both strings and compare

LeetCode 253 - Meeting Rooms II Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n log n) time, O(n) space
  • Notes: Min-heap of end times for greedy room assignment. Classic interval scheduling pattern.

πŸ”„ Sweep

Variant: sweep_line

  • Complexity: O(n log n) time, O(n) space
  • Delta from base: Track events (+1 start, -1 end) and find max concurrent.
  • Notes: Sweep line approach: sort events, track concurrent meetings.

LeetCode 261 - Graph Valid Tree Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(V + E) time, O(V + E) space
  • Notes: DFS checking connectivity and no cycles

Union Find

  • Complexity: O(V + E * alpha(V)) time, O(V) space
  • Notes: Union-Find detecting cycles during edge merges

LeetCode 268 - Missing Number Β· Solution

🟒 Easy β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(n) time, O(1) space
  • Notes: XOR indices and values; matching pairs cancel

Sum

  • Complexity: O(n) time, O(1) space
  • Notes: Gauss formula n*(n+1)/2 minus actual sum

LeetCode 269 - Alien Dictionary Β· Solution

πŸ”΄ Hard β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(C) time, O(1) space
  • Notes: BFS topological sort (Kahn's algorithm)

Dfs

  • Complexity: O(C) time, O(1) space
  • Notes: DFS topological sort with cycle detection

LeetCode 297 - Serialize and Deserialize Binary Tree Β· Solution

πŸ”΄ Hard β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(n) time, O(n) space
  • Notes: Pre-order DFS with 'N' markers for null; unique reconstruction

Bfs

  • Complexity: O(n) time, O(n) space
  • Notes: Level-order BFS serialization; processes nodes breadth-first

LeetCode 300 - Longest Increasing Subsequence Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(n log n) time, O(n) space
  • Notes: Binary search with patience sorting tails array

Dp

  • Complexity: O(n^2) time, O(n) space
  • Notes: DP where dp[i] = LIS length ending at index i

LeetCode 307 - Range Sum Query - Mutable Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n log n) build, O(log n) ops
  • Delta from base: Canonical Fenwick Tree for range sum with point updates.
  • Notes: Base template for all BIT problems.

πŸ”„ Segment Tree

Variant: segment_tree

  • Complexity: O(n) build, O(log n) ops
  • Delta from base: Segment Tree alternative - more flexible for range min/max.
  • Notes: Better for range min/max queries.

LeetCode 309 - Best Time to Buy and Sell Stock with Cooldown Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) time, O(1) space
  • Notes: 3 states (hold/sold/rest) with transitions capturing cooldown

πŸ”„ Dp 2D

Variant: dp_2d

  • Complexity: O(n) time, O(n) space
  • Delta from base: Explicit 2D DP array with hold/not-hold, cooldown via dp[i-2]
  • Notes: Classic DP formulation, easier to understand transitions

LeetCode 315 - Count of Smaller Numbers After Self Β· Solution

πŸ”΄ Hard β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n log n) time, O(n) space
  • Delta from base: BIT with coordinate compression, process right-to-left.
  • Notes: Classic inversion counting pattern.

πŸ”„ Merge Sort

Variant: merge_sort

  • Complexity: O(n log n) time, O(n) space
  • Delta from base: Merge sort with inversion counting during merge.
  • Notes: Alternative approach using divide and conquer.

LeetCode 329 - Longest Increasing Path in a Matrix Β· Solution

πŸ”΄ Hard β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(mn) time, O(mn) space
  • Notes: DFS with memoization exploiting DAG structure (no cycles due to strict increase)

πŸ”„ Topological

Variant: topological

  • Complexity: O(mn) time, O(mn) space
  • Delta from base: BFS from endpoints (outdegree 0), count levels for path length
  • Notes: Alternative view: longest path in DAG via topological sort

LeetCode 332 - Reconstruct Itinerary Β· Solution

πŸ”΄ Hard β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(E log E) time, O(E) space
  • Notes: Hierholzer's algorithm: DFS with post-order collection, reverse for path

πŸ”„ Iterative

Variant: iterative

  • Complexity: O(E log E) time, O(E) space
  • Delta from base: Explicit stack instead of recursion, avoids stack overflow
  • Notes: Same algorithm, different control flow for deep graphs

LeetCode 337 - House Robber III Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) time, O(h) space
  • Notes: Tree DP with include/exclude states. Base template for tree DP pattern.

πŸ”„ Memo

Variant: memoization

  • Complexity: O(n) time, O(n) space
  • Delta from base: Use memoization dict instead of tuple states.
  • Notes: Alternative using explicit memoization.

LeetCode 338 - Counting Bits Β· Solution

🟒 Easy β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(n) time, O(n) space
  • Notes: ans[i] = ans[i>>1] + (i&1); uses relation with i/2

Dp Lowest

  • Complexity: O(n) time, O(n) space
  • Notes: ans[i] = ans[i&(i-1)] + 1; removes lowest set bit

LeetCode 340 - Longest Substring with At Most K Distinct Characters Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

πŸ”„ Default

Variant: at_most_k_distinct

  • Complexity: O(n) time, O(K) space
  • Delta from base: Replace 'unique' with 'distinct count <= K'. Use frequency map instead of last-seen-index.
  • Notes: Cannot use jump optimization - must shrink incrementally. Uses len(freq_map) as distinct count.

πŸ”„ Two Distinct

Variant: k_equals_2

  • Complexity: O(n) time, O(1) space
  • Delta from base: Special case: K=2 (LeetCode 159)
  • Notes: Direct specialization for LeetCode 159. Hardcodes K=2.

LeetCode 347 - Top K Frequent Elements Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

πŸ”„ Default

Variant: frequency_based

  • Complexity: O(n + m log k) time, O(m + k) space
  • Delta from base: Sort by frequency instead of value; use Counter for frequency map.
  • Notes: Min-heap of size k sorted by frequency. Two-phase: count then select.

πŸ”„ Bucket

Variant: bucket_sort

  • Complexity: O(n) time, O(n) space
  • Delta from base: O(n) bucket sort when frequency is bounded.
  • Notes: Bucket sort by frequency. Index = frequency, collect from highest bucket.

LeetCode 355 - Design Twitter Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(1) post/follow/unfollow, O(k log k) getNewsFeed
  • Notes: Min-heap merge of k sorted tweet lists from followed users

πŸ”„ Simple

Variant: simple

  • Complexity: O(1) post/follow/unfollow, O(n log n) getNewsFeed
  • Delta from base: Collect all tweets and sort instead of heap merge
  • Notes: Simpler implementation, trades efficiency for clarity

LeetCode 371 - Sum of Two Integers Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(1) time, O(1) space
  • Notes: XOR for sum without carry, AND<<1 for carry, iterate until carry=0

πŸ”„ Recursive

Variant: recursive

  • Complexity: O(1) time, O(1) space
  • Delta from base: Same algorithm expressed recursively instead of iteratively
  • Notes: Elegant recursive formulation, tail-recursive style

LeetCode 417 - Pacific Atlantic Water Flow Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

πŸ”„ Default

Variant: reverse_multi_source_bfs

  • Complexity: O(m*n) time, O(m*n) space
  • Delta from base: Reverse direction: start from ocean borders, go uphill. Intersection of two reachability sets.
  • Notes: Key insight: instead of checking if each cell reaches ocean, check which cells are reachable FROM ocean going uphill.

πŸ”„ Dfs

Variant: reverse_multi_source_dfs

  • Complexity: O(m*n) time, O(m*n) space
  • Delta from base: DFS instead of BFS for reachability.
  • Notes: Recursive DFS from each ocean border.

LeetCode 424 - Longest Repeating Character Replacement Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(n) time, O(26) space
  • Notes: Track maxFreq; window valid if (size - maxFreq) <= k

Binary Search

  • Complexity: O(n log n) time, O(26) space
  • Notes: Binary search on window length, check feasibility

LeetCode 496 - Next Greater Element I Β· Solution

🟒 Easy β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n + m) time, O(n) space
  • Notes: Canonical monotonic stack template with hash map for lookup.

πŸ”„ Brute

Variant: brute_force

  • Complexity: O(m * n) time, O(1) space
  • Delta from base: Linear scan for each element.

LeetCode 503 - Next Greater Element II Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) time, O(n) space
  • Delta from base: Two-pass traversal for circular array.
  • Notes: Traverse 2n elements, only push during first n.

πŸ”„ Concat

Variant: virtual_concat

  • Complexity: O(n) time, O(n) space
  • Delta from base: Conceptually double the array using modulo.

LeetCode 516 - Longest Palindromic Subsequence Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

πŸ”„ Default

Variant: lcs_reduction

  • Complexity: O(n^2) time, O(n^2) space
  • Delta from base: LPS(s) = LCS(s, reverse(s))
  • Notes: Reduce to LCS by comparing string with its reverse

πŸ”„ Interval Dp

Variant: interval_dp

  • Complexity: O(n^2) time, O(n^2) space
  • Delta from base: Direct interval DP: dp[i][j] = LPS of s[i:j+1]
  • Notes: Interval DP approach filling by increasing length

LeetCode 542 - 01 Matrix Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

πŸ”„ Default

Variant: distance_field

  • Complexity: O(m*n) time, O(m*n) space
  • Delta from base: Return new distance matrix instead of modifying grid.
  • Notes: Multi-source BFS from zeros, compute distance field.

πŸ”„ Dp

Variant: two_pass_dp

  • Complexity: O(m*n) time, O(1) extra space
  • Delta from base: Alternative DP approach: two passes (top-left, bottom-right).
  • Notes: Does not use BFS; uses DP recurrence from four directions.

LeetCode 572 - Subtree of Another Tree Β· Solution

🟒 Easy β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(m * n) time, O(h) space
  • Notes: DFS traversal checking isSameTree at each node

Serialization

  • Complexity: O(m + n) time, O(m + n) space
  • Notes: Serialize both trees, check string containment

LeetCode 621 - Task Scheduler Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

πŸ”„ Default

Variant: math_formula

  • Complexity: O(n) time, O(1) space
  • Delta from base: Direct calculation based on max frequency.
  • Notes: Mathematical formula: (max_freq - 1) * (n + 1) + max_freq_count.

🎯 Heap (Base)

  • Complexity: O(n * m) time, O(k) space
  • Notes: Greedy scheduling with max-heap. Always pick most frequent available task.

LeetCode 648 - Replace Words Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(D*L + W*L) time, O(D*L) space
  • Notes: Find shortest matching prefix via trie.

Hashset

  • Complexity: O(W*L^2) time, O(D*L) space
  • Notes: Hash set approach - simpler but slower.

LeetCode 678 - Valid Parenthesis String Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(n) time, O(1) space
  • Notes: Track min/max open count range; valid if 0 is achievable

Two Pass

  • Complexity: O(n) time, O(1) space
  • Notes: Lβ†’R validates ), Rβ†’L validates (; both must pass

LeetCode 703 - Kth Largest Element in a Stream Β· Solution

🟒 Easy β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(n log k) time, O(k) space
  • Notes: Min-heap of size k; root is always the k-th largest

Bisect

  • Complexity: O(n^2) time, O(n) space
  • Notes: Sorted list with bisect.insort; simpler but slower insertion

LeetCode 704 - Binary Search Β· Solution

🟒 Easy β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(log n) time, O(1) space
  • Notes: Classic iterative binary search

Recursive

  • Complexity: O(log n) time, O(log n) space
  • Notes: Recursive divide and conquer

LeetCode 739 - Daily Temperatures Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) time, O(n) space
  • Delta from base: Track distance instead of value.
  • Notes: Monotonic decreasing stack for span/distance calculation.

πŸ”„ Backward

Variant: backward_scan

  • Complexity: O(n) time, O(1) space
  • Delta from base: Scan backward with jump optimization.

LeetCode 763 - Partition Labels Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) time, O(1) space
  • Notes: Track last occurrence, extend partition boundary greedily

πŸ”„ Merge Intervals

Variant: merge_intervals

  • Complexity: O(n) time, O(1) space
  • Delta from base: View as interval merge problem: each char defines [first, last] interval
  • Notes: Reduces to merge intervals, overlapping char ranges = same partition

LeetCode 785 - Is Graph Bipartite? Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

Variant: bfs_coloring

  • Complexity: O(V+E) time, O(V) space
  • Notes: BFS naturally alternates levels. Color each level with opposite color. Conflict = not bipartite.

πŸ”„ Dfs

Variant: dfs_coloring

  • Complexity: O(V+E) time, O(V) space
  • Delta from base: Recursive DFS instead of BFS.
  • Notes: Pass color to recursive call, check for conflict on already-colored neighbors.

LeetCode 787 - Cheapest Flights Within K Stops Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(K * E) time, O(V) space
  • Notes: Bellman-Ford variant: K iterations for at most K edges.

Dijkstra

  • Complexity: O((V*K) log(V*K)) time, O(V*K) space
  • Notes: State-space Dijkstra: state = (node, stops_used).

LeetCode 802 - Find Eventual Safe States Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

Default

  • Complexity: O(V+E) time, O(V) space
  • Notes: DFS three-color safe node detection.

Reverse Kahn

  • Complexity: O(V+E) time, O(V+E) space
  • Notes: Reverse graph + Kahn's from terminals.

LeetCode 841 - Keys and Rooms Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

πŸ”„ Default

Variant: dfs_reachability

  • Complexity: O(V+E) time, O(V) space
  • Delta from base: Single-source reachability check. Return visited count equals total.
  • Notes: Keys in room = directed edges. Check if all nodes reachable from node 0.

πŸ”„ Bfs

Variant: bfs_reachability

  • Complexity: O(V+E) time, O(V) space
  • Delta from base: BFS instead of DFS for reachability.
  • Notes: Iterative BFS traversal.

LeetCode 846 - Hand of Straights Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n log n) time, O(n) space
  • Notes: Greedy: smallest card must start a group, decrement counts for consecutive

πŸ”„ Ordered Dict

Variant: heap

  • Complexity: O(n log n) time, O(n) space
  • Delta from base: Process via sorted unique values with explicit iteration
  • Notes: Same logic, different iteration style using sorted keys

LeetCode 853 - Car Fleet Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n log n) time, O(n) space
  • Notes: Sort by position desc, count how many times arrival time increases (new fleet)

πŸ”„ Stack

Variant: stack

  • Complexity: O(n log n) time, O(n) space
  • Delta from base: Explicit monotonic stack tracking fleet leaders
  • Notes: Stack stores arrival times of fleet leaders (decreasing sequence)

LeetCode 907 - Sum of Subarray Minimums Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) time, O(n) space
  • Delta from base: Count contribution using left_count * right_count.
  • Notes: Each element contributes arr[i] * left * right to sum.

πŸ”„ Single

Variant: single_pass

  • Complexity: O(n) time, O(n) space
  • Delta from base: Compute contribution on-pop instead of precomputing boundaries.

LeetCode 922 - Sort Array By Parity II Β· Solution

🟒 Easy β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) time, O(1) space
  • Delta from base: Interleaved partition: even indices must have even values, odd indices must have odd values.
  • Notes: Two pointers: one for even positions, one for odd positions. Swap when mismatch found.

🎯 Two Pointers (Base)

  • Complexity: O(n) time, O(1) space
  • Delta from base: Interleaved partition: even indices must have even values, odd indices must have odd values.
  • Notes: Same as default. Two independent pointers for even and odd positions.

LeetCode 968 - Binary Tree Cameras Β· Solution

πŸ”΄ Hard β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) time, O(h) space
  • Notes: Greedy with 3-state machine: not_covered, covered, has_camera.

πŸ”„ Dp

Variant: explicit_dp

  • Complexity: O(n) time, O(h) space
  • Delta from base: Explicit DP with 3 states instead of greedy transitions.
  • Notes: More explicit state representation, same complexity.

LeetCode 981 - Time Based Key-Value Store Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(1) set, O(log n) get
  • Notes: HashMap + bisect_right; timestamps increasing so list is sorted

Variant: manual

  • Complexity: O(1) set, O(log n) get
  • Delta from base: Explicit binary search instead of bisect module
  • Notes: Educational: shows binary search logic for rightmost valid entry

LeetCode 1094 - Car Pooling Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

πŸ”„ Default

Variant: range_update

  • Complexity: O(n + m) time, O(m) space
  • Delta from base: Check capacity constraint instead of computing final values.
  • Notes: Difference array for range updates, prefix sum to verify capacity.

πŸ”„ Events

Variant: event_based

  • Complexity: O(n log n) time, O(n) space
  • Delta from base: Sort events and simulate pickup/dropoff.
  • Notes: Event-based approach: sort by location, process in order.

LeetCode 1143 - Longest Common Subsequence Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(m*n) time, O(m*n) space
  • Notes: Classic 2D DP for comparing two strings

πŸ”„ Space Optimized

Variant: space_optimized

  • Complexity: O(m*n) time, O(min(m,n)) space
  • Delta from base: Use rolling array for O(min(m,n)) space
  • Notes: Space-optimized version using two rows

LeetCode 1448 - Count Good Nodes in Binary Tree Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) time, O(h) space
  • Notes: DFS passing max-so-far as parameter, node is good if val >= max

πŸ”„ Bfs

Variant: bfs

  • Complexity: O(n) time, O(w) space
  • Delta from base: Level-order traversal storing (node, max_on_path) in queue
  • Notes: BFS alternative, useful when DFS stack depth is a concern

LeetCode 1851 - Minimum Interval to Include Each Query Β· Solution

πŸ”΄ Hard β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O((n + q) log n) time, O(n + q) space
  • Notes: Sort intervals by start, process queries in sorted order with min-heap

πŸ”„ Offline

Variant: offline

  • Complexity: O((n + q) log n) time, O(n + q) space
  • Delta from base: Emphasizes lazy deletion pattern
  • Notes: Same algorithm with explicit lazy deletion semantics

LeetCode 1899 - Merge Triplets to Form Target Triplet Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) time, O(1) space
  • Notes: Filter valid triplets (all values <= target), track which positions match exactly

πŸ”„ Set

Variant: set

  • Complexity: O(n) time, O(1) space
  • Delta from base: Uses set to track matched positions, generalizes to k-tuples
  • Notes: Elegant set-based approach that extends naturally to higher dimensions

LeetCode 2013 - Detect Squares Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(1) add, O(n) count
  • Notes: Iterate potential diagonal corners, verify other two corners exist

πŸ”„ By X

Variant: by_x

  • Complexity: O(1) add, O(n) count
  • Delta from base: Group points by x-coordinate for cleaner iteration
  • Notes: Look at vertical neighbors first, then check horizontal

LeetCode 2104 - Sum of Subarray Ranges Β· Solution

🟑 Medium β€” 2 approaches

πŸ“ View All Solutions

🎯 Default (Base)

  • Complexity: O(n) time, O(n) space
  • Delta from base: Sum of max - sum of min using dual stacks.
  • Notes: Extends 907 by computing both min and max contributions.

πŸ”„ Brute

Variant: brute_force

  • Complexity: O(n^2) time, O(1) space
  • Delta from base: Enumerate all subarrays and track running min/max.
  • Notes: Acceptable for n <= 1000.