-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
29 lines (20 loc) · 810 Bytes
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution:
def leastInterval(self, tasks: List[str], n: int) -> int:
pq = [] # (negative remain count, task)
for task, cnt in Counter(tasks).items():
heapq.heappush(pq, (-cnt, task))
cooling = [] # (time, negative remain count, task)
t = 0
while len(pq) > 0 or len(cooling) > 0:
while len(cooling) > 0 and cooling[0][0] < t:
_, neg_cnt, task = heapq.heappop(cooling)
heapq.heappush(pq, (neg_cnt, task))
if len(pq) == 0:
t = cooling[0][0] + 1
continue
neg_cnt, task = heapq.heappop(pq)
neg_cnt += 1
if neg_cnt < 0:
heapq.heappush(cooling, (t + n, neg_cnt, task))
t += 1
return t