题目描述
每个相同任务之间要有间隔 N
样例
Input: tasks = ["A","A","A","B","B","B"], n = 2
Output: 8
Explanation:
A -> B -> idle -> A -> B -> idle -> A -> B
There is at least 2 units of time between any two same tasks.
算法1 数学分析法
() $O(n)$ <– n指题目给定的实际间隔时间
时间复杂度
Python 代码
def leastInterval(self, tasks: List[str], n: int) -> int:
tasks_count = list(collections.Counter(tasks).values())
max_count = max(tasks_count)
max_count_tasks = tasks_count.count(max_count)
return max(len(tasks), (max_count-1)*(n+1)+max_count_tasks)