Skip to content

Commit 671dd5c

Browse files
Create 621-Task-Scheduler.java
1 parent bc1b725 commit 671dd5c

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

java/621-Task-Scheduler.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public int leastInterval(char[] tasks, int n) {
3+
if (n==0) return tasks.length;
4+
PriorityQueue<Integer> pq = new PriorityQueue<>((a,b)->b-a);
5+
Queue<Pair<Integer, Integer>> q = new LinkedList<>();
6+
int[] arr = new int[26];
7+
for (char c: tasks)
8+
arr[c-'A']++;
9+
for (int val: arr)
10+
if (val>0)
11+
pq.add(val);
12+
int time = 0;
13+
// System.out.println(pq);
14+
// System.out.println(q);
15+
while ((!pq.isEmpty() || !q.isEmpty())) {
16+
if (pq.isEmpty()) {
17+
time = Math.max(q.peek().getValue(), time);
18+
pq.add(q.poll().getKey());
19+
}
20+
int val = pq.poll();
21+
val--;
22+
time++;
23+
if (val>0) q.add(new Pair(val, time+n));
24+
// System.out.println(q + " "+ time);
25+
}
26+
return time;
27+
}
28+
}

0 commit comments

Comments
 (0)