You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
๐ The SIFT Method โ Your 10-Second Pattern Finder
Before writing a single line of code, say this out loud:
"My input is a ____. I need to find ____. The constraint is ____. Therefore, the pattern is ____."
State the problem in your own words
Identify the input type and constraints
Find the pattern using the trigger words below
Template: Write the skeleton code from memory
๐บ๏ธ The Master Decision Flowchart
Read the problem โ Ask Question 1 โ Narrow โ Ask Question 2 โ Solve.
flowchart TD
START["๐ Read the Problem"] --> Q1{"โ What is the INPUT?"}
Q1 -->|"Sorted Array"| SA["๐ Binary Search\n๐ Two Pointers"]
Q1 -->|"Unsorted Array / String"| UA{"โ What do you need?"}
Q1 -->|"Linked List"| LL["๐ข Fast & Slow\n๐ In-place Reversal\n๐ Clone"]
Q1 -->|"Tree"| TR{"โ Level-by-level\nor Path-by-path?"}
Q1 -->|"Graph / Matrix"| GR{"โ What to find?"}
Q1 -->|"Intervals"| IV["๐ Merge Intervals\n๐ค Greedy"]
Q1 -->|"Numbers / Bits"| BT["โ Bitwise XOR"]
UA -->|"Subarray (contiguous)"| SUB["๐ช Sliding Window\nโ Prefix Sum"]
UA -->|"Pair / Triplet"| TP["๐ Two Pointers (sort first!)"]
UA -->|"Missing / Duplicate in [1,N]"| CS["๐ข Cyclic Sort"]
UA -->|"Next Greater / Smaller"| MS["๐ Monotonic Stack"]
UA -->|"All combos / permutations"| BK["๐ Backtracking\n๐ฆ Subsets"]
UA -->|"Kth largest / smallest"| TK["๐ Top K / Heap"]
UA -->|"Count ways / Optimize"| DP["๐งฎ Dynamic Programming"]
UA -->|"Lookup / Frequency"| HM["#๏ธโฃ Hash Map"]
TR -->|"Level-by-level"| BFS["๐ Tree BFS (Queue)"]
TR -->|"Path / Depth"| DFS["๐ฒ Tree DFS (Recursion)"]
GR -->|"Connected groups"| UF["๐ Union Find"]
GR -->|"Task ordering"| TS["๐ Topological Sort"]
GR -->|"Islands / Flood fill"| IS["๐๏ธ Island Traversal"]
GR -->|"Critical edges"| AP["๐ Articulation Points"]
style START fill:#1a1a2e,color:#fff,stroke:#e94560
style Q1 fill:#16213e,color:#fff,stroke:#0f3460
style SA fill:#0f3460,color:#fff
style LL fill:#0f3460,color:#fff
style IV fill:#0f3460,color:#fff
style BT fill:#0f3460,color:#fff
style SUB fill:#533483,color:#fff
style TP fill:#533483,color:#fff
style CS fill:#533483,color:#fff
style MS fill:#533483,color:#fff
style BK fill:#533483,color:#fff
style TK fill:#533483,color:#fff
style DP fill:#533483,color:#fff
style HM fill:#533483,color:#fff
style BFS fill:#2b2d42,color:#fff
style DFS fill:#2b2d42,color:#fff
style UF fill:#e94560,color:#fff
style TS fill:#e94560,color:#fff
style IS fill:#e94560,color:#fff
style AP fill:#e94560,color:#fff
Loading
โก Engineering Triage: When to Go "Kill Mode" vs "Pull the Plug"
"Amateurs guess algorithms. Professionals inspect constraints, classify the complexity class, and decide instantly whether to optimize to the metal or switch to an approximation."
Input Constraint ($N$)
Allowed Complexity
Tactical Mindset
Strategic Move
$N \le 10$
$O(N!) \text{ or } O(N^6)$
๐ Permutations / Backtracking
Brute-force DFS, state permutations (itertools.permutations)
$N \le 20$
$O(2^N \cdot N)$
๐งฉ Bitmask DP / Subsets
Meet-in-the-Middle, State compression DP, Subsets
$N \le 100$
$O(N^4)$
๐งฎ 2D/3D DP / Floyd-Warshall
High-dimensional Dynamic Programming
$N \le 500$
$O(N^3)$
๐ Matrix Multiplication / Floyd
Triple nested loops, all-pairs shortest paths
$N \le 5,000$
$O(N^2)$
๐ช Nested Loops / 2D DP
Quadratic DP, nested scans, Kadane variants
$N \le 10^5$
$O(N \log N)$
๐ KILL MODE: Sort / Heap / Tree
Sorting, Divide-and-Conquer, Segment Trees, Heaps
$N \le 10^6$
$O(N)$
โก KILL MODE: Linear Scan
Sliding Window, Hash Map, Two Pointers, Monotonic Stack
$N \ge 10^9$
$O(\log N) \text{ or } O(1)$
๐ฏ Math / Binary Search
Binary Search over answer, Bitwise tricks, Closed-form math
๐จ The Decision Rule:
Problem is in $P$ (or $N$ fits the table)$\longrightarrow$ ๐ฅ GO KILL MODE: Derive the exact optimal pattern, eliminate allocations, exploit CPU cache locality, and nail the proof.
Problem is $NP$-Hard and $N > 30$$\longrightarrow$ ๐ PULL THE PLUG: Stop searching for polynomial exact algorithms ($P \ne NP$). Switch to Approximations (Ch 7), Randomization (Ch 8), or Metaheuristics (Ch 9).
Don't memorize 43 patterns. Memorize 7 families. Each family shares the same "feeling".
mindmap
root((๐ง DSA Patterns))
๐ Runners
Two Pointers
Fast & Slow
Sliding Window
๐ฆ Sorters
Merge Intervals
Cyclic Sort
Linear Sort
๐ฒ Explorers
Tree BFS
Tree DFS
Island Traversal
Backtracking
๐ Rankers
Top K Elements
Two Heaps
K-way Merge
๐ Searchers
Binary Search
Trie
Hash Maps
๐งฎ Counters
Prefix Sum
DP: Knapsack
DP: Fibonacci
DP: Palindrome
Segment Tree
BIT
๐ Connectors
Union Find
Topological Sort
Clone
Serialize
Loading
Family
Core Idea
When Your Gut Says...
๐ Runners
Two things moving through data
"I need to scan with a window or shrink from both sides"
๐ฆ Sorters
Put things in the right place
"Things overlap or belong at specific positions"
๐ฒ Explorers
Visit nodes, explore paths
"I need to traverse a tree, graph, or grid"
๐ Rankers
Find the best K things
"I need the top/bottom K or a running median"
๐ Searchers
Find something fast
"I need to look up, match, or search efficiently"
๐งฎ Counters
Count or accumulate values
"I need to count ways, sums, or optimize a value"
๐ Connectors
Manage relationships
"Things are connected, ordered, or need to be copied"
๐ฏ Trigger Word โ Pattern (Instant Lookup)
Scan the problem statement. Spot ONE of these phrases. You have your pattern.
๐ Runner Patterns
๐ When You See...
๐ก Instantly Think...
๐ท๏ธ Pattern
"pair", "triplet", "two sum", "sorted array"
Shrink from both ends
Two Pointers
"cycle in linked list", "middle of list", "happy number"
# Step 1: WHAT does dp[i] mean?# Step 2: HOW does dp[i] relate to dp[i-1]?# Step 3: WHAT is dp[0]?dp= [BASE] * (n+1)
foriinrange(1, n+1):
dp[i] =TRANSITION(dp[i-1], dp[i-2], ...)