forked from shrikriti5singh/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivity_selection_sorted.c
More file actions
47 lines (33 loc) · 1019 Bytes
/
Copy pathactivity_selection_sorted.c
File metadata and controls
47 lines (33 loc) · 1019 Bytes
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
36
37
38
39
40
41
42
43
44
45
46
47
#include<stdio.h>
// C++ program for activity selection problem.
// The following implementation assumes that the activities
// are already sorted according to their finish time
// 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
void printMaxActivities(int s[], int f[], int n){
printf("Following activities are selected");
// the first activity is always selected
int i =0, j;
printf("%d", i);
//for the next activities
for(j=1; j<n; j++){
// If this activity has start time greater than or
// equal to the finish time of previously selected
// activity, then select it
if(s[j]>=f[i]){
printf("%d", j);
i=j;
}
}
}
int main(){
int s[]= {2, 5, 0, 7, 1};
int f[]= {3, 4, 6, 8, 9};
int x,y=0;
int n = sizeof(s)/sizeof(s[0]);
printMaxActivities(s, f, n);
return 0;
}