Greedy Toolkit
Interview guide for Greedy Toolkit with intuition, dry run, C++ code, complexity, and practice problems
This article covers the intuition, workflow, dry run, C++ implementation, complexity, and interview usage for Greedy Toolkit.
1. Intuition
Greedy works when the best local move can be proven to lead toward the global optimum.
Key interview examples:
- activity selection
- interval scheduling
- fractional knapsack
- jump game
2. How It Works
- Sort or scan according to the greedy rule
- Take the best local option
- Prove that any other local choice cannot improve the final answer
3. Pattern Recognition
Think greedy when you see:
- intervals and non-overlap
- maximize count by sorting on finish time
- fractional item taking
- can we reach the end by maintaining best reach?
4. Dry Run Example
Input:
intervals = [[1, 3], [2, 4], [3, 5], [6, 8]]Step-by-step execution:
- Sort by end time
- Take
[1, 3] - Skip
[2, 4] - Take
[3, 5] - Take
[6, 8]
Final Output:
3 intervals5. Code (C++)
bool canJump(const vector<int>& nums) {
int farthest = 0;
for (int i = 0; i < static_cast<int>(nums.size()); i++) {
if (i > farthest) {
return false;
}
farthest = max(farthest, i + nums[i]);
}
return true;
}6. Complexity Analysis
- Often
O(n log n)for sorting-based greedy - Jump Game is
O(n)
7. When to Use
- interval picking
- continuous reach tracking
- value-density sorting
8. Common Mistakes
- using greedy where future tradeoffs matter and DP is required
- not proving why the local rule is safe
9. Variations / Extensions
- merge intervals
- erase overlap intervals
- Jump Game II
- meeting rooms
10. LeetCode Practice Problems
Easy
Medium
- https://magicsheet.dev/questions/jump-game/
- https://magicsheet.dev/questions/non-overlapping-intervals/
- https://magicsheet.dev/questions/partition-labels/
Hard
11. Key Takeaways
- Greedy needs a proof, not just a hunch
- Interval and reachability problems are common greedy territory
DP on Grids and Trees
Interview guide for DP on Grids and Trees with intuition, dry run, C++ code, complexity, and practice problems
Graphs and Union-Find
A focused graph interview guide covering DFS, cycle detection, topological sort, bipartite graphs, Dijkstra, Bellman-Ford, Union-Find, Kruskal, and Prim