Here is a list of LeetCode problems I would recommend practicing if you haven't brushed up on algorithmic questions for long.
They can turbocharge your coding ability in a short period of time before a tech interview.
Please also check out my past Medium stories on some of the interesting questions: Cracking LeetCode Problems.
- 400. Nth Digit (C++)
- 440. K-th Smallest in Lexicographical Order (C++)
- 60. Permutation Sequence (C++)
- 31. Next Permutation (C++)
- 54. Spiral Matrix (C++)
- 68. Text Justification (C++)
- Corner case: the last line and lines that only accommodate one word.
- 166. Fraction to Recurring Decimal (C++)
- Detect a cycle.
- 466. Count the Repetitions (C++)
- For each start position in s2, how many characters of s2 (wrapping around) can we match by scanning s1 from start to end.
- 2122. Recover the Original Array (C++)
- 43. Multiply Strings (C++)
- 57. Insert Intervals (C++)
- 158. Read N Characters Given read4 II — Call Multiple Times (C++)
- Implement a buffered reader.
- 722. Remove Comments (C++)
- 148. Sort List (C++)
- Merge sort, using fast and slow pointers to locate the middle of the list.
- 25. Reverse Nodes in k-Group (C++)
- 708. Insert into a Sorted Circular Linked List (C++)
- Corner case: all values in the circular list are the same.
- 287. Find the Duplicate Number (C++)
- Detect the cycle in a linked list.
- 341. Flatten Nested List Iterator (C++)
- Maintain a stack (of list iterators) for traversing the leaves of a tree.
- 654. Maximum Binary Tree (C++)
- 117. Populating Next Right Pointers in Each Node II (C++)
- Level-order traversal of a binary tree.
- 863. All Nodes Distance K in Binary Tree (C++)
- First need to find the path from the root to a given node.
- Then enumerate common ancestors.
- 1382. Balance a Binary Search Tree (C++)
- 272. Closest Binary Search Tree Value II (C++)
- Next greater element in a binary search tree.
- 353. Design Snake Game (C++)
- 588. Design In-Memory File System (C++)
- 381. Insert Delete GetRandom O(1) — Duplicates allowed (C++)
- Vector + hash index.
- 432. All O(1) Data Structure (C++)
- Linked list + hash index
- 460. LFU Cache (C++)
- Linked list + hash index
- 895. Maximum Frequency Stack (C++)
- Stack + hash-based counter.
- 1206. Design Skiplist (C++)
- 394. Decode String (C++)
- Do not use recursion; use a stack instead.
- 891. Sum of Subsequence Widths (C++)
- The original order doesn’t matter.
- 902. Numbers At Most N Given Digit Set (C++)
- Digit DP.
- 600. Non-negative Integers without Consecutive Ones (C++)
- Digit DP.
- 2031. Count Subarrays With More Ones Than Zeros (C++)
- Requirement: O(n) time.
- 47. Permutations II (C++)
- Backtrack. Try not using recursion.
- 37. Sudoku Solver (C++)
- Backtrack. Try not using recursion.
- 140. Word Break II (C++)
- Backtrack + trie.
- 425. Word Squares (C++)
- Backtrack + trie.
- 282. Expression Add Operators (C++)
- Backtrack, maintaining the partial result of an infix expression as you go.
- 301. Remove Invalid Parentheses (C++)
- 780. Reaching Points (C++)
- Search backwards from (tx, ty) to (sx, sy).
- 1553. Minimum Number of Days to Eat N Oranges (C++)
- Backward BFS.
- 403. Frog Jump (C++)
- 329. Longest Increasing Path in a Matrix (C++)
- DFS + memoization.
- 212. Word Search II (C++)
- DFS + trie.
- 980. Unique Paths III (C++)
- DFS + bitmask.
- 489. Robot Room Cleaner (C++)
- 126. Word Ladder II (C++)
- 499. The Maze III (C++)
- 815. Bus Routes (C++)
- 2092. Find All People With Secret (C++)
- 691. Stickers to Spell Word (C++)
- BFS, clamping the occurrence of each character.
- 854. K-Similar Strings (C++)
- Pruning idea: consider a permutation as independent cycles and for each cycle choose only one transposition to start from.
- 2612. Minimum Reverse Operations (C++)
- Separate the data structure for odd and even numbers.
- 1136. Parallel Courses (C++)
- Topological sort + DP. Try not using recursion.
- 332. Reconstruct Itinerary (C++)
- Eulerian path. Try not using recursion.
- 505. The Maze II (C++)
- Dijkstra’s algorithm
- 1197. Minimum Knight Moves (C++)
- A* search.
- 1928. Minimum Cost to Reach Destination in Time (C++)
- No need to use Dijkstra. Use DP and exploit the sparsity of the graph.
- 310. Minimum Height Trees (C++)
- Find the centroid of a tree.
- 2440. Create Components With Same Value (C++)
- Split a tree into equal sums.
- 407. Trapping Rain Water II (C++)
- 834. Sum of Distances in Tree (C++)
- DP on a tree.
- 805. Split Array with Same Average (C++)
- Bitmask + cut in half.
- 548. Split Array with Equal Sum (C++)
- Cut in half.
- 1125. Smallest Sufficient Team (C++)
- Knapsack + bitmask.
- 698. Partition to K Equal Sum Subsets (C++)
- DP + bitmask.
- 943. Find the Shortest Superstring (C++)
- DP + bitmask
- 1723. Find Minimum Time to Finish All Jobs (C++)
- DP + subset enumeration.
- 1494. Parallel Courses II (C++)
- DP+ subset enumeration.
- 2172. Maximum AND Sum of Array (C++)
- Memoization with state compression.
- 1659. Maximize Grid Happiness (C++)
- Memoization with state compression.
- 81. Search in Rotated Sorted Array II (C++)
- Binary search.
- 4. Median of Two Sorted Arrays (C++)
- Divide and conquer. Take heed of the base case.
- 410. Split Array Largest Sum (C++)
- Binary search.
- 644. Maximum Average Subarray II (C++)
- Binary search, checking the running sum instead of the average.
- 878. Nth Magical Number (C++)
- Binary search.
- 483. Smallest Good Base (C++)
- Binary search.
- 1574. Shortest Subarray to be Removed to Make Array Sorted (C++)
- Two pointers.
- 581. Shortest Unsorted Continuous Subarray (C++)
- O(n) time, O(1) space.
- 1793. Maximum Score of a Good Subarray (C++)
- Two pointers.
- 1498. Number of Subsequences That Satisfy the Given Sum Condition (C++)
- Two pointers.
- 378. Kth Smallest Element in a Sorted Matrix (C++)
- Binary search + two pointers.
- 786. K-th Smallest Prime Fraction (C++)
- Binary search + two pointers.
- 2040. Kth Smallest Product of Two Sorted Arrays (C++)
- Binary search + two pointers.
- 719. Find K-th Smallest Pair Distance (C++)
- Binary search + sliding window.
- 30. Substring with Concatenation of All Words (C++)
- Sliding window.
- 992. Subarrays with K Different Integers (C++)
- Sliding window.
- 324. Wiggle Sort II (C++)
- O(n) time, three-way partition (q-sort).
- 1471. The k Strongest Values in an Array (C++)
- O(n) time, three-way partition.
- 164. Maximum Gap (C++)
- Radix sort.
- 84. Largest Rectangle in Histogram (C++)
- Monotonic stack.
- 1950. Maximum of Minimum Values in All Subarrays (C++)
- Monotonic stack.
- 1130. Minimum Cost Tree From Leaf Values (C++)
- Surprisingly, this can be done in O(n) time using the monotonic stack.
- 962. Maximum Width Ramp (C++)
- O(n) time, monotonic stack + two pointers.
- 456. 132 Pattern (C++)
- O(n) time, monotonic stack.
- 316. Remove Duplicate Letters (C++)
- O(n) time, monotonic stack.
- 2030. Smallest K-Length Subsequence With Occurrences of a Letter (C++)
- O(n) time, monotonic stack.
- 2289. Steps to Make Array Non-decreasing (C++)
- O(n) time, monotonic stack.
- 321. Create Maximum Number (C++)
- Monotonic stack.
- 239. Sliding Window Maximum (C++)
- O(n) time, monotonic queue.
- 862. Shortest Subarray with Sum at Least K (C++)
- O(n) time, monotonic queue.
- 1499. Max Value of Equation (C++)
- Sliding window + monotonic queue.
- 1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit (C++)
- Sliding window + monotonic queue.
- 1687. Delivering Boxes from Storage to Ports (C++)
- DP + monotonic queue.
- 1834. Single-Threaded CPU (C++)
- 1353. Maximum Number of Events That Can Be Attended (C++)
- 1882. Process Tasks Using Servers (C++)
- 358. Rearrange String k Distance Apart (C++)
- 659. Split Array into Consecutive Subsequences (C++)
- 1717. Maximum Score From Removing Substrings (C++)
- 502. IPO (C++)
- 1665. Minimum Initial Energy to Finish Tasks (C++)
- 1326. Minimum Number of Taps to Open to Water a Garden (C++)
- 630. Course Schedule III (C++)
- 857. Minimum Cost to Hire K Workers (C++)
- 757. Set Intersection Size At Least Two (C++)
- 1489. Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree (C++)
- Kruskal’s algorithm.
- 313. Super Ugly Number (C++)
- Priority queue.
- 2386. Find the K-Sum of an Array (C++)
- Enumerate k smallest subset sums.
- 373. Find K Pairs with Smallest Sums (C++)
- K-way merge.
- 1508. Range Sum of Sorted Subarray Sums (C++)
- K-way merge.
- 1675. Minimize Deviation in Array (C++)
- K-way merge.
- 768. Max Chunks To Make Sorted II (C++)
- 149. Max Points on a Line (C++)
- Normalize and hash a line.
- 711. Number of Distinct Islands II (C++)
- Normalize and hash an island.
- 1316. Distinct Echo Substrings (C++)
- Rolling hash.
- 715. Range Module (C++)
- Maintain a tree map for disjoint intervals.
- 699. Falling Squares (C++)
- Tree map.
- 391. Perfect Rectangle (C++)
- Tree map.
- 363. Max Sum of Rectangle No Larger Than K (C++)
- DP + tree map.
- 218. The Skyline Problem (C++)
- Line sweep.
- 3009. Maximum Number of Intersections on the Chart (C++)
- Line sweep, or difference array.
- 480. Sliding Window Median (C++)
- Use two std::multisets.
- 1825. Finding MK Average (C++)
- std::deque + std::multiset
- 1703. Minimum Adjacent Swaps for K Consecutive Ones (C++)
- Running sum of distances to the sliding window median.
- 2448. Minimum Cost to Make Array Equal (C++)
- Weighted median.
- 1554. Strings Differ by One Character (C++)
- Trie or rolling hash.
- 1803. Count Pairs With XOR in a Range (C++)
- Trie.
- 642. Design Search Autocomplete System (C++)
- Trie.
- 3045. Count Prefix and Suffix Pairs II (C++)
- Trie.
- 803. Bricks Falling When Hit (C++)
- Disjoint set.
- 947. Most Stones Removed with Same Row or Column (C++)
- Disjoint set.
- 1632. Rank Transform of a Matrix (C++)
- Disjoint set + topological sort.
- 1998. GCD Sort of an Array (C++)
- Disjoint set + sieve of Eratosthenes.
- 308. Range Sum Query 2D — Mutable (C++)
- Use this to test your binary indexed tree.
- 327. Count of Range Sum (C++)
- Binary indexed tree or merge sort.
- 2179. Count Good Triplets in an Array (C++)
- Binary indexed tree.
- 1649. Create Sorted Array through Instructions (C++)
- Binary indexed tree.
- 1505. Minimum Possible Integer After at Most K Adjacent Swaps On Digits (C++)
- Binary indexed tree.
- 3165. Maximum Sum of Subsequence With Non-adjacent Elements (C++)
- Segment Tree
- 2926. Maximum Balanced Subsequence Sum (C++)
- DP + segment tree.
- 1674. Minimum Moves to Make Array Complementary (C++)
- Difference array.
- 798. Smallest Rotation with Highest Score (C++)
- Difference array.
- 220. Contains Duplicate III (C++)
- O(n) time, bucketing.
- 1714. Sum Of Special Evenly-Spaced Elements In Array (C++)
- Sqrt decomposition.
- 214. Shortest Palindrome (C++)
- Use this to test your KMP implementation.
- 3008. Find Beautiful Indices in the Given Array II (C++)
- KMP algorithm.
- 735. Asteroid Collision (C++)
- Stack.
- 331. Verify Preorder Serialization of a Binary Tree (C++)
- One pass using a stack.
- 255. Verify Preorder Sequence in Binary Search Tree (C++)
- One pass using a stack.
- 889. Construct Binary Tree from Preorder and Postorder Traversal (C++)
- One pass using a stack.
- 1008. Construct Binary Search Tree from Preorder Traversal (C++)
- One pass using a stack.
- 772. Basic Calculator III (C++)
- Try not using recursion.
- 1096. Brace Expansion II (C++)
- A variant of the calculator question.
- 1597. Build Binary Expression Tree From Infix Expression (C++)
- Try not using recursion.
- 1896. Minimum Cost to Change the Final Value of Expression (C++)
- Try not using recursion.
- 956. Tallest Billboard (C++)
- Knapsack.
- 2143. Choose Numbers From Two Arrays in Range (C++)
- Knapsack.
- 1787. Make the XOR of All Segments Equal to Zero (C++)
- Knapsack.
- 879. Profitable Schemes (C++)
- 1883. Minimum Skips to Arrive at Meeting On Time (C++)
- 1191. K-Concatenation Maximum Sum (C++)
- 188. Best Time to Buy and Sell Stock IV (C++)
- 2809. Minimum Time to Make Array Sum At Most x (C++)
- 727. Minimum Window Subsequence (C++)
- 265. Paint House II (C++)
- 1937. Maximum Number of Points with Cost (C++)
- 1388. Pizza With 3n Slices (C++)
- This reduces to LC 213.
- 174. Dungeon Game (C++)
- 656. Coin Path (C++)
- Lexicographically smallest path.
- 741. Cherry Pickup (C++)
- 32. Longest Valid Parentheses (C++)
- 1997. First Day Where You Have Been in All the Rooms (C++)
- 964. Least Operators to Express Number (C++)
- 552. Student Attendance Record II (C++)
- 903. Valid Permutations for DI Sequence (C++)
- 629. K Inverse Pairs Array (C++)
- 920. Number of Music Playlists (C++)
- 940. Distinct Subsequences II (C++)
- 730. Count Different Palindromic Subsequences (C++)
- 639. Decode Ways II (C++)
- 3003. Maximize the Number of Partitions After Operations (C++)
- 2478. Number of Beautiful Partitions (C++)
- DP + sliding window.
- 1478. Allocate Mailboxes (C++)
- The “Word Break” pattern.
- 1147. Longest Chunked Palindrome Decomposition (C++)
- 354. Russian Doll Envelopes (C++)
- Longest increasing subsequence.
- 1713. Minimum Operations to Make a Subsequence (C++)
- Longest increasing subsequence.
- 2111. Minimum Operations to Make the Array K-Increasing (C++)
- Longest increasing subsequence.
- 10. Regular Expression Matching (C++)
- Longest common subsequence.
- 2060. Check if an Original String Exists Given Two Encoded Strings (C++)
- Memoization.
- 1092. Shortest Common Supersequence (C++)
- Longest common subsequence.
- 2430. Maximum Deletions on a String (C++)
- Longest common subsequence.
- 1216. Valid Palindrome III (C++)
- Longest common subsequence.
- 471. Encode String with Shortest Length (C++)
- 1000. Minimum Cost to Merge Stones (C++)
- 312. Burst Balloons (C++)
- 546. Remove Boxes (C++)
- 664. Strange Printer (C++)
- 1246. Palindrome Removal (C++)
- 1531. String Compression II (C++)
- 887. Super Egg Drop (C++)
- This can be done in O(k * n) time.
- 1140. Stone Game (C++)
- Minimax.
- 913. Cat and Mouse (C++)
- Minimax, taking account of a draw.
- 843. Guess the Word (C++)
- An interesting interactive problem.
- 1538. Guess the Majority in a Hidden Array (C++)
- Logical reasoning.
- 3022. Minimize OR of Remaining Elements Using Operations (C++)
- Bit manipulation.
- 921. Minimum Add to Make Parentheses Valid (C++)
- 2116. Check if a Parentheses String Can Be Valid (C++)
- 1247. Minimum Swaps to Make Strings Equal (C++)
- 777. Swap Adjacent in LR String (C++)
- 1702. Maximum Binary String After Change (C++)
- 2561. Rearranging Fruits (C++)
- 2571. Minimum Operations to Reduce an Integer to 0 (C++)
- 2790. Maximum Number of Groups With Increasing Length (C++)
- Greedy. Doesn't need a binary search.
- 2333. Minimum Sum of Squared Difference (C++)
- Greedy.
- 1192. Critical Connections in a Network (C++)
- Find all edges in cycles.
- 1820. Maximum Number of Accepted Invitations (C++)
- Maximum flow.
- 2836. Maximize Value of Function in a Ball Passing Game (C++)
- Binary jumping.
- 1782. Count Pairs Of Nodes (C++)
- Take advantage of the sparsity of the graph.
- 229. Majority Element II (C++)
- Boyer-Moore majority vote algorithm.
- 1131. Maximum of Absolute Value Expression (C++)
- 1735. Count Ways to Make Array With Product (C++)
- Sieve of Eratosthenes + combinations with repetitions.
- 2584. Split the Array to Make Coprime Products (C++)
- 2183. Count Array Pairs Divisible by K (C++)
- 1819. Number of Different Subsequences GCDs (C++)
- 1622. Fancy Sequence (C++)
- Modular multiplicative inverse.
- 1401. Circle and Rectangle Overlapping (C++)
- Geometry
- 1453. Maximum Number of Darts Inside of a Circular Dartboard (C++)
- Geometry
- 587. Erect the Fence (C++)
- Graham scan for constructing convex hulls.
- 1117. Building H2O (C++)
- Concurrency
Copyright 2022-2023 Devin Zuo
Creative Commons Zero v1.0 Universal
https://creativecommons.org/publicdomain/zero/1.0/