-
Notifications
You must be signed in to change notification settings - Fork 0
/
workstations.java
131 lines (112 loc) · 3.96 KB
/
workstations.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import java.util.*;
import java.io.*;
public class workstations{
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String[] vals = reader.readLine().split(" ");
//number of researchers
int numRe = Integer.parseInt(vals[0]);
// time to autolock
int lockTime = Integer.parseInt(vals[1]);
//number of workstations needed to be unlocked
int unlockedNum = 0;
PriorityQueue<Integer> lockingTimePQ = new PriorityQueue<Integer>();
//storing all researchers arrival and usage time
ArrayList<Researcher> allRe = new ArrayList<Researcher>();
for (int i = 0; i < numRe; i++) {
String[] person = reader.readLine().split(" ");
int arrival = Integer.parseInt(person[0]);
int useTime = Integer.parseInt(person[1]);
Researcher newGuy = new Researcher(arrival, useTime);
allRe.add(newGuy);
}
//to sort researchers by earliest arrival time
allRe.sort(new SortByShortest());
for (Researcher person: allRe) {
int arrival = person.arrivalTime();
int useTime = person.timeToComplete();
//no unlocked workstations
if (lockingTimePQ.size() == 0) {
unlockedNum++;
//time that new workstation will lock
int newStationLockTime = arrival + useTime + lockTime;
lockingTimePQ.add(newStationLockTime);
}
// have workstation in PQ check if in use or unused and unlocked
else {
// assign unused and unlocked workstation
if (arrival >= lockingTimePQ.peek() - lockTime && arrival <= lockingTimePQ.peek()) {
lockingTimePQ.poll();
int newLockTime = arrival + useTime + lockTime;
lockingTimePQ.add(newLockTime);
}
// arrival is before any workstation is unused, unlock new workstation and assign
else if (arrival < lockingTimePQ.peek() - lockTime) {
unlockedNum++;
int newLockTime = arrival + useTime + lockTime;
lockingTimePQ.add(newLockTime);
}
//arrival is AFTER earliest workstation is locked
else {
//remove locked workstation
lockingTimePQ.poll();
//iterate to check for any unused and unlocked workstation
while(lockingTimePQ.size() != 0) {
// if arrival is before earliest unused workstation, break
if (arrival < lockingTimePQ.peek() - lockTime) {
break;
}
// assign unused and unlocked workstation if arrival time is apt
else if (arrival >= lockingTimePQ.peek() - lockTime && arrival <= lockingTimePQ.peek()) {
break;
}
else {
lockingTimePQ.poll();
}
}
//unlock a new workstation
if (lockingTimePQ.size() == 0) {
unlockedNum++;
//time that new workstation will lock
int newStationLockTime = arrival + useTime + lockTime;
lockingTimePQ.add(newStationLockTime);
}
else if (arrival >= lockingTimePQ.peek() - lockTime && arrival <= lockingTimePQ.peek()) {
lockingTimePQ.poll();
int newStationLockTime = arrival + useTime + lockTime;
lockingTimePQ.add(newStationLockTime);
}
// unlock new workstation to assign
else if (arrival < lockingTimePQ.peek() - lockTime) {
unlockedNum++;
//time that new workstation will lock
int newStationLockTime = arrival + useTime + lockTime;
lockingTimePQ.add(newStationLockTime);
}
}
}
}
int numSaved = numRe - unlockedNum;
System.out.println(numSaved);
}
}
class Researcher{
int arrival;
int useTime;
Researcher(int arrival, int useTime){
this.arrival = arrival;
this.useTime = useTime;
}
public int arrivalTime() {
return arrival;
}
public int timeToComplete() {
return useTime;
}
}
class SortByShortest implements Comparator<Researcher>{
// sorting researchers in ascending order of arrival
public int compare(Researcher a, Researcher b) {
return Integer.compare(a.arrival, b.arrival);
}
}