-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6757 from aniketmdinde/master
Job sequencing in c++ #6742
- Loading branch information
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
code/operating_system/src/scheduling/job_sequencing/job_sequencing.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <algorithm> | ||
|
||
using namespace std; | ||
|
||
struct Job { | ||
char id; | ||
int deadline; | ||
int profit; | ||
}; | ||
|
||
bool jobComparator(const Job &a, const Job &b) { | ||
return a.profit > b.profit; | ||
} | ||
|
||
void jobSequence(vector<Job> &jobs) { | ||
int n = jobs.size(); | ||
|
||
sort(jobs.begin(), jobs.end(), jobComparator); | ||
|
||
int maxDeadline = 0; | ||
for (const Job &job : jobs) { | ||
maxDeadline = max(maxDeadline, job.deadline); | ||
} | ||
|
||
vector<bool> slot(maxDeadline, false); | ||
|
||
vector<char> sequence; | ||
int totalProfit = 0; | ||
|
||
for (const Job &job : jobs) { | ||
for (int i = min(maxDeadline - 1, job.deadline - 1); i >= 0; i--) { | ||
if (!slot[i]) { | ||
slot[i] = true; | ||
sequence.push_back(job.id); | ||
totalProfit += job.profit; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
cout << "Job sequence: "; | ||
for (char jobId : sequence) { | ||
cout << jobId << " "; | ||
} | ||
cout << "\nTotal profit: " << totalProfit << endl; | ||
} | ||
|
||
int main() { | ||
vector<Job> jobs = { | ||
{'a', 2, 100}, | ||
{'b', 1, 19}, | ||
{'c', 2, 27}, | ||
{'d', 1, 25}, | ||
{'e', 3, 15} | ||
}; | ||
|
||
jobSequence(jobs); | ||
|
||
return 0; | ||
} |