
Greedy Algorithms are the “Smart Shortcuts” of Computer Science.
While Dynamic Programming meticulously calculates every possibility ($O(N^2)$), a Greedy Algorithm makes the best local choice at every step ($O(N \log N)$ or $O(N)$) and hopes it leads to the global optimum.
For top-tier interviews, you must know when to be greedy and when to stop. Using Greedy on a DP problem is an instant rejection. This guide covers the 8 Core Patterns that solve 95% of optimization problems.
Part 1: The “Interval Scheduling” Pattern (Time Management)
The art of fitting the most tasks into a limited time.
1. Activity Selection / N-Meetings in One Room
- The Scenario: You have
Nmeetings with Start and End times. Select the max number of non-overlapping meetings. - The “Professor’s Rule”: Sort by End Time.
- Why? Ending a meeting early leaves more room for the next one.
- Algorithm: Pick the first meeting. For the rest, if
Start[i] >= End[prev], pick it.
2. Minimum Platforms (Railway Problem)
- The Scenario: Trains arrive and depart. What is the minimum number of platforms needed so no train waits?
- The “Professor’s Rule”: Sort Arrivals and Departures separately.
- Logic: Walk through time.
- Train Arrives to Need Platform (+1).
- Train Departs to Free Platform (-1).
- Track the Maximum value ever reached.
Part 2: The “Knapsack” Pattern (Value Density)
Getting the most bang for your buck.
3. Fractional Knapsack
- The Scenario: You can take parts of an item (e.g., Gold dust).
- The “Professor’s Rule”: Sort by Value Per Unit Weight (Ratio).
- Take the “densest” items first.
- Warning: If items cannot be broken (0/1 Knapsack), Greedy fails. You must use DP.
4. Job Sequencing with Deadlines
- The Scenario: Jobs have
ProfitandDeadline. - The “Professor’s Rule”: Sort by Profit (Descending).
- Try to do the highest paying job on the latest possible day (closest to deadline).
- This saves early days for other jobs.
Part 3: The “Array Traversal” Pattern (Linear Greedy)
Making decisions while walking through an array.
5. Jump Game (I & II)
- The Scenario: Can you reach the last index? (Or min jumps to reach it).
- The “Professor’s Rule”: Track Max Reach.
- Iterate through array
i. - Update
MaxReach = max(MaxReach, i + nums[i]). - If
i > MaxReach, you are stuck.
- Iterate through array
6. Gas Station (Circular Tour)
- The Scenario: Can you travel around a circular route of gas stations?
- The “Professor’s Rule”: The Total Surplus logic.
- If
Total Gas < Total Cost, it’s impossible. - Else, iterate. If
current_tank < 0, reset start point toi + 1.
- If
Part 4: The “Structural” Pattern (Advanced)
Building efficient structures or encodings.
7. Huffman Coding (Compression)
- The Scenario: Lossless data compression (used in ZIP files).
- The “Professor’s Rule”: Build a tree from the bottom up.
- Use a Min-Heap.
- Extract two smallest frequency nodes, combine them, push back.
- Repeat until one node remains.
8. Minimum Spanning Tree (Prim’s & Kruskal’s)
- The Scenario: Connect all cities with the cheapest wire cost.
- The “Professor’s Rule”:
- Kruskal’s: Sort all edges. Add edge if it doesn’t form a cycle (Greedy!).
- Prim’s: Start at a node. Always pick the cheapest neighbor (Greedy!).
The “Professor’s Trap”: When Greedy FAILS
Add this section to save your students from rejection.
You cannot use Greedy for:
- 0/1 Knapsack: (Items are indivisible).
- Longest Path in Graph: (NP-Hard).
- Coin Change (General Case): If coins are
{1, 3, 4}and you need6.- Greedy (Descending) takes
4, 1, 1(3 coins). - Optimal is
3, 3(2 coins). - Fix: Use DP for these.
- Greedy (Descending) takes
The Professor’s Cheat Code Sheet
Stick this table at the bottom of your post.
| Problem Type | The Greedy “Trigger” (Sort By…) | Complexity |
| Max Activities | Sort by End Time (Asc) | O(N log N) |
| Fractional Knapsack | Sort by Value/Weight Ratio (Desc) | O(N log N) |
| Job Sequencing | Sort by Profit (Desc) + Use Sets | O(N^2) or O(N log N) |
| Min Platforms | Sort Arrivals & Departures Separately | O(N log N) |
| Boats to Save People | Sort + Two Pointers (Lightest + Heaviest) | O(N log N) |
| Jump Game / Gas Station | Linear Scan (Track Max Reach/Surplus) | O(N) |
| Huffman Coding | Min-Heap (Frequency) | O(N log N) |
| Candy Distribution | Two Pass (Left-to-Right, Right-to-Left) | O(N) |
The Complete Greedy Algorithms Roadmap: 8 Essential Patterns from Beginner to Expert (2026 Guide)
Greedy Algorithms for Interview, Activity Selection Problem Greedy vs Dynamic Programming Huffman Coding Explained
YouTube Channels:
Trendy VS Vlogs
VS Coding Academy
Join Our WhatsApp Channel for the latest job opportunities and updates:
VS_CODING_ACADEMY WhatsApp Channel
Join Our Telegram Channel for the latest job opportunities and updates: https://t.me/vscodingacademy
Open our site in Telegram Bot: https://t.me/vscodingacademy_bot
For DSA Guide: https://vscodingacademy.com/category/dsa-guide/