forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1136.py
25 lines (22 loc) · 749 Bytes
/
1136.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
class Solution:
def minimumSemesters(self, N: int, relations: List[List[int]]) -> int:
g, d = collections.defaultdict(list), [0] * N
for i, j in relations:
g[i - 1].append(j - 1)
d[j - 1] += 1
q, resLen, res = list(), 0, 0
for i in range(N):
if d[i] == 0:
q.append(i)
resLen += 1
while len(q):
n = len(q)
for _ in range(n):
pre = q.pop(0)
for i in g[pre]:
d[i] -= 1
if d[i] == 0:
q.append(i)
resLen += 1
res += 1
return res if resLen == N else -1