Ownership is important. Start your own blog! https://twitter.com/twitter/status/1848261668024807887/
~ updated at: 2024-10-21T12:30:03.083Z
Notes from leetcode’s editorial section on solving data structures and algorithm problems in general.
After solving a leetcode problem, found myself checking out an editorial section to see what other approaches I could’ve missed. What I found was unexpected.
A guide to identifying patterns and choosing approaches. This is something I could’ve used when I was a couple of years younger just starting out.
So this time before diving into the answer, let’s understand a few general patterns that you can use in your future journey:
Sorted Input:
Apply binary search for efficient element lookup. Use the two-pointer technique for problems involving pairs or segments. Unsorted Input:
Apply dynamic programming for questions related to counting ways or optimizing values. Use backtracking for problems that ask for all possibilities or combinations (this is also a suitable fallback if dynamic programming isn’t going to work). Use a Trie for prefix matching and string-building scenarios. Use a hash map or set to find specific elements quickly. Implement a monotonic stack or sliding window technique for managing elements while continuously finding maximum or minimum values. Input is a Graph or Tree:
Use DFS to explore all paths or when the question does not require finding the shortest path. Use BFS when the question asks for the shortest path or fewest steps. For binary trees, use DFS if the problem involves exploring specific depths or levels. Linked List Input:
Use techniques involving slow and fast pointers or “prev” and “dummy” pointers to facilitate certain operations if you are unsure how to achieve a specific outcome.
Note: There’s so much more to this pattern! We just wanted to give you a glimpse of what pattern recognition boils down to in its simplest form. Feel free to add your own flair and create a detailed chart!
Here’s a useful mindmap of techniques to use per problem type
~ updated at: 2024-10-21T03:50:20.239Z