Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package specialproject;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.List;
- public class JobSchedule {
- // schedule jobs to maximize profit
- public static int scheduleJobs(List<Job> jobs, int T)
- {
- // stores maximum profit that can be earned by scheduling jobs
- int profit = 0;
- // array to store used and unused slots info
- int[] slot = new int[T];
- Arrays.fill(slot, -1);
- // consider each job in decreasing order of their profits
- for (Job job: jobs)
- {
- // search for next free slot and map the task to that slot
- for (int j = job.getDeadline() - 1; j >= 0; j--)
- {
- if (j < T && slot[j] == -1)
- {
- slot[j] = job.getId();
- profit += job.getProfit();
- break;
- }
- }
- }
- // show all the list of scheduled jobs
- System.out.println("The Scheduled jobs are ");
- Arrays.stream(slot) // Java 8 Streams
- .filter(val -> val != -1)
- .forEach(System.out::println);
- // return total profit that can be earned
- return profit;
- }
- // Driver program to test methods
- public static void main(String[] args)
- {
- // List of given jobs. Each job has an identifier, a deadline and profit associated with it
- List<Job> jobs = Arrays.asList(
- new Job(1, 2, 60), new Job(2, 1, 100),
- new Job(3, 3, 20), new Job(4, 2, 40),
- new Job(5, 1, 20), new Job(6, 3, 20)
- );
- // stores maximum deadline that can be associated with a job
- final int T = 30;
- // arrange the jobs in decreasing order of their profits
- Collections.sort(jobs, (a, b) -> b.getProfit() - a.getProfit());
- // schedule jobs and calculate maximum profit
- System.out.println("\nTotal Profit: " + scheduleJobs(jobs, T));
- }
- }
Add Comment
Please, Sign In to add comment