【leetcode每日刷题】621. Task Scheduler
程序员文章站
2022-03-24 14:13:58
...
https://leetcode.com/problems/task-scheduler/
使用填槽的方式实现:先排序,根据具有最大数量的任务确定空槽的数量,即只执行最大数量的任务需要的空闲间隔,然后按照任务数量递减的顺序对每个任务进行遍历,使用任务数量进行填槽,最后根据空槽的数量确定返回结果。
class Solution(object):
def leastInterval(self, tasks, n):
"""
:type tasks: List[str]
:type n: int
:rtype: int
"""
maps = [0 for _ in range(26)]
for item in tasks:
maps[ord(item) - ord('A')] += 1
maps = sorted(maps)
max_value = maps[25] - 1
idle_slots = max_value * n
for i in range(24, -1, -1):
if maps[i] > 0:
idle_slots -= min(maps[i] , max_value)
else:
break
if idle_slots > 0:
return len(tasks) + idle_slots
else:
return len(tasks)
if __name__ == "__main__":
solution = Solution()
tasks = ["A","A","A","B","B","B"]
n = 2
print(solution.leastInterval(tasks, n))
上一篇: 力扣题目-----打家劫舍三道
推荐阅读