Guide to LeetCode: A Personal Guide to the Most Common and Useful Patterns for Solving Algorithm Questions
Tags: leetcode, programming, interview
Published: 2023-02-23 19:26:47

<p>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.</p>
<p>For each pattern I have listed 10 LeetCode questions to practice on and the list is sorted by learning curve.</p>
<p>&nbsp;</p>

<ol>
	<li><strong>Brute Force -&nbsp;</strong>Beginner</li>
</ol>
<p>Start simple. Try all possible solutions and check if they work.</p>
<p>Practice:</p>
<ul>
	<li>Two Sum:&nbsp;<a href="https://leetcode.com/problems/two-sum/">https://leetcode.com/problems/two-sum/</a></li>
	<li>Reverse Integer:&nbsp;<a href="https://leetcode.com/problems/reverse-integer/">https://leetcode.com/problems/reverse-integer/</a></li>
	<li>Palindrome Number:&nbsp;<a href="https://leetcode.com/problems/palindrome-number/">https://leetcode.com/problems/palindrome-number/</a></li>
	<li>Climbing Stairs:&nbsp;<a href="https://leetcode.com/problems/climbing-stairs/">https://leetcode.com/problems/climbing-stairs/</a></li>
	<li>Valid Parentheses:&nbsp;<a href="https://leetcode.com/problems/valid-parentheses/">https://leetcode.com/problems/valid-parentheses/</a></li>
	<li>Roman to Integer:&nbsp;<a href="https://leetcode.com/problems/roman-to-integer/">https://leetcode.com/problems/roman-to-integer/</a></li>
	<li>Remove Duplicates from Sorted Array:&nbsp;<a href="https://leetcode.com/problems/remove-duplicates-from-sorted-array/">https://leetcode.com/problems/remove-duplicates-from-sorted-array/</a></li>
	<li>Plus One:&nbsp;<a href="https://leetcode.com/problems/plus-one/">https://leetcode.com/problems/plus-one/</a></li>
	<li>Merge Two Sorted Lists:&nbsp;<a href="https://leetcode.com/problems/merge-two-sorted-lists/">https://leetcode.com/problems/merge-two-sorted-lists/</a></li>
	<li>Maximum Subarray:&nbsp;<a href="https://leetcode.com/problems/maximum-subarray/">https://leetcode.com/problems/maximum-subarray/</a></li>
</ul>
<br/><p>&nbsp;</p>
<p>2.&nbsp;<strong>Greedy</strong>&nbsp;- Beginner</p>
<p>Make the locally optimal choice at each step and hope that it leads to a global optimum solution.</p>
<p>Practice:</p>
<ul>
	<li>Jump Game:&nbsp;<a href="https://leetcode.com/problems/jump-game/">https://leetcode.com/problems/jump-game/</a></li>
	<li>Best Time to Buy and Sell Stock:&nbsp;<a href="https://leetcode.com/problems/best-time-to-buy-and-sell-stock/">https://leetcode.com/problems/best-time-to-buy-and-sell-stock/</a></li>
	<li>Gas Station:&nbsp;<a href="https://leetcode.com/problems/gas-station/">https://leetcode.com/problems/gas-station/</a></li>
	<li>Minimum Number of Arrows to Burst Balloons:&nbsp;<a href="https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/">https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/</a></li>
	<li>Non-overlapping Intervals:&nbsp;<a href="https://leetcode.com/problems/non-overlapping-intervals/">https://leetcode.com/problems/non-overlapping-intervals/</a></li>
	<li>Assign Cookies:&nbsp;<a href="https://leetcode.com/problems/assign-cookies/">https://leetcode.com/problems/assign-cookies/</a></li>
	<li>Jump Game II:&nbsp;<a href="https://leetcode.com/problems/jump-game-ii/">https://leetcode.com/problems/jump-game-ii/</a></li>
	<li>Lemonade Change:&nbsp;<a href="https://leetcode.com/problems/lemonade-change/">https://leetcode.com/problems/lemonade-change/</a></li>
	<li>Queue Reconstruction by Height:&nbsp;<a href="https://leetcode.com/problems/queue-reconstruction-by-height/">https://leetcode.com/problems/queue-reconstruction-by-height/</a></li>
	<li>Candy:&nbsp;<a href="https://leetcode.com/problems/candy/">https://leetcode.com/problems/candy/</a></li>
