Skip to content

Solution Variants

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

LeetCode 977 - Squares of a Sorted Array

🟒 Easy β€” 5 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.

πŸ”„ Absolute

Variant: absolute_value

  • Complexity: O(n) time, O(n) space
  • Delta from base: Compare absolute values instead of squares for efficiency.
  • Notes: Compare absolute values to avoid squaring until writing.

πŸ”„ Split

Variant: split_merge

  • Complexity: O(n) time, O(n) space
  • Delta from base: Explicitly split into negative and positive parts, then merge.
  • Notes: Explicit split-then-merge approach.

πŸ”„ Sort

Variant: sort

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

πŸ”„ Deque

Variant: deque

  • Complexity: O(n) time, O(n) space
  • Delta from base: Use deque for O(1) append from both ends.
  • Notes: Deque-based approach for efficient appending from both ends.

LeetCode 23 - Merge k Sorted Lists

πŸ”΄ 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 75 - Sort Colors

🟑 Medium β€” 4 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).

πŸ”„ Counting

Variant: counting_sort

  • Complexity: O(n) time, O(1) space
  • Delta from base: Two-pass counting sort approach.
  • Notes: Counting sort variant: count frequencies then fill array.

πŸ”„ Twopartition

Variant: two_pass_partition

  • Complexity: O(n) time, O(1) space
  • Delta from base: Two-pass approach: first separate 0s, then separate 1s and 2s.
  • Notes: Two-pass partitioning approach.

πŸ”„ Generic

Variant: generic_k_way

  • Complexity: O(n) time, O(1) space
  • Delta from base: Generic K-way partition (extensible to more colors).
  • Notes: Generalized version for K-way partition.

LeetCode 202 - Happy Number

🟒 Easy β€” 4 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.

πŸ”„ 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 approach: store seen numbers, detect cycle when number repeats.

πŸ”„ Hardcoded

Variant: hardcoded_cycle

  • Complexity: O(log n) time, O(1) space
  • Delta from base: Hardcode known cycle values (4, 16, 37, 58, 89, 145, 42, 20).
  • Notes: Optimization: check if sequence enters known cycle (4 β†’ 16 β†’ ... β†’ 4).

πŸ”„ String

Variant: string_conversion

  • Complexity: O(log n) time, O(log n) space
  • Delta from base: Convert number to string to extract digits.
  • Notes: String-based digit extraction approach.

LeetCode 215 - Kth Largest Element in an Array

🟑 Medium β€” 4 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.

πŸ”„ 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 approach: maintain heap of size K, return root.

πŸ”„ Sort

Variant: sort

  • Complexity: O(n log n) time, O(1) space
  • Delta from base: Sort array and return kth element from end.
  • Notes: Simple sorting approach: sort and return nums[-k].

πŸ”„ Counting

Variant: counting_sort

  • Complexity: O(n + m) time, O(m) space where m is range size
  • Delta from base: Counting sort for bounded range values.
  • Notes: Counting sort approach: only works when value range is bounded.

LeetCode 283 - Move Zeroes

🟒 Easy β€” 4 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.

πŸ”„ 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 with early termination.
  • Notes: Optimized swap version with reduced operations.

πŸ”„ Snowball

Variant: snowball

  • Complexity: O(n) time, O(1) space
  • Delta from base: Snowball technique: accumulate zeros and swap with non-zero.
  • Notes: Snowball approach: accumulate zeros, swap when encountering non-zero.

LeetCode 876 - Middle of the Linked List

🟒 Easy β€” 4 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.

πŸ”„ Twopass

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 approach: first count nodes, then traverse to middle.

πŸ”„ Array

Variant: array_storage

  • Complexity: O(n) time, O(n) space
  • Delta from base: Store nodes in array, return middle element.
  • Notes: Store all nodes in array, return middle element.

πŸ”„ Firstmiddle

Variant: first_middle

  • Complexity: O(n) time, O(1) space
  • Delta from base: Returns first middle node for even-length lists (different termination condition).
  • Notes: Variant that returns first middle node for even-length lists.

LeetCode 905 - Sort Array By Parity

🟒 Easy β€” 4 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.

πŸ”„ Writer

Variant: same_direction

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

πŸ”„ Newarray

Variant: new_array

  • Complexity: O(n) time, O(n) space
  • Delta from base: Create new array instead of in-place partition.
  • Notes: Two-pass approach: collect evens then odds into new array.

πŸ”„ Sort

Variant: sort

  • Complexity: O(n log n) time, O(1) space
  • Delta from base: Sort by parity using custom comparator.
  • Notes: Sorting approach: sort by (num % 2).

LeetCode 922 - Sort Array By Parity II

🟒 Easy β€” 4 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.

πŸ”„ Singlepass

Variant: single_pass

  • Complexity: O(n) time, O(1) space
  • Delta from base: Single-pass optimization with better pointer management.
  • Notes: Optimized single-pass approach.

πŸ”„ Newarray

Variant: new_array

  • Complexity: O(n) time, O(n) space
  • Delta from base: Create new array with correct placement.
  • Notes: Two-pass approach: collect evens and odds separately, then interleave.

πŸ”„ Twopass

Variant: two_pass

  • Complexity: O(n) time, O(1) space
  • Delta from base: Two-pass: first fix even positions, then odd positions.
  • Notes: Two-pass approach: separate fixing of even and odd positions.

LeetCode 3 - Longest Substring Without Repeating Characters

🟑 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 21 - Merge Two Sorted Lists

🟒 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.

πŸ”„ Recursive

Variant: recursive

  • Complexity: O(m + n) time, O(m + n) space
  • Delta from base: Recursive approach instead of iterative.
  • Notes: Recursive merge pattern.

πŸ”„ Inplace

Variant: in_place

  • Complexity: O(m + n) time, O(1) space
  • Delta from base: In-place merge without dummy node.
  • Notes: Alternative in-place implementation without dummy head.

LeetCode 25 - Reverse Nodes in k-Group

πŸ”΄ 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

🟒 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.

πŸ”„ Enumerate

Variant: enumerate

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

πŸ”„ Template

Variant: template

  • Complexity: O(n) time, O(1) space
  • Delta from base: Template-based approach for clarity.
  • Notes: Template version demonstrating the pattern structure.

LeetCode 125 - Valid Palindrome

🟒 Easy β€” 3 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.

πŸ”„ Prefilter

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

Variant: filtered_pointers

  • Complexity: O(n) time, O(1) space
  • Delta from base: Uses helper functions for character validation.
  • Notes: Cleaner implementation with helper functions.

LeetCode 680 - Valid Palindrome II

🟒 Easy β€” 3 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.

πŸ”„ Kskips

Variant: k_skips

  • Complexity: O(n) time, O(1) space
  • Delta from base: Generalized to allow K skips (extensible version).
  • Notes: Generalized version supporting K character skips.

πŸ”„ Iterative

Variant: iterative

  • Complexity: O(n) time, O(1) space
  • Delta from base: Iterative helper function instead of recursive.
  • Notes: Iterative implementation of palindrome check helper.

LeetCode 15 - 3Sum

🟑 Medium β€” 2 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.

πŸ”„ Set

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

🟑 Medium β€” 2 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.

πŸ”„ Optimized

Variant: early_termination

  • Complexity: O(nΒ²) time, O(1) extra space
  • Delta from base: Early termination when exact match found.
  • Notes: Optimized version with early return on exact match.

LeetCode 340 - Longest Substring with At Most K Distinct Characters

🟑 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.