1class Solution:2def leastInterval(self, tasks: List[str], n: int) -> int:3max_heap = []4queue = deque()5word_count = defaultdict(int)6timer = 07for i in range(len(tasks)):8word_count[tasks[i]] += 19for _ , val in word_count.items():10heappush(max_heap, -1*val)11while max_heap or queue:12timer += 113if max_heap:14v = -1* heappop(max_heap)15v -= 116if v:17queue.append((v, timer+n))18if queue and queue[0][1] == timer:19heappush(max_heap, -1*queue.popleft()[0])20return timer