Module P-15·18 min read

When the locally optimal choice is also globally optimal — and the counterexamples that prove greedy isn't always safe.

JJS
Written by Jatin Jain Saraf · Senior Software Engineer

Module P-15 — Greedy Algorithms: Local vs Global Optimum

What this module covers: Dynamic programming (Modules P-11 through P-14) considers every relevant subproblem before committing to an answer. A greedy algorithm does the opposite: make the locally best choice available right now, commit to it permanently, and never look back. This module covers when that shortcut is provably correct, and — just as importantly — a clear counterexample showing when it silently produces the wrong answer.


The Greedy Strategy

A greedy algorithm makes one pass, choosing at each step whatever looks best right now, with no reconsideration and no backtracking (contrast this directly with Module P-7's backtracking, which explicitly does undo choices that don't work out). This is fast — usually a single O(n) or O(n log n) pass — but it's only correct when the problem has a specific property: the locally best choice at each step never prevents reaching the globally best overall solution. Not every problem has that property, which is the entire reason this module exists alongside dynamic programming rather than replacing it.


Jump Game: Can You Reach the End?

Given an array where each value is the maximum jump length from that position, determine if you can reach the last index starting from index 0.

typescript

The greedy insight: you never need to remember which specific path got you to the farthest reachable point — only how far that point currently is. At each index, greedily extend farthestReachable as far as the current position allows, and check whether the current index has actually been reached yet. This works because "can I reach index i" only depends on the single number farthestReachable, not on any specific sequence of jumps that produced it — there's no scenario where remembering more detail about the path taken would ever change the answer.


Gas Station: Find the Valid Starting Point

Given gas available at each station around a circular route and the cost to travel from each station to the next, find a starting station from which a complete circuit is possible (guaranteed unique if a solution exists and the total gas covers the total cost).

typescript

The greedy leap here is subtler than Jump Game: the moment currentTank goes negative at station i, every station between the current start candidate and i can be ruled out in one step, without individually testing each one — because none of them could have accumulated more surplus gas by station i than starting exactly at start did, and even that wasn't enough. Resetting start = i + 1 and continuing the same single pass, rather than restarting the whole scan from scratch for each candidate, is what keeps this O(n) instead of the O(n²) a "try every possible starting station independently" approach would cost.


Interval Scheduling: Maximum Non-Overlapping Intervals

Given a set of intervals, select the maximum number that don't overlap with each other — directly building on Module F-10's interval vocabulary.

typescript

Sign in to keep reading

The rest of this module is free — sign in with Google to unlock it and track your progress.

Sign in & Register

Discussion

0

Join the discussion

Loading comments...

© 2026 Jatin Jain Saraf (JJS). All rights reserved.