621. Task Scheduler
- Use heap and queue to track item and idle time
- log(26)
class Solution:
def leastInterval(self, tasks: List[str], n: int) -> int:
counts = Counter(tasks)
maxHeap = [-cnt for cnt in counts.values()]
heapq.heapify(maxHeap)
q = deque()
time = 0
while maxHeap or q:
time += 1
if maxHeap:
val = heapq.heappop(maxHeap) + 1
if val:
q.append([val, n + time])
if q and q[0][1] == time:
heapq.heappush(maxHeap, q.popleft()[0])
return time