Task Scheduler



Here is my simple Python solution, and costs 669 ms, beats 100%
What's more, it can be modified to be faster, because the sort is not necessary, and we just need to get the top two most frequent tasks.
The idea is quite simple. So I don't explain it here.
The code is as follows:class Solution(object):
def leastInterval(self, tasks, n):
"""
:type tasks: List[str]
:type n: int
:rtype: int
"""dct = {} for task in tasks: dct[task] = dct.get(task, 0) + 1 lst = dct.values() lst.sort(reverse=True) max_val = lst[0]  1 print(max_val) res = max_val*n for num in lst[1:]: if num >= max_val: res = max_val else: res = num if res < 0: return len(tasks) else: return res + len(tasks)

@zhanzq Nice solution. Thanks for sharing it. I have added your approach in the article. Thanks.

I'm confused with the time complexity of both methods. For example, in first method, aside from those operations in the inner while loop which is "time" times, you also need to sort the array for several times(this number is hard to define, but it exists, let's assume it is K), then we have another klog26. Shouldn't we consider this?