</ul>
<p>&nbsp;</p>
<p>3.&nbsp;<strong>Two-Pointers -&nbsp;</strong>Beginner</p>
<p>Use two pointers to traverse an array or a linked list simultaneously.</p>
<p>Practice:</p>
<ul>
	<li>Remove Duplicates from Sorted Array:&nbsp;<a href="https://leetcode.com/problems/remove-duplicates-from-sorted-array/">https://leetcode.com/problems/remove-duplicates-from-sorted-array/</a></li>
	<li>Container With Most Water:&nbsp;<a href="https://leetcode.com/problems/container-with-most-water/">https://leetcode.com/problems/container-with-most-water/</a></li>
	<li>Trapping Rain Water:&nbsp;<a href="https://leetcode.com/problems/trapping-rain-water/">https://leetcode.com/problems/trapping-rain-water/</a></li>
	<li>Merge Two Sorted Lists:&nbsp;<a href="https://leetcode.com/problems/merge-two-sorted-lists/">https://leetcode.com/problems/merge-two-sorted-lists/</a></li>
	<li>Remove Element:&nbsp;<a href="https://leetcode.com/problems/remove-element/">https://leetcode.com/problems/remove-element/</a></li>
	<li>Squares of a Sorted Array:&nbsp;<a href="https://leetcode.com/problems/squares-of-a-sorted-array/">https://leetcode.com/problems/squares-of-a-sorted-array/</a></li>
	<li>3Sum:&nbsp;<a href="https://leetcode.com/problems/3sum/">https://leetcode.com/problems/3sum/</a></li>
	<li>Backspace String Compare:&nbsp;<a href="https://leetcode.com/problems/backspace-string-compare/">https://leetcode.com/problems/backspace-string-compare/</a></li>
	<li>Valid Palindrome:&nbsp;<a href="https://leetcode.com/problems/valid-palindrome/">https://leetcode.com/problems/valid-palindrome/</a></li>
	<li>Longest Palindromic Substring:&nbsp;<a href="https://leetcode.com/problems/longest-palindromic-substring/">https://leetcode.com/problems/longest-palindromic-substring/</a></li>
</ul>
<p>&nbsp;</p>
<p>4.&nbsp;<strong>Sliding Window</strong>&nbsp;- Beginner</p>
<p>Maintain a window of elements in an array or a string and move the window to find the solution. Combined with 2 pointers.</p>
<p>Practice:</p>
<ul>
	<li>Longest Substring Without Repeating Characters:&nbsp;<a href="https://leetcode.com/problems/longest-substring-without-repeating-characters/">https://leetcode.com/problems/longest-substring-without-repeating-characters/</a></li>
	<li>Minimum Size Subarray Sum:&nbsp;<a href="https://leetcode.com/problems/minimum-size-subarray-sum/">https://leetcode.com/problems/minimum-size-subarray-sum/</a></li>
	<li>Longest Repeating Character Replacement:&nbsp;<a href="https://leetcode.com/problems/longest-repeating-character-replacement/">https://leetcode.com/problems/longest-repeating-character-replacement/</a></li>
	<li>Permutation in String:&nbsp;<a href="https://leetcode.com/problems/permutation-in-string/">https://leetcode.com/problems/permutation-in-string/</a></li>
	<li>Minimum Window Substring:&nbsp;<a href="https://leetcode.com/problems/minimum-window-substring/">https://leetcode.com/problems/minimum-window-substring/</a></li>
	<li>Maximum Erasure Value:&nbsp;<a href="https://leetcode.com/problems/maximum-erasure-value/">https://leetcode.com/problems/maximum-erasure-value/</a></li>
	<li>Fruit Into Baskets:&nbsp;<a href="https://leetcode.com/problems/fruit-into-baskets/">https://leetcode.com/problems/fruit-into-baskets/</a></li>
	<li>Minimum Operations to Reduce X to Zero:&nbsp;<a href="https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/">https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/</a></li>
	<li>Get Maximum in Generated Array:&nbsp;<a href="https://leetcode.com/problems/get-maximum-in-generated-array/">https://leetcode.com/problems/get-maximum-in-generated-array/</a></li>
	<li>Subarrays with K Different Integers:&nbsp;<a href="https://leetcode.com/problems/subarrays-with-k-different-integers/">https://leetcode.com/problems/subarrays-with-k-different-integers/</a></li>
