import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; class Job { String id; int deadline, profit; Job(String id, int deadline, int profit) { this.id = id; this.deadline = deadline; this.profit = profit; } } public class JobScheduling { public static void printJobScheduling(Job[] arr, int t) { int n = arr.length; Arrays.sort(arr, (a, b) -> b.profit - a.profit); boolean[] result = new boolean[t]; String[] job = new String[t]; Arrays.fill(job, "-1"); for (int i = 0; i < n; i++) { for (int j = Math.min(t - 1, arr[i].deadline - 1); j >= 0; j--) { if (!result[j]) { result[j] = true; job[j] = arr[i].id; break; } } } System.out.println("Job sequence for maximum profit:"); for (String j : job) { if (!j.equals("-1")) { System.out.print(j + " "); } } System.out.println(); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the number of jobs: "); int n = sc.nextInt(); Job[] arr = new Job[n]; for (int i = 0; i < n; i++) { System.out.print("Enter job ID for job " + (i + 1) + ": "); String jobId = sc.next(); System.out.print("Enter deadline for job " + (i + 1) + ": "); int deadline = sc.nextInt(); System.out.print("Enter profit for job " + (i + 1) + ": "); int profit = sc.nextInt(); arr[i] = new Job(jobId, deadline, profit); } System.out.print("Enter the maximum number of time slots available: "); int t = sc.nextInt(); printJobScheduling(arr, t); sc.close(); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter