1
# Runtime: 1043 ms (Top 25.15%) | Memory: 14.3 MB (Top 64.15%)
2

3

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

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0