-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMinimizingLateness.java
More file actions
63 lines (52 loc) · 1.74 KB
/
MinimizingLateness.java
File metadata and controls
63 lines (52 loc) · 1.74 KB
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
/*
* Code by:@yinengy
* Time: 10/5/2018
*
* Correspond to the algorithm state in page 127 and 128,
* all /* comments are from the book.
*/
import java.util.Arrays;
import java.util.Comparator;
public class MinimizingLateness {
public static int[] Minimizing(int[][] jobs){
/* Order the jobs in order of their deadlines */
Arrays.sort(jobs, Comparator.comparingInt(array -> array[2])); // O(nlogn)
/* Assume for simplicity of notation that d1 ≤ ... ≤ dn */
/* Initially, f = s */ // f is the finishing time of last job
int f = 0;
// counting total processing time
int totalTime = 0;
for (int[] n: jobs) {
totalTime+= n[1];
}
// make a array to represent schedule
int[] schedule = new int[totalTime];
/* Consider the jobs i = 1, . . . , n in this order */
for (int i = 0; i < jobs.length; i++) { // O(n)
/* Assign job i to the time interval from s(i) = f to f(i) = f + ti */
for (int j = f; j < f + jobs[i][1]; j++) {
schedule[j] = jobs[i][0];
}
/* Let f = f + ti */
f += jobs[i][1];
} /* End */
/* Return the set of scheduled intervals [s(i), f(i)] for i = 1, . . . , n */
return schedule; // all in all, O(nlogn)
}
/**
* test
*/
public static void main(String[] args) {
// {id, processing time, due}
int[][] jobs = {{1, 3, 6},
{2, 2, 8},
{3, 1, 9},
{4, 4, 9},
{5, 3, 14},
{6, 2, 15}};
int[] schedule = Minimizing(jobs);
for (int num : schedule) {
System.out.print(num + " ");
}
}
}