From 13bbab988b1042886ae4cbfd77a1f44f777f8d35 Mon Sep 17 00:00:00 2001 From: Apurva Vats Date: Fri, 31 Oct 2025 00:50:01 +0530 Subject: [PATCH] Add job sequencing algorithm in C++ (issue #3331) --- archive/c/c-plus-plus/job-sequencing.cpp | 92 ++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 archive/c/c-plus-plus/job-sequencing.cpp diff --git a/archive/c/c-plus-plus/job-sequencing.cpp b/archive/c/c-plus-plus/job-sequencing.cpp new file mode 100644 index 000000000..f06742086 --- /dev/null +++ b/archive/c/c-plus-plus/job-sequencing.cpp @@ -0,0 +1,92 @@ +#include +#include +#include +#include +#include + +using namespace std; + +const string usage_msg = "Usage: please provide a list of profits and a list of deadlines\n"; +const int MAX_JOBS = 100; + +struct Job { + int profit; + int deadline; +}; + +bool compare(const Job &a, const Job &b) { + return a.profit > b.profit; +} + +int jobSequencing(vector &jobs) { + sort(jobs.begin(), jobs.end(), compare); + + int maxDeadline = 0; + for (auto &job : jobs) { + maxDeadline = max(maxDeadline, job.deadline); + } + + vector slot(MAX_JOBS, 0); + int totalProfit = 0; + + for (auto &job : jobs) { + for (int j = min((int)jobs.size(), job.deadline) - 1; j >= 0; --j) { + if (slot[j] == 0) { + slot[j] = 1; + totalProfit += job.profit; + break; + } + } + } + + return totalProfit; +} + +vector parseInput(const string &input) { + vector arr; + stringstream ss(input); + string token; + while (getline(ss, token, ',')) { + token.erase(0, token.find_first_not_of(" \t")); + token.erase(token.find_last_not_of(" \t") + 1); + if (!token.empty()) { + try { + arr.push_back(stoi(token)); + } catch (...) { + throw invalid_argument("Invalid input"); + } + } + } + return arr; +} + +int main(int argc, char* argv[]) { + if (argc != 3) { + cout << usage_msg; + return 1; + } + + vector profits, deadlines; + try { + profits = parseInput(argv[1]); + deadlines = parseInput(argv[2]); + } catch (...) { + cout << usage_msg; + return 1; + } + + if (profits.size() != deadlines.size() || profits.empty()) { + cout << usage_msg; + return 1; + } + + vector jobs; + for (size_t i = 0; i < profits.size(); ++i) { + jobs.push_back({profits[i], deadlines[i]}); + } + + int result = jobSequencing(jobs); + cout << result << endl; + + return 0; +}