█
LastWrite
  • > Curriculum
  • > Pricing
  • > For Educators
  • > About
  • > Contact
Log InGet Started

Questions, concerns, bug reports, or suggestions? We read every message, write to us at [email protected].

More ways to reach us →
LastWrite

Structured computer science lessons for aspiring developers and security professionals.

[email protected]

(201) 785-7951

Mon–Fri, 9 AM–5 PM EST

Learn

  • Curriculum
  • Pricing

Company

  • About
  • For Educators & Schools
  • Contact Us

Legal

  • Terms of Service
  • Privacy Policy
© 2026 LastWrite. All rights reserved.
Curriculum/Computer Science Fundamentals/Algorithms/Greedy Algorithms
40 minIntermediate

Greedy Algorithms

After this lesson, you will be able to: Understand the greedy choice property, recognize when greedy works and when it fails, and apply it to classic problems like activity selection and Dijkstra's algorithm.

A greedy algorithm makes the choice that looks best right now and never reconsiders. When the problem has the right structure, greedy is simpler and faster than dynamic programming. The hard part is knowing when greedy is actually correct, because it often looks right but gives wrong answers.

Prerequisites:Dynamic Programming

The greedy choice property

Greedy works when a locally optimal choice leads to a globally optimal solution. At each step you grab the best immediate option and commit, never backtracking. Compared to DP, which explores subproblems and combines them, greedy just charges forward. When it works it is usually O(n log n) or better. The catch: greedy is correct only when the problem provably has the greedy choice property, and proving that is the real skill.

Activity selection: greedy works

Schedule the most non-overlapping meetings. Pick by earliest finish time.

python
def max_activities(intervals):
# Greedy: always take the activity that finishes earliest.
intervals.sort(key=lambda x: x[1]) # sort by finish time
count, last_end = 0, float('-inf')
for start, end in intervals:
if start >= last_end: # no overlap with the last chosen
count += 1
last_end = end
return count
# [(1,3),(2,4),(3,5),(0,6)] -> 2 (e.g., (1,3) then (3,5))
# Why earliest finish? It leaves the most room for future activities.

💡 When greedy fails: coin change

With US coins (1, 5, 10, 25), grabbing the largest coin each time gives the fewest coins. So greedy seems to work. But with coins like (1, 3, 4) and amount 6, greedy takes 4 + 1 + 1 = 3 coins, while the optimal is 3 + 3 = 2 coins. Same problem, different coin set, and greedy is now wrong. This is exactly why coin change is solved with DP in the previous lesson. The lesson: greedy correctness depends on the specific problem, and you must verify it, not assume it.

Greedy in the wild

Dijkstra's shortest-path algorithm is greedy: it always expands the closest unvisited node next, using a min-heap. Huffman coding (data compression) greedily merges the two least-frequent symbols. Interval scheduling, fractional knapsack, and minimum spanning trees (Prim's and Kruskal's) are all greedy. These are proven correct; that is why they are trusted. You will see Dijkstra again in the graph algorithms lesson.

How to decide if greedy is safe

Do not trust greedy by default. Test it.

  1. 1

    1. Formulate the greedy rule (what is the 'best immediate choice'?).

  2. 2

    2. Try to construct a counterexample where the greedy choice leads to a worse overall result.

  3. 3

    3. If you find one, greedy is wrong; reach for DP or another approach.

  4. 4

    4. If you cannot, sketch an exchange argument: show any optimal solution can be transformed to start with the greedy choice without getting worse.

  5. 5

    5. In an interview, say explicitly 'greedy works here because...' or 'greedy fails here, so I will use DP.'

Common mistakes only experienced devs catch

Assuming greedy works because it feels right and skipping the counterexample check (the single most common greedy error). Confusing greedy with DP (greedy commits and never reconsiders; DP explores). Sorting by the wrong key (activity selection needs earliest finish, not earliest start or shortest duration). Applying greedy coin change to arbitrary coin systems. Not stating your justification, which interviewers specifically probe for.

Sign in and purchase access to unlock this lesson.

Sign in to purchase
←Dynamic Programming
Back to Algorithms
Graph Algorithms→