From 1e0c120171f11807eb15433c2d34f718945c2dae Mon Sep 17 00:00:00 2001 From: Asbar <106991686+AsbarK@users.noreply.github.com> Date: Wed, 11 Oct 2023 12:16:18 +0530 Subject: [PATCH] added Job Sequencing Problem in Java --- code/job_sequencing.java | 52 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 code/job_sequencing.java diff --git a/code/job_sequencing.java b/code/job_sequencing.java new file mode 100644 index 0000000000..55852e6f68 --- /dev/null +++ b/code/job_sequencing.java @@ -0,0 +1,52 @@ +import java.util.*; + +class Job { + char id; + int deadline; + int profit; + + public Job(char id, int deadline, int profit) { + this.id = id; + this.deadline = deadline; + this.profit = profit; + } +} + +public class JobSequencing { + public static void main(String[] args) { + List jobs = new ArrayList<>(); + jobs.add(new Job('a', 2, 100)); + jobs.add(new Job('b', 1, 19)); + jobs.add(new Job('c', 2, 27)); + jobs.add(new Job('d', 1, 25)); + jobs.add(new Job('e', 3, 15)); + + // Sort the jobs in decreasing order of profit + jobs.sort((j1, j2) -> Integer.compare(j2.profit, j1.profit)); + + int maxDeadline = 0; + for (Job job : jobs) { + if (job.deadline > maxDeadline) { + maxDeadline = job.deadline; + } + } + + char[] result = new char[maxDeadline]; + Arrays.fill(result, ' '); + + int totalProfit = 0; + + for (Job job : jobs) { + for (int i = job.deadline - 1; i >= 0; i--) { + if (result[i] == ' ') { + result[i] = job.id; + totalProfit += job.profit; + break; + } + } + } + + System.out.println("Maximized Profit: " + totalProfit); + System.out.println("Job Sequence: " + new String(result)); + } +}