100% found this document useful (1 vote)
609 views5 pages

Leetcode Pattern Recognition Cheat Sheet

Bitflip's Leetcode Pattern Recognition Cheat Sheet provides strategies for solving coding problems based on input size and type. It outlines approaches for small, medium, and large inputs, and details techniques for various data structures such as trees, graphs, and arrays. Additionally, it highlights keywords associated with common algorithms like dynamic programming, two pointers, and greedy methods to aid in problem recognition during interviews.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
609 views5 pages

Leetcode Pattern Recognition Cheat Sheet

Bitflip's Leetcode Pattern Recognition Cheat Sheet provides strategies for solving coding problems based on input size and type. It outlines approaches for small, medium, and large inputs, and details techniques for various data structures such as trees, graphs, and arrays. Additionally, it highlights keywords associated with common algorithms like dynamic programming, two pointers, and greedy methods to aid in problem recognition during interviews.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Bitflip’s Leetcode Pattern Recognition Cheat Sheet

(Version 1)
Print this out, keep it by you, but not during your interview 😉
Step 1: Check The Constraints
Small n (≤ 20):
●​ Brute force approaches are viable
●​ Backtracking and recursion
●​ Exponential time complexity (2^n, n!) is acceptable
●​ Try all possible combinations/permutations

Medium n (10^3 to 10^6):


●​❌ No brute force solutions
●​ Linear time O(n) or O(n log n) solutions
●​ Greedy algorithms
●​ Two pointers technique
●​ Heap-based solutions
●​ Dynamic programming

Large n (≥ 10^7):
●​❌ No linear time solutions
●​ O(log n) solutions only
●​ Binary search
●​ Mathematical formulas
●​ O(1) constant time approaches

Step 2: Analyze Input Format


Tree/Binary Tree/BST:
●​ Tree traversal (DFS/BFS)
●​ DFS for: all paths, recursive exploration,
preorder/inorder/postorder
●​ BFS for: level-by-level, shortest path in
unweighted tree
●​ Consider: tree properties, parent-child
relationships

Graph (nodes + edges):


●​ BFS for shortest path
●​ DFS for connected components
●​ Union Find for "connected components" or "number of
groups"
●​ Topological sort for dependencies

2D Grid/Matrix:
●​ DFS/BFS for "islands" problems
●​ Union Find for connected regions
●​ Dynamic programming for path problems
●​ Consider: 4-directional or 8-directional movement

Sorted Array:
●​ Two pointers technique
●​ Binary search
●​ Greedy approach

String:
●​ Two pointers for palindromes
●​ Sliding window for substrings
●​ Trie for word problems
●​ Stack for parentheses/brackets

Linked List:
●​ Two pointers (fast/slow)
●​ Dummy node techniques
●​ Cycle detection
Step 3: Analyze Output Format
List of Lists (combinations, subsets, paths):
●​ Backtracking is almost always the answer
●​ Generate all possibilities
●​ Use recursion with choice/no-choice pattern

Single Number (max/min profit, cost, ways, jumps):


●​ Dynamic Programming for optimization
●​ Greedy for local optimal choices
●​ Mathematical approach for counting

Modified Array/String (in-place operations):


●​ Two Pointers for in-place modifications

Ordered List (sorted sequence, valid task order):


●​ Sorting with custom comparators
●​ Topological Sort for dependencies
●​ Heap for maintaining order

Step 4: Keyword Pattern Recognition


Dynamic Programming Keywords:
●​ "Number of ways"
●​ "Maximum/minimum" + "sum/profit/cost"
●​ "Can you reach"
●​ "Longest/shortest subsequence"
●​ "Optimal" or "best"

Two Pointers Keywords:


●​ "Palindrome"
●​ "Sorted array"
●​ "Target sum"
●​ "Remove duplicates"
Heap Keywords:
●​ "K largest" or "K smallest"
●​ "Top K elements"
●​ "Median"
●​ "Priority"

Stack Keywords:
●​ "Parentheses" or "brackets"
●​ "Valid expression"
●​ "Nested structure"
●​ "Undo operations"

Monotonic Stack Keywords:


●​ "Next greater element"
●​ "Next smaller element"

HashMap Keywords:
●​ "Count frequency"
●​ "Find duplicates"
●​ "Anagram"

Trie Keywords:
●​ "Word search"
●​ "Word prefixes"

Greedy Keywords:
●​ "Minimum operations"

Union Find Keywords:


●​ "Connected components"
●​ "Number of groups"

Binary Search Keywords:


●​ "Kth element"
●​ "Search in sorted"
●​ "Minimize maximum"
●​ "First/last occurrence"

Bit Manipulation:
●​ "XOR" operations
●​ "Single number" problems
●​ “Power of 2”

Math/Geometry:
●​ "Greatest/Least Common Denominator"
●​ "Prime numbers"
●​ "Angle calculations"
●​ “Coordinate”

Game Theory:
●​ "Optimal strategy"
●​ "Win/lose scenarios"
●​ "Minimax"

Sliding Window:
●​ "Substring" with conditions
●​ "Subarray" with fixed/variable size
●​ "Maximum/minimum window"
●​ "Contains all"

Common questions

Powered by AI

In problems with small n (≤ 20), brute force approaches are viable due to the manageable number of combinations or permutations possible. Backtracking and recursion are appropriate as exponential time complexity, such as 2^n or n!, is acceptable under these constraints .

Islands problems in 2D grids or matrices are ideally tackled using DFS or BFS, which explore each land cell and connect them with adjacent ones to form a whole island. Union Find can also be employed to efficiently manage connected regions by tracking and merging parent nodes without exploring each cell individually .

Union Find is instrumental in managing connected components by efficiently handling union and find operations that signify group connections. It's particularly useful in dynamic connectivity to manage disjoint sets without repeatedly traversing each element, enabling fast checks and updates to the subset mappings, which is crucial in large graphs for determining component territories .

Sliding window techniques are preferred for substring or subarray problems because they provide a streamlined way to handle fixed or variable section lengths within a dataset. This method efficiently tracks elements that satisfy given conditions without redundant recalculations, thus reducing computational complexity compared to restarting checks after each element shift .

For medium n (10^3 to 10^6), greedy algorithms are suitable because they often provide linear O(n) or nearly linear O(n log n) solutions that efficiently handle the allowed input size. They make the locally optimal choice at each step with the hope of finding a global optimum .

Tree problems typically involve traversals like DFS for all paths or BFS for shortest unweighted path, focusing on parent-child relationships and predefined traversal orders like preorder, inorder, or postorder. Graph problems, on the other hand, use BFS for shortest path, DFS for connected components, and additional techniques like Union Find and topological sorting to handle more generalized structures like dependencies or disjoint sets .

Bit manipulation is highly efficient for large datasets due to its constant time operations. Problems like finding a 'single number' or checking 'power of 2' solve complex bitwise operations using limited computational resources. Given constraints that preclude linear time solutions for large n, bit manipulation's O(1) operations effectively address performance requirements .

The two pointers technique is versatile in solving problems involving strings and arrays. For strings, it can be applied to check palindromes by comparing characters from both ends towards the center. In arrays, it can efficiently solve sorted array problems by moving one pointer from the start and one from the end to meet specific conditions, such as computing target sums or removing duplicates .

While backtracking is largely infeasible for medium n (10^3 to 10^6) due to its generally exponential nature, it remains ideal for specific scenarios that involve combinatorial generation with constraints where brute force is necessary to explore possibilities. However, large n (≥ 10^7) requires more efficient algorithms, like O(log n) or constant time solutions, because the computational load of backtracking becomes prohibitive .

Dynamic programming techniques efficiently solve 'number of ways' problems by breaking them down into smaller, manageable subproblems and storing results to avoid redundant computations. This approach allows the solution to explore multiple paths or combinations, ideal for counting variations like subsets, paths, or sequences .

You might also like