Algorithmic patterns · 14 / 15 Exercise Hard

Activity selection

Objective

A greedy algorithm picks the locally best option at each step. To fit the maximum number of activities without overlaps, the right strategy is to sort by end time and always keep the first one that starts after the previous one ends.

Instructions

  • The first line has the number n of activities; then n lines start end. An activity may start exactly when another ends.
  • Show Activities: n with the maximum number of activities that can be done without overlapping.
Input
5
1 4
3 5
0 6
5 7
8 9
Output
Activities: 3

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise