Here is the algorithm:
Sort all activities by end time (ascending).
Select the first activity (it ends earliest).
For each remaining activity: if its start time >= the end time of the last selected activity, select it.
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: . Space: .