-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTaskScheduler.java
33 lines (28 loc) · 965 Bytes
/
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
/*https://leetcode.com/problems/task-scheduler/*/
/*Prateekshya*/
class Solution {
public int leastInterval(char[] tasks, int n) {
if (n == 0) return tasks.length;
int[] map = new int[26];
int result = 0, maxLength = 0;
ArrayList<Character> list = new ArrayList<Character>();
for (int i = 0; i < tasks.length; ++i)
{
++map[tasks[i]-'A'];
if (map[tasks[i]-'A'] > maxLength)
{
maxLength = map[tasks[i]-'A'];
list = new ArrayList<Character>();
list.add(tasks[i]);
}
else if (map[tasks[i]-'A'] == maxLength)
list.add(tasks[i]);
}
//add the maximum length tasks
result += (maxLength+((maxLength-1)*n));
//add the rest
result += (list.size()-1);
//return the longer length
return result < tasks.length ? tasks.length : result;
}
}