207 课程表

class Solution:
    def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
        adj=[]
        for i in range(numCourses):
            adj.append([])
        
        in_degree=[0 for i in range(numCourses)]
        for cur, pre in prerequisites:
            adj[pre].append(cur)
            in_degree[cur]+=1
        temp=[]
        for i in range(numCourses):
            if in_degree[i]==0:
                temp.append(i)   
        res=numCourses

        while temp:
            node=temp.pop(0)
            for j in adj[node]:
                in_degree[j]-=1
                if in_degree[j]==0:
                    temp.append(j)
            res-=1
        return not res

207 课程表_第1张图片

你可能感兴趣的:(学习笔记)