-
Notifications
You must be signed in to change notification settings - Fork 0
/
Job Sequencing Problem
32 lines (27 loc) · 982 Bytes
/
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
// problem url: https://www.codingninjas.com/codestudio/problems/job-sequencing-problem_1169460?topList=striver-sde-sheet-problems&utm_source=striver&utm_medium=website&leftPanelTab=1
// mode: Medium
// Greedy and Sorting
#include <bits/stdc++.h>
bool comparator(vector<int> j1, vector<int> j2){
if(j1[1] > j2[1]) return true;
return false;
}
int jobScheduling(vector<vector<int>> &jobs){
int maxDeadline = 0;
for(vector<int> job: jobs)
maxDeadline = max(maxDeadline, job[0]);
stable_sort(jobs.begin(), jobs.end(), comparator);
int tempArr[maxDeadline+1];
fill(tempArr, tempArr+maxDeadline+1, -1);
for(vector<int> job: jobs){
int ind = job[0];
if(tempArr[ind] != -1)
while(ind-- && tempArr[ind]!=-1);
if(ind>0)
tempArr[ind] = job[1];
}
int profit = 0;
for(int i=1; i<=maxDeadline; i++)
profit += tempArr[i]==-1 ? 0 : tempArr[i];
return profit;
}