-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion_2.cpp
More file actions
73 lines (61 loc) · 1.36 KB
/
question_2.cpp
File metadata and controls
73 lines (61 loc) · 1.36 KB
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
struct Job
{
int id;
int dead;
int profit;
};
class Solution
{
public:
static bool cmp(Job J1, Job J2) {
if (J1.dead != J2.dead)
return J1.dead < J2.dead;
return J1.profit > J2.profit;
}
vector<int> JobScheduling(Job arr[], int n) {
sort(arr, arr + n, cmp);
int curr = 0;
int p = 0;
priority_queue<int, vector<int>, greater<int>> pq;
for (int i = 0; i < n; i++) {
Job J1 = arr[i];
if (curr < J1.dead) {
curr++;
pq.push(J1.profit);
}
else if (curr == J1.dead and pq.top() < J1.profit) {
pq.pop();
pq.push(J1.profit);
}
}
while (!pq.empty()) {
p += pq.top();
pq.pop();
}
return { curr, p };
}
};
int main()
{
// Number of test cases
int t = 1;
while (t--) {
int n = 5; // Number of jobs
Job arr[n] = {
{1, 2, 100},
{2, 1, 19},
{3, 2, 27},
{4, 1, 25},
{5, 3, 15}
};
Solution ob;
vector<int> ans = ob.JobScheduling(arr, n);
cout << ans[0] << " " << ans[1] << endl;
}
return 0;
}