Decide whether you can finish every course given its prerequisites. Take courses with no locks left, and see if every course gets taken.
▼The problem
There are numCourses courses, numbered from 0, and a list of prerequisite pairs [a, b]: take b before a. Can you finish every course? Equivalently: is the prerequisite graph free of cycles?
Example: 6 courses, pairs [1,0] [4,0] [1,3] [2,1] [2,4] [5,2]. Lock counts (in-degrees) start at [0, 2, 2, 0, 1, 1] for courses 0..5. Kahn's algorithm queues 0 and 3, then takes 0 (1 drops to 1, 4 drops to 0 and joins), 3 (1 opens), 4 (2 drops to 1), 1 (2 opens), 2 (5 opens), 5: order 0 3 4 1 2 5, 6 of 6 taken, so yes. The loop (new in the remake, so the algorithm makes some progress before it gets stuck): 4 courses, pairs [1,0] [2,1] [3,2] [1,3] (0 → 1 → 2 → 3 → back to 1).
def can_finish(n, prereqs):
unlocks = [[] for _ in range(n)]
need = [0] * n
for a, b in prereqs: # b before a
unlocks[b].append(a)
need[a] += 1
ready = deque(c for c in range(n) if need[c] == 0)
taken = 0
while ready:
c = ready.popleft()
taken += 1
for nxt in unlocks[c]:
need[nxt] -= 1
if need[nxt] == 0:
ready.append(nxt)
return taken == nTRY IT ON LEETCODE ▶The solution
def can_finish(n, prereqs):
unlocks = [[] for _ in range(n)]
need = [0] * n
for a, b in prereqs: # b before a
unlocks[b].append(a)
need[a] += 1
ready = deque(c for c in range(n) if need[c] == 0)
taken = 0
while ready:
c = ready.popleft()
taken += 1
for nxt in unlocks[c]:
need[nxt] -= 1
if need[nxt] == 0:
ready.append(nxt)
return taken == nTranscript
Course Schedule. You have courses numbered from zero, and a list of prerequisite pairs: take this course before that one. Can you finish every course?
Picture each course as a level on a world map. Zero leads to one and to four. Three also leads to one. One and four lead to two, and two leads to five. Each padlock counts the prerequisites a level still needs.
You could try every order, but six courses already have seven hundred twenty. Worse, the real danger is a loop. If courses wait on each other in a circle, none of them can ever go first.
So keep counts instead. A level whose lock reads zero is ready, so it waits in a queue. Take a level from the front and clear it. Every level it leads to loses one lock. When a lock hits zero, that level joins the queue.
In code, list what each course unlocks, and count its prerequisites. Queue every course with zero. Then pop a course, add one to taken, and lower the count of each course it unlocks, queuing any that reach zero. You finish only if taken equals the number of courses.
Let's play it. Zero and three start ready. Take zero: one drops to one, and four drops to zero. Take three: one opens. Take four: two drops to one. Take one: two opens. Then two, which opens five, and finally five. Six of six, so yes.
Now add a loop. Zero leads to one, one to two, two to three, and three back to one. Take zero: one drops to one, but it still waits on three. Three waits on two, and two waits on one. The queue runs dry. One of four, so no.
With V courses and E pairs, each course enters the queue at most once, and each pair lowers a count once. So time and space are both O of V plus E.
Clear what's ready, unlock what's next, and watch for loops. That's Course Schedule.