Skip to content

Commit 2faad19

Browse files
Time: 100 ms (62.63%), Space: 15.5 MB (72.83%) - LeetHub
1 parent 7797b0b commit 2faad19

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
3+
graph = defaultdict(list)
4+
indeg = [0]*numCourses
5+
for i, j in prerequisites:
6+
graph[i].append(j)
7+
indeg[j] += 1
8+
9+
qu = deque()
10+
for i in range(numCourses):
11+
if indeg[i] == 0:
12+
qu.append(i)
13+
14+
count = 0
15+
while qu:
16+
curr = qu.popleft()
17+
count += 1
18+
for nei in graph[curr]:
19+
indeg[nei] -= 1
20+
if indeg[nei] == 0:
21+
qu.append(nei)
22+
23+
return count == numCourses

0 commit comments

Comments
 (0)