LeetCoee 621
因为在相同task之间有cooldown的时间,那么最多的task需要尽量想办法分开,把其他的task放进cooldown的时间里来。如果其他的task可以完全填满这些cooldown的空格,那么最大时间其实就是所有task的时间,因为没有任何的浪费。比如cooldown时间是2,有AAA,BB,CC,DD,那么可以做成ABCDABCDA,使用BCD任务填充完需要cooldown的时间(超过也没有关系)。
如果不能填充完,那么我们就要计算这些cooldown需要的额外时间。
还有一种特殊的情况就是,如果有多个task一样多(并且都是最多),比如cooldown时间是2, AAA,BBB,CC的情况,那么B无法全部用于填充cooldown的时间,只能放入后面成为ABCABCAB。
def leastInterval(self, tasks: List[str], n: int) -> int:
counterDict = collections.Counter(tasks)
mostCommonKeyFreq = counterDict.most_common()[0][1]
mostCommonKeysCounter = sum([counterDict[key] == mostCommonKeyFreq for key in counterDict])
return max(len(tasks), (mostCommonKeyFreq - 1) * (n + 1) + mostCommonKeysCounter)