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.