Skip to main content

Task Scheduler


What is the most efficient algorithm for scheduling tasks with varying degrees of priority?

Example 1
Input:  tasks = ["A","A","A","B","B","B"], n = 2

Output: ["A","B","A","B","A","B"]



# Given an array of tasks and a cooldown period, return the minimum amount of time needed to finish all tasks

def taskScheduler(tasks, cooldown):
# create a dictionary to store the time spent on each task
task_times = {}

# create a variable to store the total time
total_time = 0

# loop over the tasks
for task in tasks:
# if the task has already been done, add the cooldown period to the total time
if task in task_times:
total_time += cooldown
# otherwise, add the task to the dictionary and add its duration to the total time
task_times[task] = True
total_time += task.duration

# return the total time
return total_time