1
Algorithm Design Techniques
Algorithm
| Technique | Idea | Examples |
|---|---|---|
| Brute force | Try every possibility | Linear search, all pairs |
| Divide and Conquer | Split into independent parts, solve, combine | Merge sort, quick sort, binary search |
| Greedy | Take the best choice at each step | Activity selection, Kruskal, Prim, Dijkstra, Huffman |
| Dynamic Programming | Overlapping subproblems, store answers | Fibonacci, knapsack, LCS, coin change |
| Backtracking | Build a solution, undo when it fails | N-Queens, Sudoku, subsets, permutations |
| Two pointers / Sliding window | Move indices over an array instead of nested loops | Pair sum, longest substring |
Q & A
- Greedy vs DP?
Greedy makes one local choice and never looks back, so it is fast but only correct for some problems. DP tries all choices of subproblems and reuses stored answers, so it is always correct when the problem has optimal substructure.