</ul>
<p>&nbsp;</p>
<p>5.&nbsp;<strong>Backtracking</strong>&nbsp;- Intermediate</p>
<p>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.</p>
<p>Practice:</p>
<ul>
	<li>Letter Combinations of a Phone Number:&nbsp;<a href="https://leetcode.com/problems/letter-combinations-of-a-phone-number/">https://leetcode.com/problems/letter-combinations-of-a-phone-number/</a></li>
	<li>Generate Parentheses:&nbsp;<a href="https://leetcode.com/problems/generate-parentheses/">https://leetcode.com/problems/generate-parentheses/</a></li>
	<li>Combination Sum:&nbsp;<a href="https://leetcode.com/problems/combination-sum/">https://leetcode.com/problems/combination-sum/</a></li>
	<li>Permutations:&nbsp;<a href="https://leetcode.com/problems/permutations/">https://leetcode.com/problems/permutations/</a></li>
	<li>Word Search:&nbsp;<a href="https://leetcode.com/problems/word-search/">https://leetcode.com/problems/word-search/</a></li>
	<li>Palindrome Partitioning:&nbsp;<a href="https://leetcode.com/problems/palindrome-partitioning/">https://leetcode.com/problems/palindrome-partitioning/</a></li>
	<li>N-Queens:&nbsp;<a href="https://leetcode.com/problems/n-queens/">https://leetcode.com/problems/n-queens/</a></li>
	<li>Subsets:&nbsp;<a href="https://leetcode.com/problems/subsets/">https://leetcode.com/problems/subsets/</a></li>
	<li>Combination Sum II:&nbsp;<a href="https://leetcode.com/problems/combination-sum-ii/">https://leetcode.com/problems/combination-sum-ii/</a></li>
	<li>Restore IP Addresses:&nbsp;<a href="https://leetcode.com/problems/restore-ip-addresses/">https://leetcode.com/problems/restore-ip-addresses/</a></li>
</ul>
<p>&nbsp;</p>
<p>6.&nbsp;<strong>Depth-First Search (DFS) -&nbsp;</strong>Intermediate</p>
<p>Traverse a graph or tree by exploring as far as possible along each branch before backtracking.</p>
<p>Practice:</p>
<ul>
	<li>Invert Binary Tree:&nbsp;<a href="https://leetcode.com/problems/invert-binary-tree">https://leetcode.com/problems/invert-binary-tree</a></li>
	<li>Binary Tree Inorder Traversal:&nbsp;<a href="https://leetcode.com/problems/binary-tree-inorder-traversal">https://leetcode.com/problems/binary-tree-inorder-traversal</a></li>
	<li>Maximum Depth of Binary Tree:&nbsp;<a href="https://leetcode.com/problems/maximum-depth-of-binary-tree">https://leetcode.com/problems/maximum-depth-of-binary-tree</a></li>
	<li>Symmetric Tree:&nbsp;<a href="https://leetcode.com/problems/symmetric-tree">https://leetcode.com/problems/symmetric-tree</a></li>
	<li>Binary Tree Preorder Traversal:&nbsp;<a href="https://leetcode.com/problems/binary-tree-preorder-traversal">https://leetcode.com/problems/binary-tree-preorder-traversal</a></li>
	<li>Path Sum:&nbsp;<a href="https://leetcode.com/problems/path-sum">https://leetcode.com/problems/path-sum</a></li>
	<li>Kth Smallest Element in a BST:&nbsp;<a href="https://leetcode.com/problems/kth-smallest-element-in-a-bst">https://leetcode.com/problems/kth-smallest-element-in-a-bst</a></li>
	<li>Lowest Common Ancestor of a Binary Search Tree:&nbsp;<a href="https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree">https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree</a></li>
	<li>Validate Binary Search Tree:&nbsp;<a href="https://leetcode.com/problems/validate-binary-search-tree">https://leetcode.com/problems/validate-binary-search-tree</a></li>
	<li>Recover Binary Search Tree:&nbsp;<a href="https://leetcode.com/problems/recover-binary-search-tree">https://leetcode.com/problems/recover-binary-search-tree</a></li>
</ul>
<p>&nbsp;</p>
<p>7.&nbsp;<strong>Bepth-First Search (BFS)</strong>&nbsp;- Intermediate</p>
<p>Traverse a graph or tree by exploring all the neighbors at the current level before moving on to the next level.</p>
<p>Practice:</p>
<p>You can practice the same as DFS and the following.</p>
<ul>
	<li>Populating Next Right Pointers in Each Node:&nbsp;<a href="https://leetcode.com/problems/populating-next-right-pointers-in-each-node">https://leetcode.com/problems/populating-next-right-pointers-in-each-node</a></li>
	<li>Populating Next Right Pointers in Each Node II:&nbsp;<a href="https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii">https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii</a></li>
	<li>Surrounded Regions:&nbsp;<a href="https://leetcode.com/tag/breadth-first-search/#:~:text=130-,Surrounded%20Regions,-36.5%25">https://leetcode.com/tag/breadth-first-search/#:~:text=130-,Surrounded%20Regions,-36.5%25</a></li>
	<li>Number of Islands:&nbsp;<a href="https://leetcode.com/problems/number-of-islands">https://leetcode.com/problems/number-of-islands</a></li>
	<li>Course Schedule:&nbsp;<a href="https://leetcode.com/problems/course-schedule">https://leetcode.com/problems/course-schedule</a></li>
