2025/10/16
思路:贪心法,先统计不同的个数,然后每次穿一整遍所有剩下的,实在不行就插间隔
评价:完全正确
2025/10/19
def leastInterval(self, tasks: List[str], n: int) -> int:
from collections import defaultdict
counter = defaultdict(int)
for t in tasks:
counter[t] += 1
result = []
while len(counter != 1):
min_count = counter.min()
min_task = counter.argmin()
result += len(counter) * min_count
for t, c in counter.items():
counter[t] -= min_count
if counter[t] == 0:
del counter[t]
# now the counter has only 1 item
for t, c in counter.items():
result += n * c
return result