Task Scheduler

Task Scheduler

collections version - 84ms

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import collections
class Solution:
def leastInterval(self, tasks, n):
"""
:type tasks: List[str]
:type n: int
:rtype: int
"""
d = collections.Counter(tasks)
counts = d.values()
longest = max(counts)
ans = (longest - 1) * (n + 1)
for cnt in counts:
ans += cnt == longest and 1 or 0
return max(len(tasks), ans)

test code

1
2
In [98]: s = Solution(); t = s.leastInterval(["A","A","A","B","B","B"], 2); print(t)
8

leet code