Skip to content

Instantly share code, notes, and snippets.

@codyhex
Created February 16, 2020 22:24
Show Gist options
  • Save codyhex/f8e4e44507238a3c9ba7f94915bc907b to your computer and use it in GitHub Desktop.
Save codyhex/f8e4e44507238a3c9ba7f94915bc907b to your computer and use it in GitHub Desktop.
def final_time(tasks, cooldown):
# this question is actually about putting a serialized tasks into parallel task Q
task_run_time = 1
lastExcStart = {} # coreId:lastTask's Start Time
sim_time = 0
for task in tasks:
if task not in lastExcStart:
# if no task is running on this core, init the queue with cuurent simTime
lastExcStart[task] = sim_time
else:
# if this core had a task. propergate the simTime using the cooldown constrict
if sim_time - lastExcStart[task] >= task_run_time + cooldown:
# if the sim time gap is larger than a cooldown, then you can start this task right away
lastExcStart[task] = sim_time
else:
# if sime time gap is not enough, you need to add a cooldown time to it.
lastExcStart[task] += task_run_time + cooldown
sim_time = lastExcStart[task]
return sim_time + task_run_time
print(final_time([1, 1, 2, 1], 2))
print(final_time([1, 1, 2, 1, 2, 2], 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment