1# Runtime: 1043 ms (Top 25.15%) | Memory: 14.3 MB (Top 64.15%)234class Solution:5def leastInterval(self, tasks: List[str], n: int) -> int:6max_heap = []7queue = deque()8word_count = defaultdict(int)9timer = 010for i in range(len(tasks)):11word_count[tasks[i]] += 112for _, val in word_count.items():13heappush(max_heap, -1 * val)14while max_heap or queue:15timer += 116if max_heap:17v = -1 * heappop(max_heap)18v -= 119if v:20queue.append((v, timer + n))21if queue and queue[0][1] == timer:22heappush(max_heap, -1 * queue.popleft()[0])23return timer