-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDepth.java
More file actions
94 lines (67 loc) · 2.24 KB
/
Depth.java
File metadata and controls
94 lines (67 loc) · 2.24 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.util.ArrayList;
import java.util.Arrays;
public class Depth {
public static void main(String[] args) {
Schedule schedule1 = new Schedule(1, 4);
Schedule schedule2 = new Schedule(2, 3);
Schedule schedule3 = new Schedule(4, 5);
Schedule schedule4 = new Schedule(6, 8);
Schedule schedule5 = new Schedule(7, 10);
Schedule schedule6 = new Schedule(8, 9);
Schedule[] schedules = { schedule1, schedule2, schedule3, schedule4, schedule5, schedule6 };
// Print out the maximum amount of machines in use
printMachinesUsed(schedules);
}
public static void printMachinesUsed(Schedule[] schedules){
Arrays.sort(schedules);
ArrayList<Integer> machines = new ArrayList<Integer>();
for(int i = 0; i < schedules.length; i++){
boolean addedToExistingMachine = false;
for(int j = 0; j < machines.size(); j++){
if(machines.get(j)< schedules[i].getStart()){
machines.set(j,schedules[i].getEnd());
addedToExistingMachine = true;
printMachineAndSchedule(j, schedules[i]);
}
}
if(!addedToExistingMachine){
machines.add(schedules[i].getEnd());
printMachineAndSchedule(machines.size()-1,schedules[i]);
}
}
System.out.println("Number of machines used: "+ machines.size());
}
public static void printMachineAndSchedule(int machineId, Schedule schedule){
System.out.println("Machine "+machineId+" scheduled: ");
System.out.println(schedule.toString());
}
}
class Schedule implements Comparable<Schedule>{
private int start;
private int end;
public Schedule(int start, int end){
this.start = start;
this.end = end;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getEnd() {
return end;
}
public void setEnd(int end) {
this.end = end;
}
@Override
public int compareTo(Schedule o) {
// TODO Auto-generated method stub
return this.getStart() - o.getStart();
}
@Override
public String toString() {
return "Schedule [start=" + start + ", end=" + end + "]";
}
}