-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion_3.cpp
More file actions
65 lines (53 loc) · 1.19 KB
/
question_3.cpp
File metadata and controls
65 lines (53 loc) · 1.19 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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Job
{
int id;
int dead;
int profit;
};
class Solution
{
public:
static bool cmp(Job J1, Job J2) {
return (J1.dead < J2.dead);
}
vector<int> JobScheduling(Job arr[], int n) {
sort(arr, arr + n, cmp);
int maxDead = arr[n - 1].dead;
vector<int> dp(maxDead + 1, 0);
for (int i = 0; i < n; i++) {
for (int j = arr[i].dead; j > 0; j--) {
dp[j] = max(dp[j], dp[j - 1] + arr[i].profit);
}
}
int lastTime = 0, maxProfit = 0;
for (int i = 1; i <= maxDead; i++) {
if (dp[i] > maxProfit) {
maxProfit = dp[i];
lastTime = i;
}
}
return {lastTime, maxProfit};
}
};
int main()
{
int t = 1;
while (t--) {
int n = 5;
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;
}