-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path11 August Job Sequencing Problem
50 lines (50 loc) · 1.42 KB
/
11 August Job Sequencing Problem
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
class Solution
{
public:
int findUncompleted(vector<bool>& don, int j, int n){
for(int i=j; i>n; i--){
if(don[i]==false)
return i;
}
return -1;
}
//Function to find the maximum profit and the number of jobs done.
vector<int> JobScheduling(Job arr[], int n)
{
// your code here
int mxdead = 0;
vector<pair<int, int>> vec;
for(int i=0; i<n; i++){
vec.push_back({arr[i].profit, arr[i].dead});
mxdead = max(mxdead, arr[i].dead);
}
sort(vec.begin(), vec.end(), greater<pair<int, int>>());
vector<bool>don(mxdead, false);
int count = 0, prof = 0;
int fillUntil = 0;
for(int i=0; i<n; i++){
int dad = vec[i].second;
int profi = vec[i].first;
if(!don[dad]){
don[dad] = true;
count++;
prof += profi;
}
else{
if(dad <= fillUntil){
continue;
}
int done = findUncompleted(don, dad-1, fillUntil);
if(done!=-1){
don[done] = true;
count++;
prof += profi;
}
else{
fillUntil = max(fillUntil, dad);
}
}
}
return {count, prof};
}
};