Are you tired of struggling with algorithm questions on LeetCode? As someone (with 5+ years of experience) who has started practicing and studying these problems, I can relate to the frustration of not knowing where to start or how to approach a particularly tricky question. After much trial and error, I have come up with a personal guide to the most common and useful patterns for solving algorithm questions, which I hope can help others navigate this challenging terrain.
For each pattern I have listed 10 LeetCode questions to practice on and the list is sorted by learning curve.
- Brute Force - Beginner
Start simple. Try all possible solutions and check if they work.
Practice:
- Two Sum: https://leetcode.com/problems/two-sum/
- Reverse Integer: https://leetcode.com/problems/reverse-integer/
- Palindrome Number: https://leetcode.com/problems/palindrome-number/
- Climbing Stairs: https://leetcode.com/problems/climbing-stairs/
- Valid Parentheses: https://leetcode.com/problems/valid-parentheses/
- Roman to Integer: https://leetcode.com/problems/roman-to-integer/
- Remove Duplicates from Sorted Array: https://leetcode.com/problems/remove-duplicates-from-sorted-array/
- Plus One: https://leetcode.com/problems/plus-one/
- Merge Two Sorted Lists: https://leetcode.com/problems/merge-two-sorted-lists/
- Maximum Subarray: https://leetcode.com/problems/maximum-subarray/
2. Greedy - Beginner
Make the locally optimal choice at each step and hope that it leads to a global optimum solution.
Practice:
- Jump Game: https://leetcode.com/problems/jump-game/
- Best Time to Buy and Sell Stock: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
- Gas Station: https://leetcode.com/problems/gas-station/
- Minimum Number of Arrows to Burst Balloons: https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/
- Non-overlapping Intervals: https://leetcode.com/problems/non-overlapping-intervals/
- Assign Cookies: https://leetcode.com/problems/assign-cookies/
- Jump Game II: https://leetcode.com/problems/jump-game-ii/
- Lemonade Change: https://leetcode.com/problems/lemonade-change/
- Queue Reconstruction by Height: https://leetcode.com/problems/queue-reconstruction-by-height/
- Candy: https://leetcode.com/problems/candy/
3. Two-Pointers - Beginner
Use two pointers to traverse an array or a linked list simultaneously.
Practice:
- Remove Duplicates from Sorted Array: https://leetcode.com/problems/remove-duplicates-from-sorted-array/
- Container With Most Water: https://leetcode.com/problems/container-with-most-water/
- Trapping Rain Water: https://leetcode.com/problems/trapping-rain-water/
- Merge Two Sorted Lists: https://leetcode.com/problems/merge-two-sorted-lists/
- Remove Element: https://leetcode.com/problems/remove-element/
- Squares of a Sorted Array: https://leetcode.com/problems/squares-of-a-sorted-array/
- 3Sum: https://leetcode.com/problems/3sum/
- Backspace String Compare: https://leetcode.com/problems/backspace-string-compare/
- Valid Palindrome: https://leetcode.com/problems/valid-palindrome/
- Longest Palindromic Substring: https://leetcode.com/problems/longest-palindromic-substring/
4. Sliding Window - Beginner
Maintain a window of elements in an array or a string and move the window to find the solution. Combined with 2 pointers.
Practice:
- Longest Substring Without Repeating Characters: https://leetcode.com/problems/longest-substring-without-repeating-characters/
- Minimum Size Subarray Sum: https://leetcode.com/problems/minimum-size-subarray-sum/
- Longest Repeating Character Replacement: https://leetcode.com/problems/longest-repeating-character-replacement/
- Permutation in String: https://leetcode.com/problems/permutation-in-string/
- Minimum Window Substring: https://leetcode.com/problems/minimum-window-substring/
- Maximum Erasure Value: https://leetcode.com/problems/maximum-erasure-value/
- Fruit Into Baskets: https://leetcode.com/problems/fruit-into-baskets/
- Minimum Operations to Reduce X to Zero: https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/
- Get Maximum in Generated Array: https://leetcode.com/problems/get-maximum-in-generated-array/
- Subarrays with K Different Integers: https://leetcode.com/problems/subarrays-with-k-different-integers/
5. Backtracking - Intermediate
Systematically explore all possible solutions by building candidates and then abandoning them as soon as you determine that they cannot lead to a valid solution.
Practice:
- Letter Combinations of a Phone Number: https://leetcode.com/problems/letter-combinations-of-a-phone-number/
- Generate Parentheses: https://leetcode.com/problems/generate-parentheses/
- Combination Sum: https://leetcode.com/problems/combination-sum/
- Permutations: https://leetcode.com/problems/permutations/
- Word Search: https://leetcode.com/problems/word-search/
- Palindrome Partitioning: https://leetcode.com/problems/palindrome-partitioning/
- N-Queens: https://leetcode.com/problems/n-queens/
- Subsets: https://leetcode.com/problems/subsets/
- Combination Sum II: https://leetcode.com/problems/combination-sum-ii/
- Restore IP Addresses: https://leetcode.com/problems/restore-ip-addresses/
6. Depth-First Search (DFS) - Intermediate
Traverse a graph or tree by exploring as far as possible along each branch before backtracking.
Practice:
- Invert Binary Tree: https://leetcode.com/problems/invert-binary-tree
- Binary Tree Inorder Traversal: https://leetcode.com/problems/binary-tree-inorder-traversal
- Maximum Depth of Binary Tree: https://leetcode.com/problems/maximum-depth-of-binary-tree
- Symmetric Tree: https://leetcode.com/problems/symmetric-tree
- Binary Tree Preorder Traversal: https://leetcode.com/problems/binary-tree-preorder-traversal
- Path Sum: https://leetcode.com/problems/path-sum
- Kth Smallest Element in a BST: https://leetcode.com/problems/kth-smallest-element-in-a-bst
- Lowest Common Ancestor of a Binary Search Tree: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree
- Validate Binary Search Tree: https://leetcode.com/problems/validate-binary-search-tree
- Recover Binary Search Tree: https://leetcode.com/problems/recover-binary-search-tree
7. Bepth-First Search (BFS) - Intermediate
Traverse a graph or tree by exploring all the neighbors at the current level before moving on to the next level.
Practice:
You can practice the same as DFS and the following.
- Populating Next Right Pointers in Each Node: https://leetcode.com/problems/populating-next-right-pointers-in-each-node
- Populating Next Right Pointers in Each Node II: https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii
- Surrounded Regions: https://leetcode.com/tag/breadth-first-search/#:~:text=130-,Surrounded%20Regions,-36.5%25
- Number of Islands: https://leetcode.com/problems/number-of-islands
- Course Schedule: https://leetcode.com/problems/course-schedule
8. Divide and Conquer - Advanced
Break down a problem into smaller sub-problems, solve each sub-problem independently, and then combine the solutions to get the final result.
- Maximum Subarray: https://leetcode.com/problems/maximum-subarray/
- Pow(x, n): https://leetcode.com/problems/powx-n/
- Merge k Sorted Lists: https://leetcode.com/problems/merge-k-sorted-lists/
- Count of Smaller Numbers After Self: https://leetcode.com/problems/count-of-smaller-numbers-after-self/
- Find K Closest Elements: https://leetcode.com/problems/find-k-closest-elements/
- Majority Element: https://leetcode.com/problems/majority-element/
- Unique Binary Search Trees: https://leetcode.com/problems/unique-binary-search-trees/
- Kth Largest Element in an Array: https://leetcode.com/problems/kth-largest-element-in-an-array/
- Search for a Range: https://leetcode.com/problems/search-for-a-range/
- Closest Binary Search Tree Value II: https://leetcode.com/problems/closest-binary-search-tree-value-ii/
9. Dynamic Programming - Advanced
Break down a complex problem into smaller sub-problems and solve them individually. This approach can be used to solve problems that exhibit overlapping subproblems.
Practice:
- Climbing Stairs: https://leetcode.com/problems/climbing-stairs/
- Best Time to Buy and Sell Stock: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
- Longest Increasing Subsequence: https://leetcode.com/problems/longest-increasing-subsequence/
- Coin Change: https://leetcode.com/problems/coin-change/
- Maximum Subarray: https://leetcode.com/problems/maximum-subarray/
- House Robber: https://leetcode.com/problems/house-robber/
- Unique Paths: https://leetcode.com/problems/unique-paths/
- Decode Ways: https://leetcode.com/problems/decode-ways/
- Maximum Product Subarray: https://leetcode.com/problems/maximum-product-subarray/
- Palindrome Partitioning II: https://leetcode.com/problems/palindrome-partitioning-ii/
Notes:
- I would suggest you start with some data structure basics, then solve around 20 easy questions to get an idea of the questions and their formatting
- These are not all the patterns that exist, and the questions are not listed by difficulty. Each pattern might have variations or be combined with another one. By practicing you will get better.
- The questions listed for practice might not be the most suitable ones, but you get the idea.
- This list serves as a starting point for those who don't know how to get started. Follow the path or just create your own, "memento mori".
Thanks for reading.
Top comments (5)
Awesome, dude! Thanks a lot for this amazing opportunity! I've been working for this small company for the last five years, and I've become really familiar with our business model and the products we create as a team. But, man, I gotta tell you, when I started tackling some of these exercises, I was blown away! I struggled with some of the beginner-level exercises, and that just made me more eager to learn even more! I think it's super important to always stay up-to-date with the latest techniques and tricks, and not get too comfortable in our own bubble
Happy i could help a bit. A good platform where you can learn more in detail that I found is neetcode.io.
I will take a look thanks a lot!
In 2023, with ChatGPT you can level up your Leetcode game and problem solving skills with this open source tool: leetchatgpt.com/
youtube.com/watch?v=yQ7m25nEJTA
Sure, cool tool.