Greedy choices and when they are optimal
Quick answer
A greedy algorithm commits to a locally preferred choice. It is optimal only when the problem admits a proof that these choices preserve an optimal solution.
Working examples
Run as an independent script in Node.js v22.16.0. ByteMind editor support has not been verified.
const intervals=[[0,5],[1,2],[2,3],[3,4]];const selected=[];let end=-Infinity;for(const [s,e] of intervals.toSorted((a,b)=>a[1]-b[1])){ if(s>=end){selected.push([s,e]);end=e;}}console.log(JSON.stringify(selected));Expected console output:
[[1,2],[2,3],[3,4]]Early finishes leave room for three compatible intervals.
Run as an independent script in Node.js v22.16.0. ByteMind editor support has not been verified.
let remaining=6;const chosen=[];for(const coin of [4,3,1])while(remaining>=coin){chosen.push(coin);remaining-=coin;}console.log(JSON.stringify(chosen),chosen.length);Expected console output:
[4,1,1] 3Largest-first uses three coins; two threes would be better.
How it works and cost model
For selecting the largest number of non-overlapping positive-length half-open intervals, sort by end time, select the earliest finishing compatible interval, then repeat with start >= previous end. An exchange argument: replacing the first interval of any optimal schedule with the earliest finishing one leaves at least as much room for the remaining schedule. This proves the rule for unweighted interval count. The scan is linear after sorting; under an O(n log n) comparison sort the total is O(n log n). Weighted intervals have a different objective. For coin denominations [1,3,4] and amount 6, largest-first picks 4+1+1, but 3+3 uses fewer coins.
Common mistakes
Do not generalize a greedy rule just because it works on a few examples. State the objective and assumptions, and look for counterexamples.
Practice
Select the maximum number of compatible intervals from [[0,5],[1,2],[2,3],[3,4]]. Explain why maximizing total duration is a different problem.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Greedy choices and when they are optimal — Knowledge Check
Reason about the algorithm or data structure, its invariants, trade-offs and deterministic outputs.
Which objective does earliest-finish interval selection optimize under the stated assumptions?
What does a greedy exchange argument establish?
What disproves largest-coin-first optimality for denominations 1,3,4 and amount 6?
What is the exact console output in the Greedy interval scheduling example 1?
const intervals=[[0,5],[1,2],[2,3],[3,4]];const selected=[];let end=-Infinity;for(const [s,e] of intervals.toSorted((a,b)=>a[1]-b[1])){ if(s>=end){selected.push([s,e]);end=e;}}console.log(JSON.stringify(selected));What is the exact console output in the Greedy interval scheduling example 2?
let remaining=6;const chosen=[];for(const coin of [4,3,1])while(remaining>=coin){chosen.push(coin);remaining-=coin;}console.log(JSON.stringify(chosen),chosen.length);