Leetcode Patterns
Sorted input
- Use binary search for efficient searching or element lookup
- Use two pointer technique for problems related to pairs and seqments
Unsorted input
- Apply DP for questions related to counting ways or optimiszing values.
- Use backtracking for problems that ask for all possibilities or combinations (or if DP is not working)
- Use a Trie for prefix matching and string-building scenarios
- Use a hash map or set to find specific element 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 question asks for shortest path or fewest steps
- For binary trees, use DFS if problem involves exploring specific depths or levels
Linked List Input:
- Use techniques like slow and fast pointer or prev and dummy pointers to facilitate certain operations if you are unsure how to acheieve a specific outcome.