forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0207.py
More file actions
22 lines (20 loc) · 625 Bytes
/
0207.py
File metadata and controls
22 lines (20 loc) · 625 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution:
def canFinish(self, n: int, prerequisites: List[List[int]]) -> bool:
g = collections.defaultdict(list)
d, vis, q = [0] * n, [0] * n, []
for i, j in prerequisites:
g[j].append(i)
d[i] += 1
for i in range(n):
if d[i] == 0:
q.append(i)
vis[i] = 1
while q:
cur = q.pop(0)
n -= 1
for i in g[cur]:
d[i] -= 1
if not vis[i] and d[i] == 0:
vis[i] = 1
q.append(i)
return n == 0