Back to Practice Dashboard
Competitive ProgrammingEasy

Activity Selection

Learn how to solve the 'Activity Selection' problem. This detailed resource details brute force and optimized approaches.

Problem Statement

Easy

Write a function activity_selection(start, end) that takes two lists start and end representing the start and end times of activities. Find the maximum number of activities that can be performed by a single person, assuming that a person can only work on a single activity at a time. Two activities are non-overlapping if the start time of the second is greater than or equal to the end time of the first.

Constraints
  • 1 <= len(start) == len(end) <= 10^5
  • 0 <= start[i] < end[i] <= 10^9

Examples

Example 1
Input
activity_selection([1, 3, 0, 5, 8, 5], [2, 4, 6, 7, 9, 9])
Output
4
Explanation

A person can perform at most 4 activities: [1,2], [3,4], [5,7], and [8,9].

Example 2
Input
activity_selection([10, 12, 20], [20, 25, 30])
Output
2
Explanation

Two activities can be performed: [10,20] and [20,30].

Need a Hint?
Analyze the input constraints. Try sorting first (O(n log n)) or using a hash map/set to track seen elements in O(n) time.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor