-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJob Selection Problem.cpp
More file actions
59 lines (50 loc) · 1.28 KB
/
Job Selection Problem.cpp
File metadata and controls
59 lines (50 loc) · 1.28 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
// Job Selection Problem
#include <bits/stdc++.h>
using namespace std;
struct Job {
char id; // Job Id
int dead; // Deadline of job
int profit; // Profit if job is over before or on
// deadline
};
void printJobScheduling(Job arr[], int n){
sort(arr, arr + n, [](Job &a, Job &b){
return a.profit > b.profit;
});
int mx_dead = 0;
for(int i = 0; i < n; ++i){
mx_dead = max(mx_dead, arr[i].dead);
}
char slot[mx_dead + 1];
for(int i = 0; i <= mx_dead; ++i){
slot[i] = -1;
}
int count = 0, profit = 0;
for(int i = 0; i < n; ++i){
for(int j = arr[i].dead; j > 0; --j){
if(slot[j] == -1){
slot[j] = arr[i].id;
count++;
profit += arr[i].profit;
break;
}
}
}
for(int i = 1; i <= mx_dead; ++i){
cout << slot[i] << " ";
}
}
int main()
{
Job arr[] = { { 'a', 2, 100 },
{ 'b', 1, 19 },
{ 'c', 2, 27 },
{ 'd', 1, 25 },
{ 'e', 3, 15 } };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Following is maximum profit sequence of jobs "
"\n";
// Function call
printJobScheduling(arr, n);
return 0;
}