What is Greedy?
Greedy algorithms make the locally optimal choice at each step, hoping to find a global optimum. They work when local choices lead to global solutions (greedy choice property + optimal substructure).Quick Recognition: Optimization problems where taking the “best” option at each step leads to the global best. Look for keywords: “minimum number of”, “maximum profit”, “optimal selection”.
Pattern Recognition Checklist
Use Greedy When
- Interval scheduling/merging
- Minimum coins (certain denominations)
- Jump game style problems
- Huffman encoding
- Activity selection
- Fractional knapsack
Don't Use When
- 0/1 Knapsack (need DP)
- Coin change (arbitrary denominations)
- Need to try all possibilities
- Local optimal may miss global
- Problem has overlapping subproblems
Greedy vs DP: How to Choose
| Characteristic | Greedy | DP |
|---|---|---|
| Makes irrevocable choices | Yes | No, tries all |
| Optimal substructure | Required | Required |
| Overlapping subproblems | Not handled | Handled via caching |
| Time complexity | Usually O(n log n) | Usually O(n^2) or more |
| Proof required | Yes (exchange argument) | No, exhaustive |
Common Greedy Patterns
| Pattern | Greedy Choice | Example |
|---|---|---|
| Interval Scheduling | Sort by end time | Non-overlapping intervals |
| Interval Partitioning | Sort by start time | Meeting rooms |
| Fractional Knapsack | Best value/weight ratio | Maximize value |
| Job Scheduling | Sort by deadline | Minimize late jobs |
| Huffman Coding | Merge least frequent | Optimal prefix codes |
When to Use
Scheduling
Task scheduling, meeting rooms, intervals
Selection
Choosing items to maximize/minimize value
Graph Problems
MST (Kruskal, Prim), shortest paths (Dijkstra)
String/Array
Partition labels, jump game
Pattern Variations
1. Interval Scheduling
2. Activity Selection (Meeting Rooms)
3. Jump Game
4. Gas Station
5. Partition Labels
Classic Problems
1. Interval Scheduling
1. Interval Scheduling
Pattern: Sort by end time, greedily select non-overlappingKey: Earliest end time leaves most room for future
2. Jump Game
2. Jump Game
Pattern: Track maximum reachable indexKey: At each position, update farthest reach
3. Task Scheduler
3. Task Scheduler
Pattern: Process most frequent tasks firstKey: Idle time depends on most frequent task
4. Candy Distribution
4. Candy Distribution
Pattern: Two passes (left to right, right to left)Key: Satisfy constraints from both directions
Common Mistakes
How to Prove Greedy Works
1
Greedy Stays Ahead
Show that after each step, greedy is at least as good as optimal
2
Exchange Argument
Show that swapping a choice in optimal with greedy’s choice doesn’t hurt
3
Structural Induction
Prove base case + if true for size k, true for size k+1
Interview Problems by Company
- Easy
- Medium
- Hard
| Problem | Company | Key Concept |
|---|---|---|
| Assign Cookies | Amazon | Sort both arrays |
| Lemonade Change | Track denominations | |
| Best Time to Buy Stock | All FAANG | Track minimum so far |
Interview Tips
How to Explain Your Approach
How to Explain Your Approach
Script for interviews:
- “This looks like a greedy problem because local choices lead to global optimum.”
- “My greedy strategy is: [describe the choice]”
- “This works because [brief justification]”
- “I’ll sort by [criteria] first, then iterate greedily.”
- “Time is O(n log n) for sorting, O(n) for the greedy pass.”
When Interviewer Says...
When Interviewer Says...
| Interviewer Says | You Should Think |
|---|---|
| ”Minimum operations” | Greedy or DP |
| ”Schedule tasks/meetings” | Interval scheduling |
| ”Maximum profit/value” | Greedy or DP |
| ”Prove it’s optimal” | Exchange argument |
| ”What if greedy fails?” | Consider DP |
Greedy Template
Greedy Template
Practice Problems
| Problem | Difficulty | Link |
|---|---|---|
| Jump Game | Medium | LeetCode 55 |
| Gas Station | Medium | LeetCode 134 |
| Partition Labels | Medium | LeetCode 763 |
| Task Scheduler | Medium | LeetCode 621 |
| Non-overlapping Intervals | Medium | LeetCode 435 |
Practice Roadmap
1
Day 1: Basic Greedy
- Solve: Assign Cookies, Lemonade Change
- Focus: Simple greedy choices
2
Day 2: Interval Problems
- Solve: Non-overlapping Intervals, Meeting Rooms
- Focus: Sort by end time pattern
3
Day 3: Array Greedy
- Solve: Jump Game I and II, Gas Station
- Focus: Tracking reachability
4
Day 4: Complex Greedy
- Solve: Task Scheduler, Candy
- Focus: Multi-pass greedy