Skip to content

Commit 20551f1

Browse files
author
bansal
committed
[Activity selection]
1 parent 0ecb6bd commit 20551f1

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// C++ program for activity selection problem.
2+
3+
// The following implementation assumes that the activities
4+
// are already sorted according to their finish time
5+
#include <bits/stdc++.h>
6+
using namespace std;
7+
8+
// Prints a maximum set of activities that can be done by a
9+
// single person, one at a time.
10+
void printMaxActivities(int s[], int f[], int n)
11+
{
12+
int i, j;
13+
14+
cout << "Following activities are selected" << endl;
15+
16+
// The first activity always gets selected
17+
i = 0;
18+
cout << i << " ";
19+
20+
// Consider rest of the activities
21+
for (j = 1; j < n; j++) {
22+
// If this activity has start time greater than or
23+
// equal to the finish time of previously selected
24+
// activity, then select it
25+
if (s[j] >= f[i]) {
26+
cout << j << " ";
27+
i = j;
28+
}
29+
}
30+
}
31+
32+
// Driver code
33+
int main()
34+
{
35+
int s[] = { 1, 3, 0, 5, 8, 5 };
36+
int f[] = { 2, 4, 6, 7, 9, 9 };
37+
int n = sizeof(s) / sizeof(s[0]);
38+
39+
// Function call
40+
printMaxActivities(s, f, n);
41+
return 0;
42+
}

0 commit comments

Comments
 (0)