Guide to LeetCode: A Personal Guide to the Most Common and Useful Patterns for Solving Algorithm Questions

mrmarioruci

Mario Ruci

Posted on February 23, 2023

Guide to LeetCode: A Personal Guide to the Most Common and Useful Patterns for Solving Algorithm Questions

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.

 

  1. Brute Force - Beginner

Start simple. Try all possible solutions and check if they work.

Practice:

 

2. Greedy - Beginner

Make the locally optimal choice at each step and hope that it leads to a global optimum solution.

Practice:

 

3. Two-Pointers - Beginner

Use two pointers to traverse an array or a linked list simultaneously.

Practice:

 

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:

 

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:

 

6. Depth-First Search (DFS) - Intermediate

Traverse a graph or tree by exploring as far as possible along each branch before backtracking.

Practice:

 

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.

 

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.

 

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:

Notes:

  1. 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
  2. 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.
  3. The questions listed for practice might not be the most suitable ones, but you get the idea.
  4. 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.

💖 💪 🙅 🚩
mrmarioruci
Mario Ruci

Posted on February 23, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related