Skip to main content

210. Course Schedule II

link

Basically same as 207

class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:

indeg = [0] * numCourses

M = defaultdict(list)


for v1, v2 in prerequisites:
M[v2].append(v1)
indeg[v1] += 1


dq = deque([i for i in range(len(indeg)) if indeg[i] == 0])


res = []


while dq:

course = dq.popleft()

res.append(course)


for vv in M[course]:
indeg[vv] -= 1
if indeg[vv] == 0:
dq.append(vv)


return [] if len(res) != numCourses else res