Loading...
Pick the maximum number of non-overlapping activities by always choosing the earliest finishing compatible activity.
Greedy Choice Property holds: picking the activity with the earliest finish time leaves the most room for the rest. Optimal Substructure: after choosing one activity, the remaining problem is the same on the filtered intervals.
lastFinish = -∞
.1procedure ACTIVITY_SELECTION(intervals)2 sort intervals by increasing finish time3 lastFinish ← −∞4 chosen ← ∅5 for each interval (s, f) in intervals do6 if s ≥ lastFinish then7 add interval to chosen8 lastFinish ← f9 else10 skip interval11 end if12 end for13 return chosen14end procedure