-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskScheduler.java
More file actions
155 lines (130 loc) · 3.98 KB
/
Copy pathTaskScheduler.java
File metadata and controls
155 lines (130 loc) · 3.98 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
class TaskScheduler {
// MIMIC CPU COOLDOWN
// PriorityQueue<Character> schedule = new PriorityQueue<>();
ArrayDeque<Character> schedule = new ArrayDeque<>();
ArrayList<Character> scheduleOrder = new ArrayList<>();
/*
Switch to HashMap to avoid positional bias,
when you pull from a queue the elements that appear first are select more often.
*/
public void loadTasksInSchedule(char[] tasks)
{
HashMap<Character, Integer> tasksAndFrequency
= new HashMap<>();
for (char x : tasks)
{
tasksAndFrequency.put
(x, tasksAndFrequency.getOrDefault(x, 0) + 1);
}
while (!tasksAndFrequency.isEmpty())
{
Character target = findMaxFreqencyKey(tasksAndFrequency);
for (int i = 0; i < tasksAndFrequency.get(target); i++)
{
schedule.add(target);
}
tasksAndFrequency.remove(target);
}
}
public Character findMaxFreqencyKey(HashMap<Character, Integer> tasksAndFrequency)
{
int min = Integer.MAX_VALUE;
char target = ' ';
for (Map.Entry<Character, Integer> entry
:
tasksAndFrequency.entrySet())
{
int x = entry.getValue();
if (x < min)
{
min = x;
target = entry.getKey();
}
}
return target;
}
/*
Check if the distance between a currently evaluated task and
an already evaluated task is less than the limit.
E.g. Currently, Eval: A*, Rule: 2, {A, B, C, A*} = true
*/
public boolean checkIntervalLimitation(Character x, int limit)
{
if (scheduleOrder.size() < 1) return true;
int reach = scheduleOrder.size() - limit;
if (reach < 0) reach = 0;
for (int i = scheduleOrder.size() - 1; i >= reach; i--)
{
if (x.equals(scheduleOrder.get(i)))
{
return false;
}
}
return true;
}
public boolean checkScheduleCompletion()
{
for (Character x : schedule)
{
if(!x.equals('∅')) return false;
}
return true;
}
public void asdasdasd(int n)
{
int elmChecked = 0;
for (Character elm : schedule)
{
elmChecked++;
if (checkIntervalLimitation(elm, n)
&&
!elm.equals('∅'))
{
scheduleOrder.add(elm);
schedule.remove(elm);
schedule.add('∅');
return;
}
if (elmChecked == schedule.size())
{
scheduleOrder.add('∅');
}
}
}
public int runleastInterval(char[] tasks, int n)
{
loadTasksInSchedule(tasks);
while (!checkScheduleCompletion())
{
asdasdasd(n);
}
int result = scheduleOrder.size();
scheduleOrder.clear();
schedule.clear();
return result;
}
public int leastInterval(char[] tasks, int n)
{
return runleastInterval(tasks, n);
}
}
// Unit Tests
class runTaskScheduler
{
public static void main(String[] args)
{
TaskScheduler ts = new TaskScheduler();
System.out.println
(
ts.leastInterval(new char[]{'A','A','A','B','B','B'}, 2) + " " + //Answer: 8
ts.leastInterval(new char[]{'A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C', 'D', 'D', 'E'}, 2) + " " + //Answer: 12
ts.leastInterval(new char[]{'A', 'A', 'B', 'B', 'C', 'C', 'D', 'D', 'E', 'E', 'F', 'F', 'G', 'G',
'H', 'H', 'I', 'I', 'J', 'J', 'K', 'K', 'L', 'L', 'M', 'M', 'N', 'N', 'O', 'O', 'P', 'P', 'Q', 'Q', 'R', 'R', 'S', 'S', 'T',
'T','U','U','V','V','W','W','X','X','Y','Y','Z','Z'}, 2) //Answer: 52
);
}
}