task scheduler

🏠

Official answer:

 1 from collections import Counter
 2 
 3 class Solution:
 4     def leastInterval(self, tasks, n):
 5         freq = sorted(Counter([ord(x) - ord('A') for x in tasks]).values())
 6         f_max = freq.pop()
 7         idle_time = (f_max - 1) * n
 8         while freq and idle_time > 0:
 9             idle_time -= min(f_max - 1, freq.pop())
10         idle_time = idle_time if idle_time >= 0 else 0
11         return idle_time + len(tasks)
12 
13 tasks = ['A', 'A', 'A', 'B', 'B', 'C']
14 r = Solution().leastInterval(tasks, 2)
15 print(r)