-
Notifications
You must be signed in to change notification settings - Fork 1
/
MinimizeLateness.java
71 lines (61 loc) · 1.89 KB
/
MinimizeLateness.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.util.*;
class Job implements Comparable<Job>{
public String name;
public int processingTime;
public int deadline;
public int start;
public int finish;
public Job(String name, int processingTime, int deadline){
this.name=name;
this.processingTime=processingTime;
this.deadline=deadline;
}
@Override //Compare based on earliest deadline
public int compareTo(Job job) {
return this.deadline - job.deadline;
}
@Override
public String toString(){
return "[Job " + name + ": Processing Time="+processingTime + ", due="+deadline + ", (start="+start + ", finish="+finish + ")]";
}
}
public class MinimizeLateness {
public static void findMinLateness(Job[] jobs){
Arrays.sort(jobs); //Sort by earliest deadline
int currentIntervalEndTime = 0;
for(Job job : jobs){
job.start = currentIntervalEndTime;
job.finish = currentIntervalEndTime + job.processingTime;
currentIntervalEndTime += job.processingTime;
}
//Loop over jobs to find any that are late and find the maximum lateness value
int maxLateness=0;
String maxLateJob = ""; //Initialize to empty string in case no job is late
for(Job job : jobs){
int lateness = job.finish - job.deadline; //Calculate lateness. Negative means it's NOT late
if(lateness > maxLateness){
maxLateness = lateness;
maxLateJob = job.name;
}
}
System.out.print("Max Lateness = " + maxLateness);
if(!maxLateJob.equals("")){ //Only printed if a job was found. Skipped if no jobs are late
System.out.print(" From Job " + maxLateJob);
}
System.out.println("\nJob Assignment Order");
for(Job job : jobs){
System.out.println(job);
}
}
public static void main(String[] args) {
Job[] jobs = {
new Job("1", 3, 6),
new Job("2", 3, 14),
new Job("3", 1, 9),
new Job("4", 4, 9),
new Job("5", 2, 8),
new Job("6", 2, 15),
};
MinimizeLateness.findMinLateness(jobs);
}
}