PYTHON 3.14 WEBASSEMBLY RUNTIME
Problem Workspace
Study pattern guides and execute solutions against verified test cases for all 540 coding interview problems across 28 patterns, or open full written and video courses across all 52 system design modules.
EXECUTABLE PYTHON 3.14 WORKSPACE
0 / 540 solved540 Matching Problems
01.01
Valid Palindrome ★ FREETwo Pointers · Spot it: compare with its mirror → pointers at both ends
O(n) time · O(1) space01.02
3Sum ★ FREETwo Pointers · Spot it: exact sum of 3 → sort, fix one, two pointers
O(n²) time · O(1) extra space01.03
Remove Nth Node from End of List ★ FREETwo Pointers · Spot it: k-th from the end → two pointers k apart
O(n) time · O(1) space01.04
Sort Colors ★ FREETwo Pointers · Spot it: few distinct values → zones with low, mid, high
O(n) time · O(1) space01.05
Reverse Words in a String ★ FREETwo Pointers · Spot it: reverse in place → swap from both ends
O(n) time · O(n) space01.06
Valid Word Abbreviation ★ FREETwo Pointers · Spot it: two inputs, each at its own pace → i and j
O(n) time · O(1) space01.07
Strobogrammatic Number ★ FREETwo Pointers · Spot it: mirror with a twist → both ends + a lookup
O(n) time · O(1) space01.08
Minimum Number of Moves to Make Palindrome ★ FREETwo Pointers · Spot it: neighbor swaps → match ends, nearest first
O(n²) time · O(n) space01.09
Next Palindrome Using Same Digits ★ FREETwo Pointers · Spot it: next larger arrangement → next permutation
O(n) time · O(n) space01.10
Lowest Common Ancestor of a Binary Tree III ★ FREETwo Pointers · Spot it: unequal paths → swap starts and walk again
O(h) time · O(1) space01.11
Count Pairs Whose Sum is Less than Target ★ FREETwo Pointers · Spot it: count pairs under a limit → sort, count in blocks
O(n log n) time · O(1) space01.12
Count Subarrays With Fixed Bounds ★ FREETwo Pointers · Spot it: count subarrays → fix the end, track last positions
O(n) time · O(1) space01.13
Get the Maximum Score ★ FREETwo Pointers · Spot it: sorted lists + shared points → walk both, pick best
O(n + m) time · O(1) space01.14
Find the Lexicographically Largest String From Box II ★ FREETwo Pointers · Spot it: largest suffix → best and challenger pointers
O(n) time · O(1) space01.15
Create Maximum Number ★ FREETwo Pointers
O(k·(m + n + k²)) time · O(m + n) space01.16
Append Characters to String to Make Subsequence ★ FREETwo Pointers
O(n + m) time · O(1) space01.19
Intersection of Two Linked Lists ★ FREETwo Pointers · Spot it: paths of unequal length meet → switch heads
O(n + m) time · O(1) space02.01
Happy Number ★ FREEFast and Slow Pointers · Spot it: one next value each + a repeat → fast and slow
O(log n) time · O(1) space02.02
Linked List Cycle 🔒 PROFast and Slow Pointers · Spot it: follow links + loop or end? → fast and slow
O(n) time · O(1) space02.03
Middle of the Linked List 🔒 PROFast and Slow Pointers · Spot it: the middle in one pass → fast and slow
O(n) time · O(1) space02.04
Circular Array Loop 🔒 PROFast and Slow Pointers · Spot it: one next index each + find a loop → fast and slow
O(n) time · O(1) space02.05
Find the Duplicate Number 🔒 PROFast and Slow Pointers · Spot it: values as next index + a repeat → cycle start
O(n) time · O(1) space02.06
Palindrome Linked List 🔒 PROFast and Slow Pointers · Spot it: list read backward in O(1) space → middle + reverse
O(n) time · O(1) space02.07
Maximum Twin Sum of a Linked List 🔒 PROFast and Slow Pointers · Spot it: pair front with back → middle + reverse
O(n) time · O(1) space02.08
Split a Circular Linked List 🔒 PROFast and Slow Pointers · Spot it: halve a ring in one lap → fast and slow
O(n) time · O(1) space02.09
Linked List Cycle III 🔒 PROFast and Slow Pointers · Spot it: size of a loop → meet, then walk one lap
O(n) time · O(1) space02.10
Linked List Cycle IV 🔒 PROFast and Slow Pointers · Spot it: where a loop starts → meet, then restart at head
O(n) time · O(1) space03.01
Repeated DNA Sequences ★ FREESliding Window · Spot it: fixed-length pieces + seen before → window + set
O(n) time · O(n) space03.02
Sliding Window Maximum 🔒 PROSliding Window · Spot it: max of every window → decreasing queue
O(n) time · O(k) space03.03
Minimum Window Subsequence 🔒 PROSliding Window · Spot it: in order, gaps allowed → forward, then backward
O(n · m) time · O(1) space03.04
Longest Repeating Character Replacement 🔒 PROSliding Window · Spot it: up to k exceptions → grow and shrink
O(n) time · O(1) space03.05
Minimum Window Substring 🔒 PROSliding Window · Spot it: shortest window covering a set → missing counter
O(n + m) time · O(alphabet) space03.06
Longest Substring without Repeating Characters 🔒 PROSliding Window · Spot it: all distinct in a stretch → window + last-seen map
O(n) time · O(alphabet) space03.07
Minimum Size Subarray Sum 🔒 PROSliding Window · Spot it: positive values + shortest run ≥ target
O(n) time · O(1) space03.08
Maximum Average Subarray I 🔒 PROSliding Window · Spot it: fixed length k → running sum, slide by one
O(n) time · O(1) space03.09
Diet Plan Performance 🔒 PROSliding Window · Spot it: score every block of k → running sum
O(n) time · O(1) space03.10
Fruit Into Baskets 🔒 PROSliding Window · Spot it: at most k distinct kinds → window + counts
O(n) time · O(1) space03.11
Contains Duplicate II 🔒 PROSliding Window · Spot it: equal items within k places → window set
O(n) time · O(k) space03.12
Frequency of the Most Frequent Element 🔒 PROSliding Window · Spot it: raise to match on a budget → sort + window
O(n log n) time · sort dominates03.13
Subarrays with K Different Integers 🔒 PROSliding Window · Spot it: exactly k → atMost(k) − atMost(k − 1)
O(n) time · O(n) space03.14
Count Subarrays With Score Less Than K 🔒 PROSliding Window · Spot it: positive values + score under a limit → count lengths
O(n) time · O(1) space03.15
Count Substrings With K-Frequency Characters II 🔒 PROSliding Window · Spot it: count "at least" → window the opposite
O(n) time · O(1) space03.16
Substring with Concatenation of All Words 🔒 PROSliding Window · Spot it: equal-length words in any order → chunk window
O(n · word length) time · O(words) space03.17
Binary Subarrays With Sum 🔒 PROSliding Window · Spot it: exact sum with zeros → atMost(g) − atMost(g − 1)
O(n) time · O(1) space03.18
Permutation in String 🔒 PROSliding Window · Spot it: anagram inside a text → fixed window of counts
O(n) time · O(1) space03.19
Number of Substrings Containing All Three Characters 🔒 PROSliding Window · Spot it: count substrings holding a full set → add left
O(n) time · O(1) space03.20
Longest Subarray of 1's After Deleting One Element 🔒 PROSliding Window · Spot it: skip one bad item → window with ≤ 1 zero
O(n) time · O(1) space03.21
Find the Index of the First Occurrence in a String 🔒 PROSliding Window · Spot it: find an exact piece → fixed window, compare
O(n · m) time · O(1) space03.22
Max Consecutive Ones III 🔒 PROSliding Window · Spot it: fix up to k bad items → window with ≤ k zeros
O(n) time · O(1) space03.23
Longest Subarray With Diff At Most Limit 🔒 PROSliding Window · Spot it: max − min within a limit → two queues
O(n) time · O(n) space03.24
Subarray Product Less Than K 🔒 PROSliding Window · Spot it: positive values + product under a cap → count lengths
O(n) time · O(1) space04.01
Merge Intervals ★ FREEIntervals · Spot it: combine overlapping ranges → sort by start, sweep
O(n log n) time · O(n) space04.02
Insert Interval 🔒 PROIntervals · Spot it: add to a sorted list → copy, absorb, copy
O(n) time · O(n) space04.03
Interval List Intersections 🔒 PROIntervals · Spot it: two sorted range lists → i and j, drop earlier end
O(n + m) time · O(1) extra space04.04
Employee Free Time 🔒 PROIntervals · Spot it: common free time → pool, sort, find gaps
O(n log n) time · O(n) space04.05
Count Days Without Meetings 🔒 PROIntervals · Spot it: uncovered days → sort, count the gaps
O(n log n) time · O(1) space04.06
Remove Covered Intervals 🔒 PROIntervals · Spot it: ranges inside ranges → sort, track furthest end
O(n log n) time · O(1) space04.07
Car Pooling 🔒 PROIntervals · Spot it: how many at once → events, sort, running count
O(n log n) time · O(n) space04.08
Data Stream as Disjoint Intervals 🔒 PROIntervals · Spot it: stream into ranges → sorted list, check neighbors
O(n) per add · list insert shifts04.09
Minimum Interval to Include Each Query 🔒 PROIntervals · Spot it: point queries → sort them, heap of ranges
O((n + q) log n) time · O(n) space04.10
Meeting Rooms II 🔒 PROIntervals · Spot it: most at once → sort starts, heap of end times
O(n log n) time · O(n) space05.01
Reverse Linked List ★ FREEIn-Place Manipulation of a Linked List · Spot it: flip the order in place → save, rewire, advance
O(n) time · O(1) space05.02
Reverse Nodes in k-Group 🔒 PROIn-Place Manipulation of a Linked List · Spot it: reverse in chunks → count ahead, then reverse
O(n) time · O(1) space05.03
Reverse Linked List II 🔒 PROIn-Place Manipulation of a Linked List · Spot it: reverse a slice → move nodes to its front
one pass · O(1) space05.04
Reorder List 🔒 PROIn-Place Manipulation of a Linked List · Spot it: front meets back → split, reverse, weave
O(n) time · O(1) space05.05
Swapping Nodes in a Linked List 🔒 PROIn-Place Manipulation of a Linked List · Spot it: k-th from the end → two pointers, a fixed gap
O(n) time · O(1) space05.06
Reverse Nodes in Even Length Groups 🔒 PROIn-Place Manipulation of a Linked List · Spot it: changing groups → count real size, then reverse
O(n) time · O(1) space05.07
Remove Duplicates from Sorted List 🔒 PROIn-Place Manipulation of a Linked List · Spot it: sorted + repeats → compare each node with the next
O(n) time · O(1) space05.08
Remove Linked List Elements 🔒 PROIn-Place Manipulation of a Linked List · Spot it: deletes that may hit the head → dummy node
O(n) time · O(1) space05.09
Split Linked List in Parts 🔒 PROIn-Place Manipulation of a Linked List · Spot it: equal pieces → count, divide, then cut
O(n + k) time · no new nodes05.10
Delete N Nodes After M Nodes of a Linked List 🔒 PROIn-Place Manipulation of a Linked List · Spot it: repeating keep and delete → one link per run
O(n) time · O(1) space05.11
Insert into a Sorted Circular Linked List 🔒 PROIn-Place Manipulation of a Linked List · Spot it: sorted ring → find the pair, mind the seam
O(n) time at most one lap · O(1) space05.12
Odd Even Linked List 🔒 PROIn-Place Manipulation of a Linked List · Spot it: group by position → two chains, then join
O(n) time · O(1) space05.13
Rotate List 🔒 PROIn-Place Manipulation of a Linked List · Spot it: shift a list → close a ring, cut once
O(n) time · O(1) space05.14
Swap Nodes in Pairs 🔒 PROIn-Place Manipulation of a Linked List · Spot it: swap neighbors → dummy + three link changes
O(n) time · O(1) space06.01
IPO ★ FREEHeaps · Spot it: options unlock over time, take the best → 2 heaps
O(n log n) time · O(n) space06.02
Find Median from Data Stream 🔒 PROHeaps · Spot it: running median → max-heap + min-heap
add O(log n) · median O(1) · O(n) space06.03
Sliding Window Median 🔒 PROHeaps · Spot it: median of a sliding window → 2 heaps + lazy deletion
O(n log n) time · O(n) space06.04
Schedule Tasks on Minimum Machines 🔒 PROHeaps · Spot it: overlapping intervals → min-heap of end times
O(n log n) time · O(n) space06.05
Meeting Rooms III 🔒 PROHeaps · Spot it: assign by lowest id + earliest free → two min-heaps
O(m log m + m log n) time · O(n) space06.06
Largest Number After Digit Swaps by Parity 🔒 PROHeaps · Spot it: reorder within groups → one heap per group
O(d log d) time · O(d) space06.07
Find Right Interval 🔒 PROHeaps · Spot it: next match after each end → sorted heaps
O(n log n) time · O(n) space06.08
Minimum Cost to Connect Sticks 🔒 PROHeaps · Spot it: repeated cheapest merge → min-heap
O(n log n) time · O(n) space06.09
Longest Happy String 🔒 PROHeaps · Spot it: no long runs → place the most plentiful first
O(a + b + c) time · O(1) space06.10
Maximum Average Pass Ratio 🔒 PROHeaps · Spot it: shrinking returns → max-heap by next gain
O((n + k) log n) time · O(n) space06.11
The Number of the Smallest Unoccupied Chair 🔒 PROHeaps · Spot it: lowest free id over time → two min-heaps
O(n log n) time · O(n) space06.12
Construct Target Array With Multiple Sums 🔒 PROHeaps · Spot it: undo from the largest → max-heap, work backwards
O(log n) per step · O(n) space07.01
Merge Sorted Array ★ FREEK-way Merge · Spot it: merge in place, room at the end → fill from back
O(m + n) time · O(1) space07.02
Kth Smallest Number in M Sorted Lists 🔒 PROK-way Merge · Spot it: k-th value across sorted lists → heap of fronts
O(k log m) time · O(m) space07.03
Find K Pairs with Smallest Sums 🔒 PROK-way Merge · Spot it: k best pairs from sorted arrays → rows + heap
O(k log k) time · O(k) space07.04
Merge K Sorted Lists 🔒 PROK-way Merge · Spot it: k sorted lists → one → heap of heads
O(N log k) N nodes in total · O(k) space07.05
Kth Smallest Element in a Sorted Matrix 🔒 PROK-way Merge · Spot it: sorted rows + k-th smallest → heap of row fronts
O(k log n) time · O(n) space07.06
Kth Smallest Prime Fraction 🔒 PROK-way Merge · Spot it: one sorted list per choice + k-th → heap
O(k log n) time · O(n) space07.07
Super Ugly Number 🔒 PROK-way Merge · Spot it: new = old × a factor → one list per factor
O(n·k·log k) worst case · O(n + k) space08.01
Kth Largest Element in a Stream ★ FREETop K Elements · Spot it: a stream + the kth best so far
O(log k) per number · O(k) space08.02
Reorganize String 🔒 PROTop K Elements · Spot it: spread out repeats → most frequent first
O(n log 26) time · O(n) space08.03
K Closest Points to Origin 🔒 PROTop K Elements · Spot it: k nearest / smallest → max heap of size k
O(n log k) time · O(k) space08.04
Top K Frequent Elements 🔒 PROTop K Elements · Spot it: most frequent k → count, then heap of size k
O(n log k) time · O(n) space08.05
Kth Largest Element in an Array 🔒 PROTop K Elements · Spot it: kth largest → min heap of size k, read the top
O(n log k) time · O(k) space08.06
Third Maximum Number 🔒 PROTop K Elements · Spot it: fixed small k + repeats count once → heap + set
O(n) time · O(1) space08.07
Find Subsequence of Length K with the Largest Sum 🔒 PROTop K Elements · Spot it: k best + original order → heap, then sort by index
O(n log k) time · O(k) space08.08
Minimum Cost to Hire K Workers 🔒 PROTop K Elements · Spot it: one member sets the rate → sort, heap the rest
O(n log n) time · O(n) space08.09
Maximal Score After Applying K Operations 🔒 PROTop K Elements · Spot it: take the current best, again and again → heap
O(n + k log n) time · O(n) space08.10
Find the Kth Largest Integer in the Array 🔒 PROTop K Elements · Spot it: top k + custom order → heap with a custom key
O(n log k) comparisons · O(k) space08.11
Maximum Performance of a Team 🔒 PROTop K Elements · Spot it: sum × minimum → sort by the minimum, heap the sum
O(n log n) time · O(n) space08.12
Smallest Range Covering Elements from K Lists 🔒 PROTop K Elements · Spot it: one from each sorted list → min heap of k heads
O(n log k) time · O(k) space08.13
K Maximum Sum Combinations From Two Arrays 🔒 PROTop K Elements · Spot it: next best = a step from a taken one → heap
O(n log n + k log k) time08.14
K Empty Slots 🔒 PROTop K Elements · Spot it: sliding window + its minimum → min heap
O(n log n) time · O(n) space08.15
Maximum Product After K Increments 🔒 PROTop K Elements · Spot it: k upgrades, weakest gains most → min heap
O(n + k log n) time · O(n) space08.16
Find the K-Sum of an Array 🔒 PROTop K Elements · Spot it: first k of a huge ranked set → heap of next steps
O(n log n + k log k) time08.17
Least Number of Unique Integers after K Removals 🔒 PROTop K Elements · Spot it: spend a budget on the cheapest first → min heap
O(n log n) time · O(n) space08.18
Final Array State After K Multiplication Operations I 🔒 PROTop K Elements · Spot it: repeatedly change the minimum → heap of (value, index)
O(n + k log n) time · O(n) space09.02
Search in Rotated Sorted Array 🔒 PROModified Binary Search
O(log n) time · O(1) space09.03
First Bad Version 🔒 PROModified Binary Search
O(log n) checker calls · O(1) space09.04
Random Pick with Weight 🔒 PROModified Binary Search
O(n) setup · O(log n) per pick09.05
Find K Closest Elements 🔒 PROModified Binary Search
O(log(n − k) + k) time · O(1) space09.06
Single Element in a Sorted Array 🔒 PROModified Binary Search
O(log n) time · O(1) space09.07
Maximum Value at a Given Index in a Bounded Array 🔒 PROModified Binary Search
O(log maxSum) time · O(1) space09.08
The K Weakest Rows in a Matrix 🔒 PROModified Binary Search
O(m log n + m log m) time · O(m) space09.09
Split Array Largest Sum 🔒 PROModified Binary Search
O(n log(sum)) time · O(1) space09.10
Find Minimum in Rotated Sorted Array II 🔒 PROModified Binary Search
O(log n) usual · O(n) worst · O(1) space09.11
Maximum Running Time of N Computers 🔒 PROModified Binary Search
O(n log(sum)) time · O(1) space09.12
Minimize Max Distance to Gas Station 🔒 PROModified Binary Search
O(n log(range / tolerance)) time09.14
Split Array Into Two Arrays to Minimize Sum Difference 🔒 PROModified Binary Search
O(2ⁿ · n) time · O(2ⁿ) space09.15
Number of Flowers in Full Bloom 🔒 PROModified Binary Search
O((n + m) log n) time · O(n) space09.16
Koko Eating Bananas 🔒 PROModified Binary Search
O(n log(max pile)) time · O(1) space09.19
Find First and Last Position of Element in Sorted Array 🔒 PROModified Binary Search
O(log n) time · O(1) space09.20
Search in Rotated Sorted Array II 🔒 PROModified Binary Search
O(log n) usual · O(n) worst · O(1) space09.21
Kth Smallest Product of Two Sorted Arrays 🔒 PROModified Binary Search
O(n log m log(range)) time09.23
Reaching Points 🔒 PROModified Binary Search
O(log(max(tx, ty))) time · O(1) space09.24
Kth Missing Positive Number 🔒 PROModified Binary Search
O(log n) time · O(1) space10.01
Subsets ★ FREESubsets · Spot it: every selection → include or exclude, item by item
O(n · 2ⁿ) time and space10.02
Permutations 🔒 PROSubsets · Spot it: every order → insert into every gap
O(n · n!) time and space10.03
Letter Combinations of a Phone Number 🔒 PROSubsets · Spot it: options per position → extend every partial
O(n · 4ⁿ) time and space10.04
Generate Parentheses 🔒 PROSubsets · Spot it: build under rules → cut a branch early
time: valid strings × 2n10.05
Letter Case Permutation 🔒 PROSubsets · Spot it: two forms per letter → double the list per letter
O(n · 2ᵏ) time and space10.06
Letter Tile Possibilities 🔒 PROSubsets · Spot it: repeated items → choose by value, not by copy
O(S * U) — bounded by O(n!) worst case, where S is the number of distinct non-empty sequences and U <= 26 is the alphabet size scanned per node.10.07
Subsets II 🔒 PROSubsets · Spot it: repeats in input → sort, extend only new ones
O(n · 2ⁿ) worst case10.08
Find K-Sum Subsets 🔒 PROSubsets · Spot it: exact total → include or exclude, prune over k
O(n · 2ⁿ) worst case11.01
Jump Game ★ FREEGreedy Techniques · Spot it: farthest point so far → one greedy scan
O(n) time · O(1) space11.02
Boats to Save People 🔒 PROGreedy Techniques · Spot it: pairs under a limit → sort + two ends
O(n log n) time · O(1) space11.03
Gas Station 🔒 PROGreedy Techniques · Spot it: running total < 0 → restart after it
O(n) time · O(1) space11.04
Two City Scheduling 🔒 PROGreedy Techniques · Spot it: two options, fixed split → sort by difference
O(n log n) time · O(1) space11.05
Minimum Number of Refueling Stops 🔒 PROGreedy Techniques · Spot it: decide later → keep the options in a heap
O(n log n) time · O(n) space11.06
Largest Palindromic Number 🔒 PROGreedy Techniques · Spot it: biggest value → best digit in each position
O(n) time · O(1) space11.07
Assign Cookies 🔒 PROGreedy Techniques · Spot it: match two lists → sort both, smallest fit
O(n log n) time · O(1) space11.08
Rearranging Fruits 🔒 PROGreedy Techniques · Spot it: count the surplus → pay for the cheaper half
O(n log n) time · O(n) space11.09
Number of Steps to Reduce a Binary Number to One 🔒 PROGreedy Techniques · Spot it: bit rules → scan right to left with a carry
O(n) time · O(1) space11.10
Maximum Swap 🔒 PROGreedy Techniques · Spot it: one change, biggest number → fix the leftmost
O(d) time · O(d) space, d digits11.11
Can Place Flowers 🔒 PROGreedy Techniques · Spot it: spacing rule, fit the most → take the first spot
O(n) time · O(1) space11.12
Largest Odd Number in String 🔒 PROGreedy Techniques · Spot it: last digit decides → scan from the right
O(n) time · O(1) space11.13
Candy 🔒 PROGreedy Techniques · Spot it: rules on both sides → one pass each way
O(n) time · O(n) space11.14
Minimum Replacements to Sort the Array 🔒 PROGreedy Techniques · Spot it: right end limits the left → scan backward
O(n) time · O(1) space11.15
Sort an Array 🔒 PROGreedy Techniques · Spot it: repeatedly take the largest → a heap
O(n log n) time · O(1) space11.16
Text Justification 🔒 PROGreedy Techniques · Spot it: fill in order → pack each line, then the next
O(total characters) time11.17
Largest Number 🔒 PROGreedy Techniques · Spot it: order by a pairwise rule → custom sort
O(n log n) compares · O(n) space11.18
Best Time to Buy and Sell Stock II 🔒 PROGreedy Techniques · Spot it: gains add up step by step → take each one
O(n) time · O(1) space11.19
Wildcard Matching 🔒 PROGreedy Techniques · Spot it: stars → start empty, grow the latest one
O(n × m) worst case · O(1) space11.20
Remove K Digits 🔒 PROGreedy Techniques · Spot it: smallest sequence → stack, pop bigger tops
O(n) time · O(n) space11.21
Strong Password Checker 🔒 PROGreedy Techniques · Spot it: one edit, two faults → count and take the max
O(n) time · O(n) space for the runs11.22
Jump Game II 🔒 PROGreedy Techniques · Spot it: fewest steps over ranges → grow the range
O(n) time · O(1) space11.23
Best Time to Buy and Sell Stock 🔒 PROGreedy Techniques · Spot it: best earlier value → carry a running min
O(n) time · O(1) space11.24
Maximum Points After Enemy Battles 🔒 PROGreedy Techniques · Spot it: repeat the cheapest action, fuel it with the rest
O(n) time · O(1) space11.25
Integer to Roman 🔒 PROGreedy Techniques · Spot it: make change from fixed values → largest first
O(1) time · O(1) space12.01
N-Queens II ★ FREEBacktracking · Spot it: place items with no clashes → row by row
O(n!) time · O(n) space12.02
Word Search 🔒 PROBacktracking · Spot it: trace through a grid, no reuse → mark, unmark
O(cells · 3ᴸ) time · O(L) space12.03
House Robber III 🔒 PROBacktracking · Spot it: take or skip per node → return two totals
O(n) time · O(h) space12.04
Restore IP Addresses 🔒 PROBacktracking · Spot it: cut into rule-checked pieces → try each length
at most 3⁴ = 81 branches · O(1)12.05
Flood Fill 🔒 PROBacktracking · Spot it: spread to touching cells → flood fill
O(rows × cols) time and space12.06
Minimum Moves to Spread Stones Over Grid 🔒 PROBacktracking · Spot it: few items to few slots, least cost → try all
O(1) — the grid size is fixed at 3 x 3 with m <= 8 empty cells, bounding the search tree by at most 8! states.12.07
Binary Tree Paths 🔒 PROBacktracking · Spot it: list root-to-leaf routes → one shared path
O(n · h) time · O(h) space12.08
Binary Watch 🔒 PROBacktracking · Spot it: choose k from a set, with limits → combinations
at most C(10, k) choices12.09
Optimal Account Balancing 🔒 PROBacktracking · Spot it: pair off plus and minus amounts → try each
O(n!) worst case · O(n) space12.10
Split a String Into the Max Number of Unique Substrings 🔒 PROBacktracking · Spot it: cut a string, pieces must differ → used set
O(n · 2ⁿ) worst case · O(n) space12.11
All Paths From Source to Target 🔒 PROBacktracking · Spot it: list all routes, no cycles → path, no visited set
O(n · 2ⁿ) time · O(n) space12.12
Remove Invalid Parentheses 🔒 PROBacktracking · Spot it: fewest edits, all fixes → count, then delete
O(n · 2ⁿ) worst case12.13
N-Queens 🔒 PROBacktracking · Spot it: repeated conflict checks → sets of blocked lines
O(n!) time · n² per copy12.14
Unique Paths III 🔒 PROBacktracking · Spot it: visit every cell once → walk, mark, unmark
O(3ⁿ) time · O(n) space12.15
Combinations 🔒 PROBacktracking · Spot it: every group of k, order free → increasing picks
O(k · C(n, k)) time · O(k) space12.16
Word Ladder II 🔒 PROBacktracking · Spot it: all shortest routes → BFS levels, then backtrack
O(N^2 * L + P * L) — BFS labels at most N = len(words) words using O(N * L) pairwise comparisons per word (reducible to O(N * 26 * L^2) via wildcard/set lookups), and DFS traverses only shortest-path DAG nodes totaling P * L output work.12.17
Flip Game 🔒 PROBacktracking · Spot it: all states one move away → do, record, undo
O(n²) time · O(n²) output12.18
Additive Number 🔒 PROBacktracking · Spot it: early choices force the rest → try only those
O(n³) time · O(n) space12.19
Sudoku Solver 🔒 PROBacktracking · Spot it: fill blanks under rules → check, write, erase
O(9^m) — on a standard 9 x 9 board with m <= 81 blanks, each blank branches across at most 9 digits (heavily pruned by row, column, and box constraints).12.20
Matchsticks to Square 🔒 PROBacktracking · Spot it: pack items into equal bins → biggest first
O(4ⁿ) worst case · O(n) space13.01
0/1 Knapsack ★ FREEDynamic Programming · Spot it: use each item once, under a limit → knapsack
O(n · W) time and space13.02
Coin Change 🔒 PRODynamic Programming · Spot it: reusable pieces, fewest for an exact total
O(amount × coins) time13.03
N-th Tribonacci Number 🔒 PRODynamic Programming · Spot it: each term from the last few → rolling table
O(n) time · O(1) space13.04
Partition Equal Subset Sum 🔒 PRODynamic Programming · Spot it: pick some numbers to hit an exact sum
O(n · sum) time13.05
Counting Bits 🔒 PRODynamic Programming · Spot it: answer for i from a smaller number → table
O(n) time13.06
01 Matrix 🔒 PRODynamic Programming · Spot it: distance to the nearest special cell
O(rows × cols) two passes, no queue13.07
House Robber II 🔒 PRODynamic Programming · Spot it: no two neighbors, in a circle → two straight runs
O(n) time · O(1) space13.08
Maximum Product Subarray 🔒 PRODynamic Programming · Spot it: signs can flip → track best and worst
O(n) time · O(1) space13.09
Combination Sum 🔒 PRODynamic Programming · Spot it: reusable parts, list every way to a total
O(n * target * C) — iterates over n candidates and up to target sums, copying C stored partial combinations of length at most target / min(candidates).13.10
Word Break 🔒 PRODynamic Programming · Spot it: split a string into dictionary pieces
O(n²) substring checks13.11
Palindromic Substrings 🔒 PRODynamic Programming · Spot it: answer for a range from the range inside it
O(n²) time13.12
Longest Common Subsequence 🔒 PRODynamic Programming · Spot it: compare two sequences' ends
O(m · n) time13.13
Word Break II 🔒 PRODynamic Programming · Spot it: list every split → store results per position
O(n^3 + 2^n * n) — evaluates O(n^2) substring slices of length up to n plus copies/joins up to O(2^n) valid token lists of length O(n).13.14
Decode Ways 🔒 PRODynamic Programming · Spot it: count splits into short pieces → look back 1 or 2
O(n) time · O(1) space13.15
Count the Number of Good Subsequences 🔒 PRODynamic Programming · Spot it: fix one value, then multiply independent choices
O(n + 26 · highest letter count)13.16
Binary Tree Cameras 🔒 PRODynamic Programming · Spot it: node state from children's states → tree DP
O(n) time · O(height) stack13.17
Number of Ways to Form Target String Given a Dictionary 🔒 PRODynamic Programming · Spot it: pick left to right from columns, count ways
O(columns × target)13.18
Pascal's Triangle 🔒 PRODynamic Programming · Spot it: each row from the row above → build row by row
O(n²) one addition per cell13.19
Triangle 🔒 PRODynamic Programming · Spot it: cheapest path down, two moves → bottom up
O(n²) time · O(1) extra13.20
Frog Jump 🔒 PRODynamic Programming · Spot it: state = position + last move
O(n²) time and space13.21
Cherry Pickup 🔒 PRODynamic Programming · Spot it: two trips on one grid → move both at once
O(n³) time · O(n²) space13.22
Dungeon Game 🔒 PRODynamic Programming · Spot it: the end sets the need → fill backward
O(m × n) time and space13.23
Burst Balloons 🔒 PRODynamic Programming · Spot it: removals change neighbors → choose the last one
O(n³) time · O(n²) space13.24
Regular Expression Matching 🔒 PRODynamic Programming · Spot it: two strings compared → table of prefixes
O(m × n) time and space13.25
Shortest Common Supersequence 🔒 PRODynamic Programming · Spot it: merge two sequences → longest common subsequence
O(m × n) time and space13.26
Interleaving String 🔒 PRODynamic Programming · Spot it: two sequences in order → state = how far in each
O(m × n) time and space13.27
Maximal Rectangle 🔒 PRODynamic Programming · Spot it: rectangles in a grid → one histogram per row
O(rows × cols) time · O(cols) space13.28
Longest Increasing Path in a Matrix 🔒 PRODynamic Programming · Spot it: overlapping searches per cell → save answers
O(rows × cols) time and space13.29
Min Cost Climbing Stairs 🔒 PRODynamic Programming · Spot it: reached from the last few positions → best per spot
O(n) time · O(1) space13.30
Number of Longest Increasing Subsequence 🔒 PRODynamic Programming · Spot it: count the best ones → keep best and count
O(n²) time · O(n) space13.31
Distinct Subsequences 🔒 PRODynamic Programming · Spot it: count matches in order → skip + use
O(m·n) time · O(n) space13.32
Cheapest Flights Within K Stops 🔒 PRODynamic Programming · Spot it: cheapest path with a step limit → row per step
O(k × flights) time · O(cities) space13.33
Minimum Path Sum 🔒 PRODynamic Programming · Spot it: grid path, right or down → cell + min(two in)
O(m × n) time · O(1) extra space13.34
Best Time to Buy and Sell Stock III 🔒 PRODynamic Programming · Spot it: ordered actions with a limit → best after each
O(n) time · O(1) space13.35
Maximal Square 🔒 PRODynamic Programming · Spot it: square grows from neighbors → side ending here
O(rows × cols) time and space13.36
The Number of Good Subsets 🔒 PRODynamic Programming · Spot it: few small items → state = set of used items
O(n + U * 2^10) — counts occurrences across n elements and updates 1024 bitmask states for at most U <= 30 distinct values.13.37
Freedom Trail 🔒 PRODynamic Programming · Spot it: greedy can trap you → best cost per position
O(key × ring²) time · O(ring) space13.38
Soup Servings 🔒 PRODynamic Programming · Spot it: random steps, repeated states → save each one
O(1) — bounded by at most 192 * 192 = 36,864 states due to the n > 4800 cutoff (O((n / 25)^2) for small n).13.39
Number of People Aware of a Secret 🔒 PRODynamic Programming · Spot it: depends on a range of past days → running sum
O(n) time · O(n) space13.40
Climbing Stairs 🔒 PRODynamic Programming · Spot it: count ways with a few move sizes → add last moves
O(n) time · O(1) space13.41
Edit Distance 🔒 PRODynamic Programming · Spot it: turn one string into another → prefix table
O(m × n) time and space14.01
Cyclic Sort ★ FREECyclic Sort · Spot it: values 1 to n → each value has a home index
O(n) time · O(1) space14.02
Missing Number 🔒 PROCyclic Sort · Spot it: a range with one gap → place home, then scan
O(n) time · O(1) space14.03
First Missing Positive 🔒 PROCyclic Sort · Spot it: smallest missing positive → homes for 1 to n
O(n) time · O(1) space14.04
Find the Corrupt Pair 🔒 PROCyclic Sort · Spot it: 1 to n with a gap and a repeat → place, then scan
O(n) time · O(1) space14.05
Sort Array By Parity II 🔒 PROCyclic Sort · Spot it: allowed slots by type → swap misfit pairs
O(n) time · O(1) space14.06
Find the First K Missing Positive Numbers 🔒 PROCyclic Sort · Spot it: k missing positives → place, scan, count on
O(n + k) time · O(k) space15.01
Compilation Order ★ FREETopological Sort · Spot it: dependencies + a valid order → topological sort
O(V + E) time · O(V + E) space15.02
Alien Dictionary 🔒 PROTopological Sort · Spot it: hidden order from sorted data → arrows + topo sort
O(total letters) time15.03
Verifying an Alien Dictionary 🔒 PROTopological Sort · Spot it: check against a known order → rank table
O(total letters) time15.04
Course Schedule II 🔒 PROTopological Sort · Spot it: prerequisites + full order → Kahn's queue
O(V + E) time and space15.05
Course Schedule 🔒 PROTopological Sort · Spot it: can all be done? → count what Kahn's sort takes
O(V + E) time and space15.06
Build a Matrix with Conditions 🔒 PROTopological Sort · Spot it: rules on two axes → one sort per axis
O(k² + conditions) time15.07
Longest Path With Different Adjacent Characters 🔒 PROTopological Sort · Spot it: children before parent → peel leaves upward
O(n) time and space15.08
Parallel Courses III 🔒 PROTopological Sort · Spot it: parallel jobs + dependencies → latest finish first
O(V + E) time and space15.09
Parallel Courses 🔒 PROTopological Sort · Spot it: count rounds → peel in-degree 0 layer by layer
O(V + E) time and space15.10
Sort Items by Groups Respecting Dependencies 🔒 PROTopological Sort · Spot it: order inside and between groups → two sorts
two sorts · O(V + E) time15.11
Find All Possible Recipes from Given Supplies 🔒 PROTopological Sort · Spot it: made things unlock more → queue from the supplies
O(V + E) time and space15.12
Collect Coins in a Tree 🔒 PROTopological Sort · Spot it: trim leaves layer by layer, then count
O(n) time and space16.01
Find the Distance Value Between Two Arrays ★ FREESort and Search · Spot it: is anything near x? → sort, then binary search
O((n + m) log m) time · O(1) space16.02
Longest Subsequence With Limited Sum 🔒 PROSort and Search · Spot it: most items under a limit → sort, sum, search
O((n + q) log n) time · O(n) space16.03
Find Target Indices After Sorting Array 🔒 PROSort and Search · Spot it: where does a block of equals start, end?
O(n log n) time · O(1) space16.04
Count Pairs in Two Arrays 🔒 PROSort and Search · Spot it: pair test splits per index → sort, two pointers
O(n log n) time · O(n) space16.05
Valid Triangle Number 🔒 PROSort and Search · Spot it: count triples with a sum test → fix one, two pointers
O(n²) time · O(1) space16.06
Minimum Operations to Make All Array Elements Equal 🔒 PROSort and Search · Spot it: total distance to q → sort, prefix sums, search
O((n + q) log n) time · O(n) space16.07
Sum of Mutated Array Closest to Target 🔒 PROSort and Search · Spot it: a cap or threshold on a sum → sort, scan
O(n log n) time · O(1) space16.08
Range Sum of Sorted Subarray Sums 🔒 PROSort and Search · Spot it: ranks among all subarray sums → list, sort
O(n² log n) time · O(n²) space16.09
Magnetic Force Between Two Balls 🔒 PROSort and Search · Spot it: maximize the minimum → binary search the answer
O(n log n + n log range) time · O(1) space16.10
Find K-th Smallest Pair Distance 🔒 PROSort and Search · Spot it: k-th of an implied list → guess, count, search
O(n log n + n log range) time · O(1) space16.11
Minimum Space Wasted from Packaging 🔒 PROSort and Search · Spot it: sorted items into size buckets → search the edges
O(n log n + b log b + b log n) time16.12
Russian Doll Envelopes 🔒 PROSort and Search · Spot it: grow in two measures → sort one, rising run on other
O(n log n) time · O(n) space16.13
Put Marbles in Bags 🔒 PROSort and Search · Spot it: choices add fixed amounts → sort, take the ends
O(n log n) time · O(n) space16.14
H-Index 🔒 PROSort and Search · Spot it: at least h items worth h → sort, search
O(n log n) time · O(1) space16.15
Reverse Pairs 🔒 PROSort and Search · Spot it: count earlier-later pairs → merge sort
O(n log n) time · O(n) space16.16
Two Sum Less Than K 🔒 PROSort and Search · Spot it: best pair sum under a limit → sort, two pointers
O(n log n) time · O(1) space16.17
Maximum Number of Integers to Choose from a Range I 🔒 PROSort and Search · Spot it: most picks under a limit → cheapest first
O(b log b + n) time · O(1) space16.18
Minimum Time Difference 🔒 PROSort and Search · Spot it: closest pair → sort, compare neighbors
O(n log n) time · O(n) space17.01
Set Matrix Zeroes ★ FREEMatrices · Spot it: marks per row or column, no extra memory
O(m × n) time · O(1) extra space17.02
Rotate Image 🔒 PROMatrices · Spot it: turn a square grid in place → two flips
O(n²) time · O(1) extra space17.03
Spiral Matrix 🔒 PROMatrices · Spot it: ring by ring → four shrinking boundaries
O(m × n) time · O(1) extra space17.04
Where Will the Ball Fall 🔒 PROMatrices · Spot it: local rule per step → simulate it
O(m × n) time · O(1) extra space17.05
Transpose Matrix 🔒 PROMatrices · Spot it: rows become columns → transpose
O(m × n) time and space17.06
Count Negative Numbers in a Sorted Matrix 🔒 PROMatrices · Spot it: rows and columns sorted → staircase walk
O(m + n) time · O(1) space17.07
Minimum Time Takes to Reach Destination Without Drowning 🔒 PROMatrices · Spot it: a spreading hazard → BFS it, then race it
O(m × n) time and space17.08
Smallest Rectangle Enclosing Black Pixels 🔒 PROMatrices · Spot it: one connected shape → DFS from inside it
O(m × n) time and space17.09
Island Perimeter 🔒 PROMatrices · Spot it: count shared edges → look only up and left
O(m × n) time · O(1) space17.10
Convert 1D Array Into 2D Array 🔒 PROMatrices · Spot it: flat index ↔ cell → divide and remainder
O(m × n) time · no extra space17.11
Spiral Matrix II 🔒 PROMatrices · Spot it: fill in rings → four shrinking boundaries
O(n²) time · no extra space17.12
Flip Columns For Maximum Number of Equal Rows 🔒 PROMatrices · Spot it: same change to every row → group by a key
O(m × n) time and space17.13
Number of Spaces Cleaning Robot Cleaned 🔒 PROMatrices · Spot it: endless process, small state → stop on repeat
O(m × n) time and space17.14
Minimize Maximum Value in a Grid 🔒 PROMatrices · Spot it: keep the order, shrink values → sort, assign
O(mn log mn) time · O(mn) space17.15
Kth Smallest Number in Multiplication Table 🔒 PROMatrices · Spot it: easy to count below x → binary search on x
O(m · log(m × n)) time · O(1) space17.16
Swim in Rising Water 🔒 PROMatrices · Spot it: path cost = its worst cell → min heap
O(n² log n) time · O(n²) space17.17
Best Meeting Point 🔒 PROMatrices · Spot it: total walking distance → median per axis
O(m × n) time · O(homes) space17.18
Game of Life 🔒 PROMatrices · Spot it: update all at once in place → encode both
O(m × n) time · O(1) extra space17.19
Toeplitz Matrix 🔒 PROMatrices · Spot it: diagonal rule → compare with the upper left cell
O(m × n) time · O(1) space17.20
Diagonal Traverse 🔒 PROMatrices · Spot it: zigzag diagonals → step and bounce at edges
O(m × n) time · O(1) extra space18.01
Basic Calculator ★ FREEStacks · Spot it: brackets pause an outer sum → stack
O(n) time · O(n) space18.02
Remove All Adjacent Duplicates In String 🔒 PROStacks · Spot it: deletions make neighbors meet → stack
O(n) time · O(n) space18.03
Minimum Remove to Make Valid Parentheses 🔒 PROStacks · Spot it: find the unpaired brackets → stack of indexes
O(n) time · O(n) space18.04
Exclusive Time of Functions 🔒 PROStacks · Spot it: nested start and end events → stack
O(n) time · O(n) space18.05
Flatten Nested List Iterator 🔒 PROStacks · Spot it: nested data, handed out lazily → stack
O(n) time in total · O(n) space18.06
Implement Queue Using Stacks 🔒 PROStacks · Spot it: reverse an order lazily → two stacks
O(1) amortized each value moves once · O(n) space18.07
Daily Temperatures 🔒 PROStacks · Spot it: wait for the next larger → monotonic stack
O(n) time · O(n) space18.08
Decode String 🔒 PROStacks · Spot it: nested brackets, inside first → stack
O(output length) time and space18.09
Minimum String Length After Removing Substrings 🔒 PROStacks · Spot it: deletions create pairs at the join → stack
O(n) time · O(n) space18.10
Number of Valid Subarrays 🔒 PROStacks · Spot it: runs end at the first smaller value → stack
O(n) time · O(n) space18.11
Number of Visible People in a Queue 🔒 PROStacks · Spot it: taller items block the view → stack
O(n) time · O(n) space18.12
Parsing a Boolean Expression 🔒 PROStacks · Spot it: nested operator(args) → stack
O(n) time · O(n) space18.13
Remove Duplicate Letters 🔒 PROStacks · Spot it: smallest order, with must-keep items → stack
O(n) time · stack of at most 26 letters18.14
Longest Valid Parentheses 🔒 PROStacks · Spot it: longest matched run → stack with a boundary
O(n) time · O(n) space18.15
Next Greater Element IV 🔒 PROStacks · Spot it: k-th next larger → one stack per stage
O(n) time · O(n) space18.16
Valid Parentheses 🔒 PROStacks · Spot it: pairs that must nest → stack
O(n) time · O(n) space18.17
Maximum Width Ramp 🔒 PROStacks · Spot it: widest pair, later value not smaller → stack
O(n) time · O(n) space18.18
Remove All Adjacent Duplicates in String II 🔒 PROStacks · Spot it: runs vanish, sides join → stack of counts
O(n) time · O(n) space18.19
Basic Calculator II 🔒 PROStacks · Spot it: operator precedence, no brackets → stack
O(n) time · O(n) space18.20
Simplify Path 🔒 PROStacks · Spot it: undo cancels the latest step → stack
O(n) time · O(n) space19.01
Network Delay Time ★ FREEGraphs · Spot it: costs on links + cheapest arrival → Dijkstra
O(E log V) time · O(V + E) space19.02
Paths in Maze That Lead to Same Room 🔒 PROGraphs · Spot it: count triangles → pair neighbors, check one link
O(V · d²) time19.03
Clone Graph 🔒 PROGraphs · Spot it: copy with loops → map original to copy
O(V + E) time · O(V) space19.04
Graph Valid Tree 🔒 PROGraphs · Spot it: one piece, no loops → count edges + one search
O(V + E) time and space19.05
Bus Routes 🔒 PROGraphs · Spot it: count switches → make each group a node
O(S) S = stops over all routes19.06
Reconstruct Itinerary 🔒 PROGraphs · Spot it: every edge once → Hierholzer, reverse when stuck
O(E log E) time · O(E) space19.07
Find the Town Judge 🔒 PROGraphs · Spot it: only counts matter → in degree, out degree
O(n + E) time · O(n) space19.08
Find Center of Star Graph 🔒 PROGraphs · Spot it: the shape is guaranteed → check just enough
O(1) time · O(1) space19.10
Path with Maximum Probability 🔒 PROGraphs · Spot it: multiply along a path → Dijkstra, largest first
O(E log V) time · O(V + E) space19.11
Tree Diameter 🔒 PROGraphs · Spot it: longest path in a tree → search twice
O(n) time · O(n) space19.12
Reorder Routes to Make All Paths Lead to the City Zero 🔒 PROGraphs · Spot it: directed tree → walk out from 0, count wrong ways
O(n) time · O(n) space19.13
Minimum Cost to Make at Least One Valid Path in a Grid 🔒 PROGraphs
O(rows × cols) time19.14
Longest Cycle in a Graph 🔒 PROGraphs · Spot it: one arrow out per node → stamp and follow
O(n) time · O(n) space19.15
Shortest Cycle in a Graph 🔒 PROGraphs · Spot it: smallest loop → BFS from every node
O(V · (V + E)) time19.16
Shortest Path Visiting All Nodes 🔒 PROGraphs · Spot it: visit all, small n → BFS over (node, bitmask)
O(n² · 2ⁿ) time19.18
Shortest Path in a Grid with Obstacles Elimination 🔒 PROGraphs
O(rows·cols·k) time20.01
Flatten Binary Tree to Linked List ★ FREETree Depth-First Search · Spot it: link each node to its successor → walk backwards
O(n) time · O(h) stack20.02
Diameter of Binary Tree 🔒 PROTree Depth-First Search · Spot it: best path bends anywhere → combine child heights
O(n) time · O(h) stack20.03
Serialize and Deserialize Binary Tree 🔒 PROTree Depth-First Search · Spot it: tree ↔ text → preorder with null markers
O(n) time and space, each way20.04
Invert Binary Tree 🔒 PROTree Depth-First Search · Spot it: same local change at every node → recurse
O(n) time · O(h) stack20.05
Binary Tree Maximum Path Sum 🔒 PROTree Depth-First Search · Spot it: bending path + negatives → drop negative gains
O(n) time · O(h) stack20.06
Convert Sorted Array to Binary Search Tree 🔒 PROTree Depth-First Search · Spot it: sorted → balanced tree → middle as root
O(n) time · O(log n) stack20.07
Build Binary Tree from Preorder and Inorder Traversal 🔒 PROTree Depth-First Search · Spot it: two traversals → preorder root splits inorder
O(n) time · O(n) space20.08
Binary Tree Right Side View 🔒 PROTree Depth-First Search · Spot it: one node per depth → carry the depth down
O(n) time · O(h) stack20.09
Lowest Common Ancestor of a Binary Tree 🔒 PROTree Depth-First Search · Spot it: where do two nodes meet → report found nodes up
O(n) time · O(h) stack20.10
Validate Binary Search Tree 🔒 PROTree Depth-First Search · Spot it: limits from every ancestor → carry a range down
O(n) time · O(h) stack20.11
Nested List Weight Sum II 🔒 PROTree Depth-First Search · Spot it: needs a fact from the whole tree → two passes
O(n) time · O(depth) stack20.12
Inorder Successor in BST 🔒 PROTree Depth-First Search · Spot it: next value in a search tree → walk one path
O(h) time · O(1) space20.13
Height of Binary Tree After Subtree Removal Queries 🔒 PROTree Depth-First Search · Spot it: many removal queries → precompute in two passes
two passes · O(n + q) time20.14
Delete Nodes And Return Forest 🔒 PROTree Depth-First Search · Spot it: deletions split the tree → return node or nothing
O(n) time · O(n) space20.15
Sum of Distances in a Tree 🔒 PROTree Depth-First Search · Spot it: an answer for every node → adjust along each edge
O(n) time · O(n) space20.16
Recover a Tree From Preorder Traversal 🔒 PROTree Depth-First Search · Spot it: depths in preorder → keep the path as a stack
O(n) time · O(h) stack20.17
Binary Tree Preorder Traversal 🔒 PROTree Depth-First Search · Spot it: parent before children → preorder
O(n) time · O(h) stack20.18
Univalued Binary Tree 🔒 PROTree Depth-First Search · Spot it: a rule for every node → stop at first failure
O(n) time · O(h) stack20.19
Path Sum 🔒 PROTree Depth-First Search · Spot it: root-to-leaf total → carry the remainder down
O(n) time · O(h) stack20.20
Closest Binary Search Tree Value 🔒 PROTree Depth-First Search · Spot it: nearest value in a search tree → walk one path
O(h) time · O(1) space20.21
Kth Smallest Element in a BST 🔒 PROTree Depth-First Search · Spot it: rank in a search tree → inorder, then count
O(h + k) time · O(h) stack20.22
Maximum Depth of Binary Tree 🔒 PROTree Depth-First Search · Spot it: answer from the children's answers → postorder
O(n) time · O(h) stack21.01
Binary Tree Level Order Traversal ★ FREETree Breadth-First Search · Spot it: answer grouped by level → BFS with level size
O(n) time · O(n) space21.02
Binary Tree Zigzag Level Order Traversal 🔒 PROTree Breadth-First Search · Spot it: levels in alternating order → BFS + a flag
O(n) time · O(n) space21.03
Populating Next Right Pointers in Each Node 🔒 PROTree Breadth-First Search · Spot it: link same-level neighbors → walk the level above
O(n) time O(1) extra space21.04
Vertical Order Traversal of a Binary Tree 🔒 PROTree Breadth-First Search · Spot it: group by column → BFS with (row, col)
O(n log n) time · O(n) space21.05
Symmetric Tree 🔒 PROTree Breadth-First Search · Spot it: two parts must match → queue pairs
O(n) time · O(n) space21.06
Word Ladder 🔒 PROTree Breadth-First Search · Spot it: fewest one-change moves → BFS over states
n words of length L · O(n · L²) time21.07
Two Sum IV - Input Is a BST 🔒 PROTree Breadth-First Search · Spot it: two values sum to a target → seen set
2 + 5 = 7 · O(n) time · O(n) space21.08
Find Minimum Diameter After Merging Two Trees 🔒 PROTree Breadth-First Search · Spot it: longest path in a tree → two searches
O(n + m) time · O(n + m) space21.09
Closest Node to Path in Tree 🔒 PROTree Breadth-First Search · Spot it: nearest member of a group → BFS waves
two searches · O(n) per query21.10
Frog Position After T Seconds 🔒 PROTree Breadth-First Search · Spot it: one level per second → BFS carrying a value
0.25 · O(n) time · O(n) space21.11
Average of Levels in Binary Tree 🔒 PROTree Breadth-First Search · Spot it: one number per level → BFS level loop
O(n) time · O(n) space21.12
Open the Lock 🔒 PROTree Breadth-First Search · Spot it: states + small moves + fewest → BFS
O(10^4 * 4^2 + D) — bounded by at most `10,000` states each generating `8` length-`4` strings, plus O(D) to hash `D` deadends.21.13
Connect All Siblings of a Binary Tree 🔒 PROTree Breadth-First Search · Spot it: one chain in reading order → BFS + previous
O(n) time · O(n) space21.14
Shortest Distance from All Buildings 🔒 PROTree Breadth-First Search · Spot it: one spot near many sources → BFS from each
O(buildings × rows × cols)22.01
Implement Trie (Prefix Tree) ★ FREETrie · Spot it: store words + ask word or prefix → trie
O(L) per operation · L = word length22.02
Search Suggestions System 🔒 PROTrie · Spot it: suggest as you type → trie + ordered DFS
O(L) walk plus a DFS that stops after 3 words22.03
Replace Words 🔒 PROTrie · Spot it: word vs many possible starts → trie
O(total letters) time22.04
Design Add and Search Words Data Structure 🔒 PROTrie · Spot it: wildcard patterns over a word set → trie + DFS
O(L) no dots · each dot can branch 26 ways22.05
Word Search II 🔒 PROTrie · Spot it: many words in one grid → trie + DFS
O(T + m * n * 4 * 3^(L - 1)) worst-case — O(T) to build the trie over `T` characters, plus trie-pruned DFS of depth up to max word length `L` from each cell.22.06
Top K Frequent Words 🔒 PROTrie · Spot it: rank by count, ties A to Z → buckets of tries
O(total letters) time22.07
Longest Common Prefix 🔒 PROTrie · Spot it: shared start of many strings → trie path
O(total letters) time22.08
Index Pairs of a String 🔒 PROTrie · Spot it: all matches of many words → trie per start
O(n × longest word) time22.09
K-th Smallest in Lexicographical Order 🔒 PROTrie · Spot it: k-th in a huge order → count subtrees, skip
O(log² n) time · O(1) space22.10
Palindrome Pairs 🔒 PROTrie · Spot it: joins that read the same backwards → trie of reversed words
O(n × L²) time22.11
Longest Common Suffix Queries 🔒 PROTrie · Spot it: shared endings → trie of reversed words
O(total letters) time22.12
Map Sum Pairs 🔒 PROTrie · Spot it: totals by prefix → running sums on trie nodes
O(L) per insert and per sum22.13
Check If a Word is a Prefix of Any Word in a Sentence 🔒 PROTrie · Spot it: first word with a prefix → tag trie nodes
O(sentence + search word) time22.14
Longest Word With All Prefixes 🔒 PROTrie · Spot it: every prefix must be a word → marked-path DFS
O(total letters) time22.15
Lexicographical Numbers 🔒 PROTrie · Spot it: numbers in dictionary order → digit trie walk
O(n) time O(1) extra space23.01
Design HashMap ★ FREEHash Maps · Spot it: build lookup by key from plain lists
O(1) average per call · O(n) space23.02
Fraction to Recurring Decimal 🔒 PROHash Maps · Spot it: find where a repeating process loops back
O(denominator) time and space23.03
Logger Rate Limiter 🔒 PROHash Maps · Spot it: each key needs only its last time
O(1) per message · O(messages) space23.04
Next Greater Element I 🔒 PROHash Maps · Spot it: next bigger item to the right → stack + map
O(n + m) time · O(n) space23.05
Isomorphic Strings 🔒 PROHash Maps · Spot it: two sequences must pair up one to one
O(n) time · O(alphabet) space23.06
Find Duplicate File in System 🔒 PROHash Maps · Spot it: group items by an exact shared property
O(total characters) time and space23.07
Dot Product of Two Sparse Vectors 🔒 PROHash Maps · Spot it: mostly empty data → store only real entries
O(nonzero entries) time and space23.08
High Five 🔒 PROHash Maps · Spot it: per group, keep only the best k
O(n log n) time · O(n) space23.09
Bulls and Cows 🔒 PROHash Maps · Spot it: count matches, each item used once
O(n) time · O(1) space23.10
Custom Sort String 🔒 PROHash Maps · Spot it: follow a given ranking → count, then write
O(n) time · O(alphabet) space23.11
Number of Distinct Islands 🔒 PROHash Maps · Spot it: compare shapes anywhere → offsets as key
O(rows × cols) time and space23.12
Number of Wonderful Substrings 🔒 PROHash Maps · Spot it: odd or even counts → bit mask + map of masks
O(10 · n) time · O(1024) space23.13
Total Appeal of a String 🔒 PROHash Maps · Spot it: sum over all substrings → last index map
O(n) time · O(1) space23.14
Continuous Subarray Sum 🔒 PROHash Maps · Spot it: run sums → prefix sums + map of remainders
O(n) time · O(min(n, k)) space23.15
Unique Number of Occurrences 🔒 PROHash Maps · Spot it: compare frequencies → count map, then a set
O(n) time · O(n) space23.16
Longest Happy Prefix 🔒 PROHash Maps · Spot it: compare many substrings fast → rolling hash
O(n) time · O(n) space23.17
Find Longest Self-Contained Substring 🔒 PROHash Maps · Spot it: a piece must own all copies of its letters
O(26 · n) time · O(26) space23.18
Intersection of Two Arrays 🔒 PROHash Maps · Spot it: common items of two lists, each once
O(n + m) time · O(n) space23.19
Word Pattern 🔒 PROHash Maps · Spot it: two sequences must pair up one to one
O(n) time · O(n) space23.20
Valid Sudoku 🔒 PROHash Maps · Spot it: no repeats inside rows, columns, groups
81 cells · 3 lookups each · O(1)23.21
Roman to Integer 🔒 PROHash Maps · Spot it: fixed table + compare with the next item
O(n) time · O(1) space23.22
Contiguous Array 🔒 PROHash Maps · Spot it: longest stretch where two kinds balance
O(n) time · O(n) space23.23
Jewels and Stones 🔒 PROHash Maps · Spot it: test many items against a fixed group
O(j + s) time · O(j) space23.24
Vowel Spellchecker 🔒 PROHash Maps · Spot it: near matches → simplify to a shared key
KiTe, hare, KiTe, empty · O(letters)23.25
N-Repeated Element in Size 2N Array 🔒 PROHash Maps · Spot it: the first value seen twice
O(n) time · O(n) space23.26
Powerful Integers 🔒 PROHash Maps · Spot it: few fast-growing terms → build every sum
O(log² bound) time · O(answers) space23.27
Before and After Puzzle 🔒 PROHash Maps · Spot it: link items end to start → map the starts
O(n + joins) time · O(n) space23.28
Intersection of Two Arrays II 🔒 PROHash Maps · Spot it: shared items, counted copy for copy
O(n + m) time · O(n) space23.29
Subarray Sum Equals K 🔒 PROHash Maps · Spot it: count stretches with an exact sum
O(n) time · O(n) space23.30
Longest Palindrome 🔒 PROHash Maps · Spot it: only letter counts matter, not order
O(n) time · O(1) space23.31
Identify the Largest Outlier in an Array 🔒 PROHash Maps · Spot it: solve for the missing value, then look it up
O(n) time · O(n) space23.32
Find the Length of the Longest Common Prefix 🔒 PROHash Maps · Spot it: shared beginnings → store every prefix
O((n + m) · digits) time and space24.06
First Unique Character in a String 🔒 PROKnowing What to Track
O(n) time · O(1) space24.07
Find All Anagrams in a String 🔒 PROKnowing What to Track
O(n) time · O(1) space24.08
Longest Palindrome by Concatenating Two-Letter Words 🔒 PROKnowing What to Track
O(n) time · O(1) space24.09
Rank Teams by Votes 🔒 PROKnowing What to Track
O(v · t) count · then sort teams24.10
Pairs of Songs With Total Durations Divisible by 60 🔒 PROKnowing What to Track
O(n) time · O(1) space24.11
Minimum Number of Pushes to Type Word II 🔒 PROKnowing What to Track
O(n) time · O(1) space24.14
Divide Array Into Increasing Sequences 🔒 PROKnowing What to Track
O(n) time · O(1) space24.16
Count and Say 🔒 PROKnowing What to Track
O(sum_{k=1}^{n} L_k) — each term of length L_k is built in one O(L_{k-1}) scan of the preceding term.24.18
Find Words That Can Be Formed by Characters 🔒 PROKnowing What to Track
O(n) time · O(1) space24.19
Check if One String Swap Can Make Strings Equal 🔒 PROKnowing What to Track
O(n) time · O(1) space24.21
Sort Array by Increasing Frequency 🔒 PROKnowing What to Track
O(n log n) time · O(n) space24.24
Zero Array Transformation I 🔒 PROKnowing What to Track
O(n + q) time · O(n) space25.01
Redundant Connection ★ FREEUnion Find · Spot it: first link that makes a loop → union find
O(n) time · O(n) space25.02
Number of Islands 🔒 PROUnion Find · Spot it: count touching groups → union neighbors
O(m·n) time and space25.03
Most Stones Removed with Same Row or Column 🔒 PROUnion Find · Spot it: shared property links items → count groups
O(n²) time · O(n) space25.04
Longest Consecutive Sequence 🔒 PROUnion Find · Spot it: values chain by a rule → union, track size
O(n) time · O(n) space25.05
Last Day Where You Can Still Cross 🔒 PROUnion Find · Spot it: things only split → reverse time, then union
O(m·n) time and space25.06
Regions Cut by Slashes 🔒 PROUnion Find · Spot it: walls cut a space → count closed loops
O(n²) time and space25.07
Accounts Merge 🔒 PROUnion Find · Spot it: merge records sharing a key → union the keys
O(n log n) time · O(n) space25.08
Minimize Malware Spread 🔒 PROUnion Find · Spot it: spread fills groups → group sizes + counts
O(n²) time · O(n) space25.09
Find if Path Exists in Graph 🔒 PROUnion Find · Spot it: connected or not, route not needed → union find
O((n + e) α(n)) time · O(n) space25.10
The Skyline Problem 🔒 PROUnion Find · Spot it: fill ranges, skip filled parts → next free
O(n log n) time · O(n) space25.11
Similar String Groups 🔒 PROUnion Find · Spot it: groups follow chains of pairs → union find
O(n² · L) time · O(n) space25.12
Optimize Water Distribution in a Village 🔒 PROUnion Find · Spot it: connect all at least cost → Kruskal + union find
O(e log e) time · O(n) space25.13
Number of Islands II 🔒 PROUnion Find · Spot it: items arrive, count groups live → union find
O(k) time · O(k) space25.14
Evaluate Division 🔒 PROUnion Find · Spot it: pairwise factors → weighted union find
O((e + q) α) time · O(n) space26.01
Snapshot Array ★ FREECustom Data Structures · Spot it: read old versions → store only the changes
set O(1) · get O(log s) for s snaps26.02
Time Based Key-Value Store 🔒 PROCustom Data Structures · Spot it: value as of a time → sorted list + search
O(log n) get set O(1) · binary search in one key's list26.03
LRU Cache 🔒 PROCustom Data Structures · Spot it: lookup + recency order → map + linked list
O(1) get and put · O(capacity) space26.04
Insert Delete GetRandom O(1) 🔒 PROCustom Data Structures · Spot it: delete + random pick → array + index map
O(1) insert, remove and getRandom, on average26.05
Min Stack 🔒 PROCustom Data Structures · Spot it: stack + running min → a parallel stack
O(1) every call · O(n) extra space26.06
Range Module 🔒 PROCustom Data Structures · Spot it: covered stretches → sorted, disjoint ranges
O(log n) query add and remove are O(n) on a sorted list26.07
Shortest Word Distance II 🔒 PROCustom Data Structures · Spot it: fixed data, many queries → index it once
O(a + b) per query, for the two lists' lengths26.08
Design HashSet 🔒 PROCustom Data Structures · Spot it: build a set or map → hash into buckets
O(1) average worst case O(n / buckets) per call26.09
Max Stack 🔒 PROCustom Data Structures · Spot it: two orders at once → two blocks + marks
O(log n) per call, amortized · O(n) space26.10
Moving Average from Data Stream 🔒 PROCustom Data Structures · Spot it: sliding window on a stream → queue + sum
O(1) per call · O(window) space26.11
Two Sum III - Data structure design 🔒 PROCustom Data Structures · Spot it: make the frequent call cheap
add O(1) · O(n) space26.12
Range Sum Query - Immutable 🔒 PROCustom Data Structures · Spot it: fixed array + range totals → prefix sums
O(1) per query O(n) to build · O(n) space26.13
Stream of Characters 🔒 PROCustom Data Structures · Spot it: words ending now → trie of reversed words
O(L) per query L = longest word · trie built once26.14
All O`one Data Structure 🔒 PROCustom Data Structures · Spot it: ±1 counts + max/min → linked buckets
O(1) inc, dec, getMaxKey and getMinKey26.16
LFU Cache 🔒 PROCustom Data Structures · Spot it: evict by frequency → lists per count
O(1) get and put · O(capacity) space27.01
Find the Difference ★ FREEBitwise Manipulation · Spot it: all twins but one → XOR everything
O(n) time · O(1) space27.02
Complement of Base 10 Integer 🔒 PROBitwise Manipulation · Spot it: change some bits → build a mask, then XOR
O(log n) time · O(1) space27.03
Flipping an Image 🔒 PROBitwise Manipulation · Spot it: reverse + change → two pointers, mirror pairs
O(r × c) time · O(1) space27.04
Single Number 🔒 PROBitwise Manipulation · Spot it: pairs plus one → XOR the whole list
O(n) time · O(1) space27.06
Encode and Decode Strings 🔒 PROBitwise Manipulation · Spot it: no safe separator → fixed-size length prefix
O(n) time for n characters in total27.08
Find The K-th Lucky Number 🔒 PROBitwise Manipulation · Spot it: two choices per digit → binary counting
O(log k) time · O(log k) space27.09
Minimum Number of K Consecutive Bit Flips 🔒 PROBitwise Manipulation · Spot it: flip a window → greedy from the left, parity bit
O(n) time · O(n) space27.10
Find the Longest Substring Having Vowels in Even Counts 🔒 PROBitwise Manipulation · Spot it: odd or even counts → a parity mask
O(n) time · O(1) space27.11
Count Triplets That Can Form Two Arrays of Equal XOR 🔒 PROBitwise Manipulation · Spot it: XOR of ranges → prefix XORs
O(n²) time · O(n) space27.12
Longest Subarray With Maximum Bitwise AND 🔒 PROBitwise Manipulation
O(n) time · O(1) space27.13
Minimum One Bit Operations to Make Integers Zero 🔒 PROBitwise Manipulation
O(log n) time · O(1) space27.14
Triples with Bitwise AND Equal To Zero 🔒 PROBitwise Manipulation · Spot it: AND of three → count pair results first
O(n² + n · 2¹⁶) time27.15
Power of Two 🔒 PROBitwise Manipulation · Spot it: exactly one bit set → n & (n − 1)
O(1) time · O(1) space27.16
Hamming Distance 🔒 PROBitwise Manipulation · Spot it: how many bits differ → XOR, count ones
O(1) time · O(1) space27.17
Minimum Operations to Make the Integer Zero 🔒 PROBitwise Manipulation · Spot it: sum of powers of two → count the 1 bits
O(1) time · O(1) space27.18
Reverse Bits 🔒 PROBitwise Manipulation · Spot it: mirror the bits → read low, push into result
O(1) time · O(1) space28.01
Check If It Is a Straight Line ★ FREEMath and Geometry · Spot it: compare slopes → cross multiply
O(n) time · O(1) space28.02
Minimum Cuts to Divide a Circle 🔒 PROMath and Geometry · Spot it: symmetry → a formula, no simulation
O(1) time · O(1) space28.03
Rectangle Overlap 🔒 PROMath and Geometry · Spot it: axis-aligned shapes → one axis at a time
O(1) time · O(1) space28.04
Minimum Time Visiting All Points 🔒 PROMath and Geometry · Spot it: diagonal moves allowed → distance = larger gap
O(n) time · O(1) space28.05
Reverse Integer 🔒 PROMath and Geometry · Spot it: digits → remainder and divide by ten
O(digits) time · O(1) space28.06
Valid Square 🔒 PROMath and Geometry · Spot it: shape from points → compare squared distances
O(1) time · O(1) space28.07
Rectangle Area 🔒 PROMath and Geometry · Spot it: overlapping regions → add, minus the overlap
O(1) time · O(1) space28.08
Minimum Area Rectangle 🔒 PROMath and Geometry · Spot it: a few points fix the shape → set lookups
O(n²) time · O(n) space28.09
Maximum Area Rectangle With Point Constraints I 🔒 PROMath and Geometry · Spot it: tiny input → brute force is fine
O(n³) time · O(n) space28.10
Convex Polygon 🔒 PROMath and Geometry · Spot it: which way it bends → signs of cross products
O(n) time · O(1) space28.11
Queries on Number of Points Inside a Circle 🔒 PROMath and Geometry · Spot it: distance vs radius → compare squares
O(q·n) time · O(1) extra space28.12
Max Points on a Line 🔒 PROMath and Geometry · Spot it: group by direction → reduced slope keys
O(n²) time · O(n) space28.13
Maximum Number of Visible Points 🔒 PROMath and Geometry · Spot it: only direction matters → sorted angles + window
O(n log n) time · O(n) space28.14
Minimize Manhattan Distances 🔒 PROMath and Geometry · Spot it: max Manhattan distance → use x + y and x − y
O(n) time · O(n) space28.15
Self Crossing 🔒 PROMath and Geometry · Spot it: the path's shape limits hits → check a few recent moves
O(n) time · O(1) space28.16
Erect the Fence 🔒 PROMath and Geometry · Spot it: outer boundary → sort + keep one turn way
O(n log n) time · O(n) space28.17
Minimum Number of Lines to Cover Points 🔒 PROMath and Geometry · Spot it: about 10 items → bitmask + memo
bitmask DP · O(2ⁿ · n²) time28.18
Nth Magical Number 🔒 PROMath and Geometry · Spot it: easy to count up to x → binary search on x
O(log) time · O(1) space28.19
Add Strings 🔒 PROMath and Geometry · Spot it: numbers too long → digit by digit + carry
O(max(n, m)) time · O(max(n, m)) space28.20
Integer to English Words 🔒 PROMath and Geometry · Spot it: repeating chunks → one helper + scale words
O(1) at most four groups in 32 bits · O(1) space28.21
Fibonacci Number 🔒 PROMath and Geometry · Spot it: depends on the last few → keep only those
O(n) time · O(1) space28.22
Palindrome Number 🔒 PROMath and Geometry · Spot it: mirror check on a number → reverse half
O(log n) time · O(1) space28.28
Water and Jug Problem 🔒 PROMath and Geometry
O(log min(x, y)) time · O(1) space28.31
Detonate the Maximum Bombs 🔒 PROMath and Geometry
n² distance checks, n searches · O(n³) time28.32
Add to Array-Form of Integer 🔒 PROMath and Geometry
O(max(n, log k)) time · O(1) extra space28.34
Count Substrings with Only One Distinct Letter 🔒 PROMath and Geometry
O(n) time · O(1) space28.35
Adding Two Negabinary Numbers 🔒 PROMath and Geometry
O(n) time · O(n) space for the output28.36
Greatest Common Divisor of Strings 🔒 PROMath and Geometry
O(m + n) time · O(m + n) space28.39
Sum of k-Mirror Numbers 🔒 PROMath and Geometry
O(sqrt(X) * log_k X) — generates O(sqrt(X)) base-10 palindromes up to the n-th k-mirror number X, checking each in O(log_k X) steps.28.40
Sum of Squares of Special Elements 🔒 PROMath and Geometry
O(n) time · O(1) space