Greedy Algorithms8 sections · 316 units
Open in Course

The Algorithm

Step by step

Here is the algorithm:

1.1. Sort all activities by end time (ascending).

2.2. Select the first activity (it ends earliest).

3.3. For each remaining activity: if its start time >= the end time of the last selected activity, select it.

4.4. Return the count of selected activities. The key condition: no overlap means start >= previous end. If an activity starts exactly when the previous one ends, they do not conflict. Time: O(nlogn)O(n \log n). Space: O(1)O(1).