PATTERN 9: Greedy Algorithms

D

Qubits of DPK

April 11, 2026

Core DSA
Pattern 9 is where interviewers test judgment, not coding speed.
Many candidates use greedy.
Very few can justify greedy.

Core Idea (lock this)

“Make a locally optimal choice that leads to a globally optimal solution.”
Greedy works only when a proof exists.
Otherwise, it lies.

How to Recognize This Pattern (CRITICAL)

Think Greedy when:
  • You’re selecting or scheduling
  • You see:
  • Sorting suddenly feels necessary
  • DP feels like overkill
If local choice can be proven safe → greedy.

30 MUST-DO INTERVIEW PROBLEMS

(10 Easy → 10 Medium → 10 Hard)

EASY (1–10): Greedy Intuition Builders

Goal: build trust in greedy decisions.
  1. #
    Assign Cookies – LC 455
  2. #
    Lemonade Change – LC 860
  3. #
    Can Place Flowers – LC 605
  4. #
    Split a String in Balanced Strings – LC 1221
  5. #
    Minimum Cost to Move Chips – LC 1217
  6. #
    Minimum Difference Between Highest and Lowest of K Scores – LC 1984
  7. #
    Boats to Save People – LC 881
  8. #
    Minimum Operations to Make Array Increasing – LC 1827
  9. #
    Jump Game – LC 55
  10. #
    Maximum Units on a Truck – LC 1710
After these:
You should feel when greedy "just works".

️ MEDIUM (11–20): Greedy + Proof

English explanation must include why this choice is safe.
  1. #
    Jump Game II – LC 45
  2. #
    Gas Station – LC 134
  3. #
    Partition Labels – LC 763
  4. #
    Remove K Digits – LC 402
  5. #
    Candy – LC 135
  6. #
    Course Schedule III – LC 630
  7. #
    Reorganize String – LC 767
  8. #
    Hand of Straights – LC 846
  9. #
    Task Scheduler – LC 621
  10. #
    Minimum Number of Taps to Water Garden – LC 1326
Interviewers listen for:
"Why not another choice?"

HARD (21–30): Greedy with Constraints

These separate thinking from memorizing.
  1. #
    Job Sequencing with Deadlines – GFG
  2. #
    Minimum Number of Refueling Stops – LC 871
  3. #
    Maximum Performance of a Team – LC 1383
  4. #
    Non-overlapping Intervals – LC 435
  5. #
    Minimum Number of Arrows to Burst Balloons – LC 452
  6. #
    IPO – LC 502
  7. #
    Car Fleet – LC 853
  8. #
    Advantage Shuffle – LC 870
  9. #
    Minimum Initial Energy to Finish Tasks – LC 1665
  10. #
    Smallest Range Covering K Lists – LC 632
These require:
  • Sorting + priority queue
  • Strong justification
  • Calm reasoning

THE ONE SENTENCE THAT WINS GREEDY INTERVIEWS

Practice saying:
“This greedy choice is optimal because it leaves the most room for future decisions.”
That line signals maturity.

Common Interview Killers

  • Using greedy when DP is required
  • No proof / intuition explanation
  • Wrong sorting criteria
  • Confusing greedy with two pointers

INTERVIEW REVISION CARD — Pattern 9

1️⃣ Pattern in One Line

"Make the locally optimal choice at each step — but only when you can prove it leads to the global optimum."

2️⃣ Recognize It When...

  • Selecting, scheduling, or assigning resources
  • Trigger words: "minimum number", "maximum profit", "earliest", "fewest steps"
  • Sorting suddenly feels necessary before decisions
  • DP feels like overkill — no overlapping subproblems

3️⃣ Don't Confuse With...

  • DP (P13) → DP explores all choices; greedy commits to one without reconsidering
  • Sorting (P6) → sorting is often the setup for greedy, not the solution itself
  • Backtracking (P10) → backtracking tries all; greedy never undoes a choice

4️⃣ Approach in 3 Steps

  1. #
    Prove the greedy choice is safe — "this choice leaves maximum room for future decisions"
  2. #
    Sort or use a priority queue to always access the best candidate
  3. #
    Make the choice, update state, repeat

5️⃣ Invariant to Maintain

"At every step, the greedy choice made so far is part of some optimal solution."

6️⃣ Complexity to Justify

  • Time: O(N log N) with sort, O(N log N) with heap
  • Space: O(1) or O(N) depending on structure
  • Why: No backtracking, no revisiting decisions

7️⃣ Edge Cases to Always Check

  • Single element — greedy trivially correct
  • All elements same
  • Already optimal input — greedy should handle gracefully
  • Ties in priority — comparator must be deterministic
  • Empty input

8️⃣ One Line to Say in Interview

"This greedy choice is optimal because it leaves the most room for future decisions — I can prove no other choice leads to a better outcome."