-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjob.cpp
46 lines (40 loc) · 1.06 KB
/
job.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
#include <bits/stdc++.h>
using namespace std;
struct Job
{
int id; // Job Id
int dead; // Deadline of job
int profit; // Profit if job is over before or on deadline
};
struct comparitor_{
bool operator()(Job min,Job max){
return max.profit>min.profit;
}
};
class Solution
{
public:
//Function to find the maximum profit and the number of jobs done.
vector<int> JobScheduling(Job arr[], int n)
{
priority_queue<Job,vector<Job>,comparitor_> pq;
vector<int> ans(2,0);
vector<int> slot(n,0),result(n);
for(int i=0;i<n;i++){
pq.push(arr[i]);
}
for(int i=0;i<n;i++){
auto top=pq.top();pq.pop();
for(int j=min(n,top.dead)-1;j>=0;j--){
if(slot[j]==0){
result[j]=top.id;
slot[j]=1;
ans[0]++;
ans[1]+=top.profit;
break;
}
}
}
return ans;
}
};