Skip to content

Commit 9a9f80e

Browse files
committed
feat: implement least interval
1 parent 71d1484 commit 9a9f80e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

alternative/medium/task_scheduler.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from typing import List
2+
import heapq
3+
from collections import deque, Counter
4+
5+
def leastInterval(tasks: List[str], n: int) -> int:
6+
maxHeap = []
7+
time = 0
8+
letter_count = {}
9+
q = deque()
10+
11+
letter_count = Counter(tasks)
12+
for value in letter_count.values():
13+
heapq.heappush(maxHeap, -value)
14+
15+
while maxHeap or q:
16+
time+=1
17+
if maxHeap:
18+
cnt = -heapq.heappop(maxHeap) - 1
19+
if cnt > 0:
20+
q.append([-cnt, time + n])
21+
if q and q[0][1] == time:
22+
heapq.heappush(maxHeap, q.popleft()[0])
23+
return time
24+
25+
print(leastInterval(["A","A","A","B","B","B"], 2))
26+
assert leastInterval(["A","A","A","B","B","B"], 2) == 8

0 commit comments

Comments
 (0)