Activity Selection Problem

Given a set of activities represented by start and finish times, select the maximum number of activities that can be performed by a single person, assuming a person can only work on one activity at a time. Pattern focus: Exchange Argument. Sorting by end time and selecting the earliest finishing compatible activity leaves as much room as possible for future choices.

Input Format

activities = list of [start, end] pairs

Output Format

maximum number of non-overlapping activities

Constraints

  • 1 <= activities.length <= 10^5
  • -10^9 <= start < end <= 10^9

Examples

Example 1:

Input:

activities = [[1,2],[3,4],[0,6],[5,7],[8,9],[5,9]]

Output:

4

Explanation:

Choose [1,2], [3,4], [5,7], and [8,9].

Example 2:

Input:

activities = [[1,3],[2,5],[4,7],[6,9],[8,10]]

Output:

3

Explanation:

Choose the earliest finishing compatible activities: [1,3], [4,7], and [8,10].

Example 3:

Input:

activities = [[10,12],[12,15],[0,1]]

Output:

3

Explanation:

Touching endpoints are compatible, so all three activities can be selected.

Loading...
Activity Selection Problem - Greedy DSA Problem