Easy Python Solution


  • 0
    X
    class Solution(object):
        def findOrder(self, numCourses, prerequisites):
            """
            :type numCourses: int
            :type prerequisites: List[List[int]]
            :rtype: List[int]
            """
            n = numCourses; indegree = [0]*n; childs = [[] for i in range(n)] 
            for e in prerequisites:
                indegree[e[0]] += 1
                childs[e[1]].append(e[0])
                
            k = 0; stack = []
            for i in range(n):
                if indegree[i] == 0:
                    stack.append(i)
            ans = []
            while stack:
                tmp = stack.pop()
                k += 1
                if tmp not in ans:
                    ans.append(tmp)
                for ee in childs[tmp]:
                    indegree[ee] -= 1
                    if indegree[ee] == 0:
                        stack.append(ee)
                        
            return [[],ans][k==n]

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.