|
| 1 | +//created by Xingya Ren |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +class Assignment implements Comparator<Assignment>{ |
| 6 | + int number; |
| 7 | + int weight; |
| 8 | + int deadline; |
| 9 | + |
| 10 | + |
| 11 | + protected Assignment() { |
| 12 | + } |
| 13 | + |
| 14 | + protected Assignment(int number, int weight, int deadline) { |
| 15 | + this.number = number; |
| 16 | + this.weight = weight; |
| 17 | + this.deadline = deadline; |
| 18 | + } |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + /** |
| 23 | + * This method is used to sort to compare assignment objects for sorting. |
| 24 | + * Return -1 if a1 > a2 |
| 25 | + * Return 1 if a1 < a2 |
| 26 | + * Return 0 if a1 = a2 |
| 27 | + */ |
| 28 | + @Override |
| 29 | + public int compare(Assignment a1, Assignment a2) { |
| 30 | + // TODO Implement this |
| 31 | + //sort by weight in descending order |
| 32 | + //if there is a tie-->put the one that has larger deadline before |
| 33 | + if(a1.weight < a2.weight) { |
| 34 | + return 1; |
| 35 | + }else if(a1.weight > a2.weight) { |
| 36 | + return -1; |
| 37 | + }else { |
| 38 | + //two have the same weights |
| 39 | + //the one with larger deadline comes first |
| 40 | + if(a1.deadline < a2.deadline) { |
| 41 | + return 1; |
| 42 | + }else { |
| 43 | + return -1; |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +public class HW_Sched { |
| 50 | + ArrayList<Assignment> Assignments = new ArrayList<Assignment>(); |
| 51 | + int m; |
| 52 | + int lastDeadline = 0; |
| 53 | + |
| 54 | + protected HW_Sched(int[] weights, int[] deadlines, int size) { |
| 55 | + for (int i=0; i<size; i++) { |
| 56 | + Assignment homework = new Assignment(i, weights[i], deadlines[i]); |
| 57 | + this.Assignments.add(homework); |
| 58 | + if (homework.deadline > lastDeadline) { |
| 59 | + lastDeadline = homework.deadline; |
| 60 | + } |
| 61 | + } |
| 62 | + m =size; |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + /** |
| 67 | + * |
| 68 | + * @return Array where output[i] corresponds to the assignment |
| 69 | + * that will be done at time i. |
| 70 | + */ |
| 71 | + public int[] SelectAssignments() { |
| 72 | + //TODO Implement this |
| 73 | + |
| 74 | + //Sort assignments |
| 75 | + //Order will depend on how compare function is implemented |
| 76 | + Collections.sort(Assignments, new Assignment()); |
| 77 | + |
| 78 | + // If schedule[i] has a value -1, it indicates that the |
| 79 | + // i'th timeslot in the schedule is empty |
| 80 | + int[] homeworkPlan = new int[lastDeadline]; |
| 81 | + for (int i=0; i < homeworkPlan.length; ++i) { |
| 82 | + homeworkPlan[i] = -1; |
| 83 | + } |
| 84 | + //STUDENT CODE STARTS HERE |
| 85 | + //add assign to the array |
| 86 | + //if the next one should come before, then swap(?) |
| 87 | + |
| 88 | + //loop through the sorted list |
| 89 | + //get the largest deadline |
| 90 | + //check if that corresponding slot is occupied |
| 91 | + //if it is -->go to the previous slot |
| 92 | + //if not --> assign the assignment |
| 93 | + for(int i=1; i<=Assignments.size(); i++) { |
| 94 | + int theDdl = Assignments.get(i-1).deadline; |
| 95 | + if(homeworkPlan[theDdl-1] == -1) { |
| 96 | + //there is currently no assignment assiged to this slot |
| 97 | + homeworkPlan[theDdl-1] = Assignments.get(i-1).number; |
| 98 | + }else { |
| 99 | + //go to the previous slot |
| 100 | + for(int j=theDdl-2; j>=0; j--) { |
| 101 | + if (homeworkPlan[j]==-1) { |
| 102 | + homeworkPlan[j] = Assignments.get(i-1).number; |
| 103 | + break; |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + return homeworkPlan; |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
0 commit comments