Activity Selector

Greedy Algorithms Activity Selection Coding Challenge

Activity Selector

You are given a list of activities, where each activity is defined by its start and end times. Your task is to determine the maximum number of activities that can be scheduled without overlapping. In other words, select the maximum subset of non-overlapping activities.

Input:

  • An array (or list) of activities. Each activity is represented as an object or tuple with two properties: start and end.

Output:

  • An integer representing the maximum number of non-overlapping activities that can be selected.

Example:

Input: [ { start: 1, end: 4 }, { start: 3, end: 5 }, { start: 0, end: 6 }, { start: 5, end: 7 }, { start: 8, end: 9 }, { start: 5, end: 9 } ]
Output: 4

Explanation: One possible solution is to select activities with times (1,4), (5,7), (8,9) and one more that doesn't overlap with these intervals.

Instructions:

  • Use a greedy algorithm: sort the activities by their finish times and then select activities one by one.
  • Aim to solve the problem in under 45 minutes.

Historical note: February 21st is celebrated as International Mother Language Day, reminding us that clear communication (even in code) is key across different languages. Happy coding!