panaewboi

jobsc

Apr 17th, 2020
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. package specialproject;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Collections;
  5. import java.util.List;
  6.  
  7. public class JobSchedule {
  8.  
  9. // schedule jobs to maximize profit
  10. public static int scheduleJobs(List<Job> jobs, int T)
  11. {
  12. // stores maximum profit that can be earned by scheduling jobs
  13. int profit = 0;
  14.  
  15. // array to store used and unused slots info
  16. int[] slot = new int[T];
  17. Arrays.fill(slot, -1);
  18.  
  19. // consider each job in decreasing order of their profits
  20. for (Job job: jobs)
  21. {
  22. // search for next free slot and map the task to that slot
  23. for (int j = job.getDeadline() - 1; j >= 0; j--)
  24. {
  25. if (j < T && slot[j] == -1)
  26. {
  27. slot[j] = job.getId();
  28. profit += job.getProfit();
  29. break;
  30. }
  31. }
  32. }
  33.  
  34. // show all the list of scheduled jobs
  35. System.out.println("The Scheduled jobs are ");
  36. Arrays.stream(slot) // Java 8 Streams
  37. .filter(val -> val != -1)
  38. .forEach(System.out::println);
  39.  
  40. // return total profit that can be earned
  41. return profit;
  42. }
  43.  
  44.  
  45. // Driver program to test methods
  46. public static void main(String[] args)
  47. {
  48. // List of given jobs. Each job has an identifier, a deadline and profit associated with it
  49. List<Job> jobs = Arrays.asList(
  50. new Job(1, 2, 60), new Job(2, 1, 100),
  51. new Job(3, 3, 20), new Job(4, 2, 40),
  52. new Job(5, 1, 20), new Job(6, 3, 20)
  53. );
  54.  
  55. // stores maximum deadline that can be associated with a job
  56. final int T = 30;
  57.  
  58. // arrange the jobs in decreasing order of their profits
  59. Collections.sort(jobs, (a, b) -> b.getProfit() - a.getProfit());
  60.  
  61. // schedule jobs and calculate maximum profit
  62. System.out.println("\nTotal Profit: " + scheduleJobs(jobs, T));
  63. }
  64.  
  65. }
Add Comment
Please, Sign In to add comment