</ul>
<p>&nbsp;</p>
<p>8.&nbsp;<strong>Divide and Conquer -&nbsp;</strong>Advanced</p>
<p>Break down a problem into smaller sub-problems, solve each sub-problem independently, and then combine the solutions to get the final result.</p>
<ul>
	<li>Maximum Subarray:&nbsp;<a href="https://leetcode.com/problems/maximum-subarray/">https://leetcode.com/problems/maximum-subarray/</a></li>
	<li>Pow(x, n):&nbsp;<a href="https://leetcode.com/problems/powx-n/">https://leetcode.com/problems/powx-n/</a></li>
	<li>Merge k Sorted Lists:&nbsp;<a href="https://leetcode.com/problems/merge-k-sorted-lists/">https://leetcode.com/problems/merge-k-sorted-lists/</a></li>
	<li>Count of Smaller Numbers After Self:&nbsp;<a href="https://leetcode.com/problems/count-of-smaller-numbers-after-self/">https://leetcode.com/problems/count-of-smaller-numbers-after-self/</a></li>
	<li>Find K Closest Elements:&nbsp;<a href="https://leetcode.com/problems/find-k-closest-elements/">https://leetcode.com/problems/find-k-closest-elements/</a></li>
	<li>Majority Element:&nbsp;<a href="https://leetcode.com/problems/majority-element/">https://leetcode.com/problems/majority-element/</a></li>
	<li>Unique Binary Search Trees:&nbsp;<a href="https://leetcode.com/problems/unique-binary-search-trees/">https://leetcode.com/problems/unique-binary-search-trees/</a></li>
	<li>Kth Largest Element in an Array:&nbsp;<a href="https://leetcode.com/problems/kth-largest-element-in-an-array/">https://leetcode.com/problems/kth-largest-element-in-an-array/</a></li>
	<li>Search for a Range:&nbsp;<a href="https://leetcode.com/problems/search-for-a-range/">https://leetcode.com/problems/search-for-a-range/</a></li>
	<li>Closest Binary Search Tree Value II:&nbsp;<a href="https://leetcode.com/problems/closest-binary-search-tree-value-ii/">https://leetcode.com/problems/closest-binary-search-tree-value-ii/</a></li>
</ul>
<p>&nbsp;</p>
<p>9.&nbsp;<strong>Dynamic Programming&nbsp;</strong>- Advanced</p>
<p>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.</p>
<p>Practice:</p>
<ul>
	<li>Climbing Stairs:&nbsp;<a href="https://leetcode.com/problems/climbing-stairs/">https://leetcode.com/problems/climbing-stairs/</a></li>
	<li>Best Time to Buy and Sell Stock:&nbsp;<a href="https://leetcode.com/problems/best-time-to-buy-and-sell-stock/">https://leetcode.com/problems/best-time-to-buy-and-sell-stock/</a></li>
	<li>Longest Increasing Subsequence:&nbsp;<a href="https://leetcode.com/problems/longest-increasing-subsequence/">https://leetcode.com/problems/longest-increasing-subsequence/</a></li>
	<li>Coin Change:&nbsp;<a href="https://leetcode.com/problems/coin-change/">https://leetcode.com/problems/coin-change/</a></li>
	<li>Maximum Subarray:&nbsp;<a href="https://leetcode.com/problems/maximum-subarray/">https://leetcode.com/problems/maximum-subarray/</a></li>
	<li>House Robber:&nbsp;<a href="https://leetcode.com/problems/house-robber/">https://leetcode.com/problems/house-robber/</a></li>
	<li>Unique Paths:&nbsp;<a href="https://leetcode.com/problems/unique-paths/">https://leetcode.com/problems/unique-paths/</a></li>
	<li>Decode Ways:&nbsp;<a href="https://leetcode.com/problems/decode-ways/">https://leetcode.com/problems/decode-ways/</a></li>
	<li>Maximum Product Subarray:&nbsp;<a href="https://leetcode.com/problems/maximum-product-subarray/">https://leetcode.com/problems/maximum-product-subarray/</a></li>
	<li>Palindrome Partitioning II:&nbsp;<a href="https://leetcode.com/problems/palindrome-partitioning-ii/">https://leetcode.com/problems/palindrome-partitioning-ii/</a></li>
</ul>

<p><strong>Notes:</strong></p>
<ol>
  <li>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</li>
	<li>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.</li>
	<li>The questions listed for practice might not be the most suitable ones, but you get the idea.</li>
	<li>This list serves as a starting point for those who don&#39;t know how to get started. Follow the path or just create your own, &quot;memento mori&quot;.</li>
</ol>

<p>Thanks for reading.</p>