Advertisement
Guest User

Untitled

a guest
Sep 30th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. public static class FinishTimeComparator implements Comparator<Course>{
  2.  
  3. @Override
  4. public int compare(Course arg0, Course arg1) {
  5. if(arg0.finish <= arg1.finish){
  6. return -1;
  7. }else{
  8. return 1;
  9. }
  10. }
  11.  
  12. }
  13.  
  14.  
  15. public static class WeightedJobSchedulingMaximumProfit {
  16.  
  17. /**
  18. * Sort the jobs by finish time.
  19. * For every job find the first job which does not overlap with this job
  20. * and see if this job profit plus profit till last non overlapping job is greater
  21. * than profit till last job.
  22. * @param jobs
  23. * @return
  24. */
  25. public int maximum(Course[] arr){
  26. int T[] = new int[arr.length];
  27. FinishTimeComparator comparator = new FinishTimeComparator();
  28. Arrays.sort(arr, comparator);
  29.  
  30. T[0] = arr[0].profit;
  31. for(int i=1; i < arr.length; i++){
  32. T[i] = Math.max(arr[i].profit, T[i-1]);
  33. for(int j=i-1; j >=0; j--){
  34. if(arr[j].finish <= arr[i].start){
  35. T[i] = Math.max(T[i], arr[i].profit + T[j]);
  36. break;
  37. }
  38. }
  39. }
  40. int maxVal = Integer.MIN_VALUE;
  41. for (int val : T) {
  42. if (maxVal < val) {
  43. maxVal = val;
  44. }
  45. }
  46. return maxVal;
  47. }
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement