-
Notifications
You must be signed in to change notification settings - Fork 0
/
Job_Sequence_Problem.cpp
102 lines (86 loc) · 2.34 KB
/
Job_Sequence_Problem.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//{ Driver Code Starts
// Program to find the maximum profit job sequence from a given array
// of jobs with deadlines and profits
#include<bits/stdc++.h>
using namespace std;
// A structure to represent a job
struct Job
{
int id; // Job Id
int dead; // Deadline of job
int profit; // Profit if job is over before or on deadline
};
// } Driver Code Ends
/*
struct Job
{
int id; // Job Id
int dead; // Deadline of job
int profit; // Profit if job is over before or on deadline
};
*/
class Solution
{
public:
static bool comp(const Job &val1, const Job &val2)
{
return val1.profit > val2.profit;
}
pair<int, int> func(Job arr[], int n)
{
sort(arr, arr + n, comp);
int maxDeadline = 0;
for (int i = 0; i < n; i++) {
maxDeadline = max(maxDeadline, arr[i].dead);
}
vector<bool> slots(maxDeadline + 1, false);
int totalProfit = 0, jobCount = 0;
for (int i = 0; i < n; i++)
{
for (int j = min(maxDeadline, arr[i].dead); j > 0; j--)
{
if (!slots[j])
{
slots[j] = true;
jobCount++;
totalProfit += arr[i].profit;
break;
}
}
}
return {jobCount, totalProfit};
}
vector<int> JobScheduling(Job arr[], int n)
{
pair<int, int> result = func(arr, n);
return {result.first, result.second};
}
};
//{ Driver Code Starts.
// Driver program to test methods
int main()
{
int t;
//testcases
cin >> t;
while(t--){
int n;
//size of array
cin >> n;
Job arr[n];
//adding id, deadline, profit
for(int i = 0;i<n;i++){
int x, y, z;
cin >> x >> y >> z;
arr[i].id = x;
arr[i].dead = y;
arr[i].profit = z;
}
Solution ob;
//function call
vector<int> ans = ob.JobScheduling(arr, n);
cout<<ans[0]<<" "<<ans[1]<<endl;
}
return 0;
}
// } Driver Code Ends