19/32
Part 5 of 7 · Rounds and timesLearn and do
More than an order: rounds and finish times
Some questions ask for more than an order: how many rounds the tasks take with enough workers, or when each one finishes.
With one-minute tasks and enough workers, the fewest rounds is the longest chain
from collections import deque
def count_rounds(tasks, needs):
unblocks, waiting = build_graph(tasks, needs)
queue = deque(task for task in tasks if waiting[task] == 0)
rounds = ran = 0
while queue:
rounds += 1
# exactly this round's tasks
for _ in range(len(queue)):
task = queue.popleft()
ran += 1
for nxt in unblocks[task]:
waiting[nxt] -= 1
if waiting[nxt] == 0:
# waits for the next round
queue.append(nxt)
return rounds if ran == len(tasks) else -1
print(count_rounds(tasks, needs), count_rounds(tasks, broken))
4 -1
Say every task takes one minute, and as many run at once as are ready.
Netflix's LeetCode tag list includes Parallel Courses (1 source), tagged Topological Sort, and this is its shape.
The fewest rounds is the grid lesson's one level per pass, with Kahn's counts in place of the seen-set.
With one-minute tasks and as many workers as you like, which round does package run in?
Pick an answer above to see the rest