Many beginners treat LeetCode as a “guessing game.” They stare at the problem hoping for inspiration. That leads to frustration.
👉 Instead, treat every problem as a puzzle with a known set of tools. Your goal is to identify which tool/pattern applies, not to reinvent the wheel.
Many students try to guess solutions blindly. Instead, approach each problem as a puzzle with known patterns. The key is to:
| Pattern | How to Recognize | How to Approach | Example Problem | Dificulty | Data structure used |
|---|---|---|---|---|---|
| HashMap / Set | Problem involves lookups, duplicates, counting frequencies, matching pairs | Use Map/Set to store seen values or counts; check in O(1). Often loop once. |
Two Sum → Use a Map to store target - num and find complement |
easy | HashMap,Se.t |
| Stack | Problem involves balanced symbols, nested structures, undo/redo, or matching opening/closing | Push opening elements; pop and check when closing appears; stack must be empty at end | Valid Parentheses → Push (, {, [, pop on closing |
easy-medium | Stack/Array. |
| Two Pointers | Problem involves sorted arrays, comparing ends, removing duplicates, partitioning | Initialize left and right pointers; move inward depending on condition |
Container With Most Water → Start at both ends, move the smaller side | medium | Array,String. |
| Sliding Window | Problem involves substrings/subarrays, longest/shortest segment, fixed or variable length | Maintain a window with start and end indexes; expand and shrink dynamically |
Longest Substring Without Repeating Characters → Expand end, shrink start when duplicate | medium | Array,String,Set,Map. |
| Binary Search | Problem involves sorted data, finding target, optimization (min/max) | Apply binary search over sorted array or answer space; halve search each time | Search Insert Position → Find target index in sorted array | easy-medium | Sorted Array, Number, range. |
| Sorting + Greedy | Problem involves ordering, intervals, minimizing/maximizing | Sort input first; then make greedy decisions per step | Merge Intervals → Sort by start, merge overlapping | medium | Array |
| Recursion / DFS / BFS | Problem involves trees, graphs, nested structures, path finding | Use recursion or stack/queue to traverse; DFS for depth, BFS for breadth | Binary Tree Inorder Traversal → Recursive DFS | medium-hard | Tree, Graph, Stack, Queue. |
| Dynamic Programming (DP) | Problem involves optimization, overlapping subproblems, counting combinations | Identify subproblems → write recurrence → bottom-up or top-down with memo | Climbing Stairs → DP: f(n) = f(n-1)+f(n-2) | medium-hard | Array, Matrix, hasmap |
| Backtracking | Problem involves generating combinations, permutations, exploring all possibilities | Build solution incrementally, backtrack when invalid | Combination Sum → DFS building combinations | medium-hard | array, recursion, tree |
| Union-Find / Disjoint Set | Problem involves connectivity, components, networks | Use union-find structure to track connected components | Number of Provinces → Union-Find to group cities | medium | Disjoint Set Structure (Array/Map) |
| Graph Algorithms (Dijkstra, Topo Sort) | Problem involves paths, dependencies, directed edges | Choose algorithm depending on weighted/unweighted graph | Course Schedule → Topological Sort to detect cycles | medium-hard | Graph (Adjacency List/Matrix), Queue, Heap |
| Math / Bit Manipulation | Problem involves parity, XOR, counting bits, mathematical properties | Identify formula, parity, or bit operation patterns | Single Number → XOR all elements to find unique | easy-medium | Integers, Bitwise Operations |
When analyzing a new problem: