An approach to finding global optimum solutions by finding local optimum solutions.
A greedy choice property
means that choosing the local optimal choice can’t hurt the final solution.
When to use
- Need to make a sequence of local choices that lead to a global optimum.
- Hiring schedule
- Interval scheduling / Activity Selection
- Choose the lecture that ends earliest
- Remove all that overlap
- repeat → maximal number of non-overlapping intervals.
- Minimum coins for change with canonical coin systems
- Always pick the largest coin ≤ remaining amount.
- Jump Game
- At each position, greedily pick the farthest reachable index.
Mechanism
Sketch
For the activity selection problem
- Sort lectures by end time.
count = 1
,lastEnd = end[0]
- For each interval
(s[i], e[i])
starting from i=1:- If
s[i] ≥ lastEnd
,count++
,lastEnd = e[i]
.
- If
- Return
count
Time: O(n log n) Space: O(1).