-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy path1136-parallel-courses.py
36 lines (29 loc) · 1014 Bytes
/
1136-parallel-courses.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from typing import List
class Solution:
def minimumSemesters(self, n: int, relations: List[List[int]]) -> int:
graph = {i: []for i in range(1, n+1)}
inCount = {i: 0 for i in range(1, n+1)}
for startNode, endNode in relations:
graph[startNode].append(endNode)
inCount[endNode] += 1
queue = []
for node in graph:
if inCount[node] == 0:
queue.append(node)
step = 0
studiedCount = 0
while queue:
step += 1
nextQueue = []
for node in queue:
studiedCount += 1
endNodeS = graph[node]
for endNode in endNodeS:
inCount[endNode] -= 1
if inCount[endNode] == 0:
nextQueue.append(endNode)
queue = nextQueue
return step if studiedCount == n else -1
n = 3
relations = [[1, 3], [2, 3]]
print(Solution().minimumSemesters(n, relations))