-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTaskScheduler.java
More file actions
76 lines (64 loc) · 2.74 KB
/
TaskScheduler.java
File metadata and controls
76 lines (64 loc) · 2.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
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.util.*;
public class TaskScheduler {
static void scheduleWithoutRelativeOrder(int[] tasks, int cooldown) {
// count frequency of each task
Map<Integer, Integer> counts = new HashMap<> ();
Set<Integer> mostFrequentTasks = new HashSet<> ();
int maxFreq = -1;
for(int i=0; i<tasks.length; ++i) {
counts.putIfAbsent(tasks[i], 0);
counts.put(tasks[i], counts.get(tasks[i]) + 1);
if(counts.get(tasks[i]) > maxFreq) {
maxFreq = counts.get(tasks[i]);
mostFrequentTasks = new HashSet<> ();
mostFrequentTasks.add(tasks[i]);
} else if(counts.get(tasks[i]) == maxFreq) {
mostFrequentTasks.add(tasks[i]);
}
}
//System.out.println(maxFreq + " ");
int leastUnits = Math.max(tasks.length, (maxFreq - 1) * (cooldown + 1) + mostFrequentTasks.size());
System.out.println(leastUnits + " units.");
}
static void scheduleWithRelativeOrder(int[] tasks, int cooldown) {
int ts = 0;
Map<Integer, Integer> task2Timestamp = new HashMap<> ();
for(int i=0; i<tasks.length; ++i) {
// first detection.
if(!task2Timestamp.containsKey(tasks[i])) {
task2Timestamp.put(tasks[i], ts);
System.out.print(tasks[i]);
ts += 1;
continue;
}
int prevTimeStamp = task2Timestamp.get(tasks[i]);
int currentTimeStamp = ts;
// if current ts satisfy the cooldown restriction
if(currentTimeStamp - prevTimeStamp - 1 < cooldown) {
task2Timestamp.put(tasks[i], cooldown + prevTimeStamp + 1);
int dif = cooldown - (currentTimeStamp - prevTimeStamp - 1);
for(int k=0; k<dif; ++k) System.out.print("_");
System.out.print(tasks[i]);
ts = cooldown + prevTimeStamp + 2;
} else { // if current ts doe not satisfy the cooldown restriction
task2Timestamp.put(tasks[i], ts);
System.out.print(tasks[i]);
ts += 1;
}
}
System.out.println();
System.out.println(ts + " units.");
}
public static void main(String[] args) {
int[] tasks = new int[] {1, 1, 1, 2, 2, 2};
int cooldown = 2;
scheduleWithoutRelativeOrder(tasks, cooldown);
tasks = new int[] {1, 2, 1, 1, 3, 4};
cooldown = 2;
scheduleWithRelativeOrder(tasks, cooldown);
tasks = new int[] {1,2,1,3,1,4};
scheduleWithRelativeOrder(tasks, cooldown);
tasks = new int[] {1, 2, 4 , 1, 3, 2, 1, 4};
scheduleWithRelativeOrder(tasks, cooldown);
}
}