leetcode 621 task scheduler python
from collections import Counter
class Solution(object):
def leastInterval(self, tasks, n):
"""
:type tasks: List[str]
:type n: int
:rtype: int
"""
# init = tasks.popleft()
cnt = Counter("".join(tasks))
(init, _) = cnt.most_common(1)[0]
cnt.subtract({init: 1})
# init = tasks.pop(0)
schedule = [init]
interval = {init: 0}
i = 1
idx = 0
recompute = True
while True:
if recompute:
tasks_sorted = [t for (t, k) in cnt.most_common() if k != 0]
n_tasks = len(tasks_sorted)
if n_tasks == 0:
break
noop = 0
success = False
for t in tasks_sorted:
if t not in schedule or i-interval[t] > n:
schedule.append(t)
interval[t] = i
cnt.subtract({t: 1})
i += 1
success = True
recompute = True
break
if not success:
recompute = False
schedule.append('idle')
i += 1
# print schedule
return len(schedule)