-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweightedIntervalScheduling.cpp
More file actions
63 lines (54 loc) · 1.42 KB
/
Copy pathweightedIntervalScheduling.cpp
File metadata and controls
63 lines (54 loc) · 1.42 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <algorithm>
#include <iostream>
using namespace std;
class Interval {
public:
int start;
int finish;
int weights;
};
bool jobsComparison(Interval a, Interval b) {
return (a.finish < b.finish);
}
int NonOverlappingJobs(Interval job[], int i) {
for (int j = i - 1; j >= 0; j--) {
if (job[j].finish <= job[i - 1].start) {
return j;
}
}
return -1;
}
int computeOptimal(Interval job[], int n) {
if (n == 1) {
{ return job[n - 1].weights; }
}
int j = NonOverlappingJobs(job, n);
int selected = job[n - 1].weights + computeOptimal(job, j + 1);
int notSelected = computeOptimal(job, n - 1);
return max(selected, notSelected);
}
int findSolution(Interval job[], int n) {
sort(job, job + n, jobsComparison);
return computeOptimal(job, n);
}
int main() {
int n;
cout << "Enter the number of jobs: ";
cin >> n;
Interval interval[n];
for (int i = 0; i < n; i++) {
cout << "Enter the start time of jobs: ";
cin >> interval[i].start;
}
for (int i = 0; i < n; i++) {
cout << "Enter the finish time of jobs: ";
cin >> interval[i].finish;
}
for (int i = 0; i < n; i++) {
cout << "Enter the weights of job: ";
cin >> interval[i].weights;
}
int optimalSolution = findSolution(interval, n);
cout << "\nOptimal Solution : " << optimalSolution;
return 0;
}