forked from Hemanth5603/DP---DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgreedy_job_sequencing_problem.cpp
More file actions
57 lines (33 loc) · 871 Bytes
/
greedy_job_sequencing_problem.cpp
File metadata and controls
57 lines (33 loc) · 871 Bytes
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
#include<bits/stdc++.h>
using namespace std;
static bool compare(vector<int>a, vector<int>b) {
return (a[2] > b[2]);
}
vector<int> jobScheduling(vector<vector<int>> &jobs){
int n=jobs.size();
sort(jobs.begin(),jobs.end(),compare);
int maxi=INT_MIN;
for(int i=0;i<n;i++){
if(jobs[i][1]>maxi){
maxi=jobs[i][1];
}
}
vector<bool>papa(maxi,false);
int sum=0;
int count=0;
for(int i=0;i<n;i++)
{
int deadline=jobs[i][1];
for(int j=deadline;j>=1;j--)
{
if(papa[j-1]==false)
{
sum+=jobs[i][2];
count++;
papa[j-1]=true;
break;
}
}
}
return {count,sum};
}