-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOptimalScheduling.java
More file actions
72 lines (58 loc) · 2.17 KB
/
Copy pathOptimalScheduling.java
File metadata and controls
72 lines (58 loc) · 2.17 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
64
65
66
67
68
69
70
71
72
import java.util.*;
import datastrs.ArrayListOnSteroids;
/**
* Problem: Scheduling Problem
* Input: A set I of n intervals on the line.
* Output: What is the largest subset of mutually non-overlapping intervals which can be selected from I ?
* @author agoyal
*
*/
public class OptimalScheduling {
public List<List<Integer>> findOptimalSchedule(List<List<Integer>> options) {
List<List<Integer>> optimalSchedule = new ArrayList<List<Integer>>();
while (options.size() > 0) {
int earliestOption = findEarliestCompletionDate(options);
optimalSchedule.add(options.get(earliestOption));
removeNonDisjoint(options, earliestOption);
}
return optimalSchedule;
}
public int findEarliestCompletionDate(List<List<Integer>> options) {
int index = 0;
List<Integer> earliestOption = options.get(0);
for(int i = 0; i < options.size(); i++) {
List<Integer> option = options.get(i);
if(option.get(1) < earliestOption.get(1)) {
earliestOption = option;
index = i;
}
}
return index;
}
public void removeNonDisjoint(List<List<Integer>> options, int index) {
List<Integer> earliestOption = options.get(index);
List<Integer> indicesToRemove = new ArrayList<Integer>();
for(int i = 0; i < options.size(); i++) {
if (isDisjoint(earliestOption, options.get(i)))
continue;
indicesToRemove.add(i);
}
Collections.sort(indicesToRemove, Collections.reverseOrder());
for(int i : indicesToRemove) {
options.remove(i);
}
}
public boolean isDisjoint(List<Integer> one, List<Integer> two) {
return (one.get(1) < two.get(0) || one.get(0) > two.get(1));
}
public static void main(String[] args) {
OptimalScheduling os = new OptimalScheduling();
List<List<Integer>> options = new ArrayList<List<Integer>>();
options.add(new ArrayListOnSteroids<Integer>().addItem(2).addItem(25));
options.add(new ArrayListOnSteroids<Integer>().addItem(3).addItem(6));
options.add(new ArrayListOnSteroids<Integer>().addItem(17).addItem(21));
options.add(new ArrayListOnSteroids<Integer>().addItem(4).addItem(12));
options.add(new ArrayListOnSteroids<Integer>().addItem(15).addItem(25));
System.out.println(os.findOptimalSchedule(options));
}
}