-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathActivity_Selection.cpp
35 lines (33 loc) · 1.11 KB
/
Activity_Selection.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*Sort the activities according to their finishing time .Then,select the first activity from the
sorted array and print it.Do the following for the remaining activities in the sorted array.If the
start time of this activity is greater than or equal to the finish time of the previously selected
activity then select this activity and print it. Prints a maximum set of activities that can be done
by a single person, one at a time.*/
// n --> Total number of activities
// s[] --> An array that contains start time of all activities
// f[] --> An array that contains finish time of all activities
#include <bits/stdc++.h>
using namespace std;
void printMaxActivities(int s[], int f[], int n)
{
int i, j;
cout <<"Following activities are selected "<< endl;
i = 0;
cout <<" "<< i;
for (j = 1; j < n; j++)
{
if (s[j] >= f[i])
{
cout <<" " << j;
i = j;
}
}
}
int main()
{
int s[] = {1, 3, 0, 5, 8, 5};
int f[] = {2, 4, 6, 7, 9, 9};
int n = sizeof(s)/sizeof(s[0]);
printMaxActivities(s, f, n);
return 0;
}