-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJobStart&End.cpp
68 lines (57 loc) · 2.75 KB
/
JobStart&End.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
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
64
65
66
67
68
#include <iostream>
#include <algorithm>
using namespace std;
/*———————————————————————————————————————————————————————————————————————————*/
struct Job {
int start, end, profit;
};
/*———————————————————————————————————————————————————————————————————————————*/
bool comp(Job job1, Job job2) {
return (job1.end < job2.end);
}
/*———————————————————————————————————————————————————————————————————————————*/
int nonConflictJob(Job jobList[], int i) { //non conflicting job of jobList[i]
for (int j = i - 1; j >= 0; j--) {
if (jobList[j].end <= jobList[i-1].start)
return j;
}
return -1;
}
/*———————————————————————————————————————————————————————————————————————————*/
int findMaxProfit(Job jobList[], int n) {
sort(jobList, jobList + n, comp); //sort jobs based on the ending time
int *table = new int[n]; //create job table
table[0] = jobList[0].profit;
//Job display[n];
//display[0] = jobList[0];
for (int i=1; i<n; i++) {
// Find profit including the current job
int addProfit = jobList[i].profit;
int l = nonConflictJob(jobList, i);
if (l != -1)
addProfit += table[l];
if (addProfit > table[i-1]){
table[i] = addProfit;
cout << jobList[i-1].start << " - " << jobList[i-1].end << " - " << jobList[i-1].profit << "\n";
}
else{
table[i] = table[i-1];
cout << jobList[i-1].start << " - " << jobList[i-1].end << " - " << jobList[i-1].profit << "\n";
}
}
int result = table[n-1];
delete[] table; //clear table from memory
return result;
}
/*———————————————————————————————————————————————————————————————————————————*/
int main() {
Job jobList[] = {
{3, 5, 25},
{1, 2, 50},
{6, 15, 75},
{2, 100, 100}
};
int n = 4;
cout << "The maximum profit : \n" << findMaxProfit(jobList, n);
return 0;
}