|
1 | 1 | /**
|
2 |
| -Problem Statement: |
3 |
| -You are given n activities with their start and finish times. Select the maximum number of activities |
4 |
| -that can be performed by a single person, assuming that a person can only work on a single activity at a time. |
5 |
| -Solution: |
6 |
| -Since the activities are sorted in increasing order of finish time, we chose the activity whose finish time is less than |
7 |
| -all other activities and start time is more than the previous activities finish time. We should sort the activities in |
8 |
| -increasing order of finish time of activities. |
| 2 | + * @file |
| 3 | + * @brief |
| 4 | + * [https://www.geeksforgeeks.org/greedy-algorithms/] - Greedy algorithm |
| 5 | + * Problem info: https://www.geeksforgeeks.org/activity-selection-problem-greedy-algo-1/ |
| 6 | + * |
| 7 | + * @details |
| 8 | + * You are given n activities with their start and finish times. Select the maximum number of activities |
| 9 | + * that can be performed by a single person, assuming that a person can only work on a single activity at a time. |
| 10 | + * |
| 11 | + * Input: start[] = {10, 12, 20}, finish[] = {20, 25, 30} |
| 12 | + * Output: 0 |
| 13 | + * Explanation: A person can perform at most one activities. |
| 14 | + * |
| 15 | + * |
| 16 | + * Input: start[] = {1, 3, 0, 5, 8, 5}, finish[] = {2, 4, 6, 7, 9, 9}; |
| 17 | + * Output: 0 1 3 4 |
| 18 | + * Explanation: A person can perform at most four activities. The |
| 19 | + * maximum set of activities that can be executed |
| 20 | + * is {0, 1, 3, 4} [ These are indexes in start[] and finish[] |
| 21 | + * |
| 22 | + * @Approach |
| 23 | + * Since the activities are sorted in increasing order of finish time, we chose the activity whose finish time is less than |
| 24 | + * all other activities and start time is more than the previous activities finish time. We should sort the activities in |
| 25 | + * increasing order of finish time of activities. |
| 26 | + * |
| 27 | + * @author [Chhavi Bansal](https://github.com/chhavibansal) |
9 | 28 | */
|
| 29 | + |
10 | 30 | // The assumption here is input activities will be provided in sorted order of finish time of activities.
|
11 | 31 | #include <cassert> /// for assert
|
12 | 32 | #include <iostream>
|
|
0 commit comments