-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJobSequencingProblem.cpp
More file actions
80 lines (68 loc) · 1.22 KB
/
JobSequencingProblem.cpp
File metadata and controls
80 lines (68 loc) · 1.22 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
74
75
76
77
78
79
80
#include <iostream>
#include <vector>
#include <algorithm>
// QUESTION: https://www.techiedelight.com/job-sequencing-problem-deadlines/
struct Job
{
int id;
int deadline;
int profit;
};
class Scheduler
{
private:
std::vector<Job> jobs;
public:
Scheduler(std::vector<Job> j)
: jobs(j)
{
// Sor in decreasing order of profit
std::sort(jobs.begin(), jobs.end(),
[](const Job& a, const Job& b)
{
return a.profit > b.profit;
});
}
void compute()
{
std::vector<int> slots(jobs.size(), -1);
int maxProft = 0;
for (const auto& iter : jobs)
{
int deadline = iter.deadline - 1;
// Try to see if any slot before the deadline is available
for (int j = deadline; j >= 0; --j)
{
if (slots[j] == -1)
{
slots[j] = iter.id;
maxProft += iter.profit;
break;
}
}
}
std::cout << "Jobs can be scheduled as shown below, to get a max profit of " << maxProft << std::endl;
for (auto iter : slots)
{
std::cout << iter << " ";
}
std::cout << std::endl;
}
};
int main()
{
std::vector<Job> jobs{
{1, 9, 15},
{2, 2, 2},
{3, 5, 18},
{4, 7, 1},
{5, 4, 25},
{6, 2, 20},
{7, 5, 8},
{8, 7, 10},
{9, 4, 12},
{10, 3, 5}
};
Scheduler obj(jobs);
obj.compute();
}