-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallel_courses.py
30 lines (30 loc) · 1.1 KB
/
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
from collections import defaultdict
class Solution:
def minimumSemesters(self, N: int, relations: List[List[int]]) -> int:
edges = defaultdict(list)
semester = 0
indegree = defaultdict(lambda: 0)
for rel in relations:
edges[rel[0]].append(rel[1])
indegree[rel[1]] += 1
sources = []
for i in range(1, N + 1):
if indegree[i] == 0:
sources.append(i)
visited = [0]*(N + 1)
while (sources):
# print('sources: ', sources, 'indegree: ', indegree)
nextSource = []
for source in sources:
if not visited[source]:
for neighbor in edges[source]:
indegree[neighbor] -= 1
if not visited[neighbor] and indegree[neighbor] == 0:
nextSource.append(neighbor)
del indegree[source]
sources = nextSource
visited[source] = True
semester += 1
if len(indegree.keys()) != 0:
return -1
return semester