Skip to content

Commit c7dd012

Browse files
authoredDec 26, 2021
Create 621-Task-Scheduler.py
1 parent 37f0235 commit c7dd012

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
 

‎621-Task-Scheduler.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def leastInterval(self, tasks: List[str], n: int) -> int:
3+
count = Counter(tasks)
4+
maxHeap = [-cnt for cnt in count.values()]
5+
heapq.heapify(maxHeap)
6+
7+
time = 0
8+
q = deque() # pairs of [-cnt, idleTime]
9+
while maxHeap or q:
10+
time += 1
11+
12+
if not maxHeap:
13+
time = q[0][1]
14+
else:
15+
cnt = 1 + heapq.heappop(maxHeap)
16+
if cnt:
17+
q.append([cnt, time + n])
18+
if q and q[0][1] == time:
19+
heapq.heappush(maxHeap, q.popleft()[0])
20+
return time

0 commit comments

Comments
 (0)
Please sign in to comment.