forked from super30admin/Greedy-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaskScheduler.java
55 lines (51 loc) · 1.69 KB
/
taskScheduler.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
// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : no
import java.util.*;
class Main {
// greedy approch by schedule critical task first with max freq
public static int leastInterval(char[] tasks, int n) {
// null case
if (tasks == null || tasks.length == 0)
return 0;
if (n == 0)
return tasks.length;
HashMap<Character, Integer> map = new HashMap<>();
int t = tasks.length;
// count each task and its freq
int maxFreq = 0;
int maxCount = 0;
for (char task : tasks) {
if (!map.containsKey(task)) {
map.put(task, 0);
}
// current Freq of any tasks
int curr = map.get(task);
map.put(task, curr + 1);
maxFreq = Math.max(maxFreq, map.get(task));
}
// second pass
// count maxCount with maxFreq tasks
for (char key : map.keySet()) {
if (map.get(key) == maxFreq) {
maxCount++;
}
}
// partition
int partition = maxFreq - 1;
// available time betwwen critical tasks is product
// of partition into effective k
int available = partition * (n - (maxCount - 1));
// remaining tasks
int rem = t - (maxCount * maxFreq);
// idle time
int idle = Math.max(0, available - rem);
return t + idle;
}
public static void main(String[] args) {
char[] tasks = new char[] { 'A', 'A', 'A', 'B', 'B', 'B' };
int n = 2;
System.out.println(leastInterval(tasks, n));
}
}