From ac78838c8b10fbc2c2ae235aed9b0ce56aee6e55 Mon Sep 17 00:00:00 2001 From: Khushi Singh <87067309+singhkhushi3026@users.noreply.github.com> Date: Fri, 23 Dec 2022 00:53:08 +0530 Subject: [PATCH] Create JobSequencing.java --- greedy-algorithms/JobSequencing.java | 65 ++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 greedy-algorithms/JobSequencing.java diff --git a/greedy-algorithms/JobSequencing.java b/greedy-algorithms/JobSequencing.java new file mode 100644 index 0000000..e81bb01 --- /dev/null +++ b/greedy-algorithms/JobSequencing.java @@ -0,0 +1,65 @@ +import java.util.*; + +class Job { + + char id; + int deadline, profit; + + public Job() {} + + public Job(char id, int deadline, int profit) + { + this.id = id; + this.deadline = deadline; + this.profit = profit; + } + + void printJobScheduling(ArrayList arr, int t) + { + int n = arr.size(); + + Collections.sort(arr, + (a, b) -> b.profit - a.profit); + + + boolean result[] = new boolean[t]; + char job[] = new char[t]; + + for (int i = 0; i < n; i++) { + for (int j + = Math.min(t - 1, arr.get(i).deadline - 1); + j >= 0; j--) { + if (result[j] == false) { + result[j] = true; + job[j] = arr.get(i).id; + break; + } + } + } + + + for (char jb : job) + System.out.print(jb + " "); + System.out.println(); + } + + + public static void main(String args[]) + { + ArrayList arr = new ArrayList(); + arr.add(new Job('a', 2, 100)); + arr.add(new Job('b', 1, 19)); + arr.add(new Job('c', 2, 27)); + arr.add(new Job('d', 1, 25)); + arr.add(new Job('e', 3, 15)); + + System.out.println( + "Following is maximum profit sequence of jobs"); + + Job job = new Job(); + + // Function call + job.printJobScheduling(arr, 3); + } +} +