-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlc_207.py
More file actions
43 lines (26 loc) · 1023 Bytes
/
Copy pathlc_207.py
File metadata and controls
43 lines (26 loc) · 1023 Bytes
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
37
38
39
40
41
42
43
# vedio: https://www.youtube.com/watch?v=EgI5nU9etnU&t=371s
# For question understanding https://www.youtube.com/watch?v=kXy0ABd1vwo
from typing import List
class Solution():
def canFinish(self,numCourses:int,prerequisites:List[List[int]])->bool:
preMap = { i:[] for i in range(numCourses)}
for crs,pre in prerequisites:
preMap[crs].append(pre)
#visitSet = all courses along the curr DFS path
visitSet=set()
def dfs(crs):
if crs in visitSet:
return False
if preMap[crs] == []:
return True
visitSet.add(crs)
for pre in preMap[crs]:
if not dfs(pre): return False
visitSet.remove(crs)
preMap[crs]=[]
return True
for crs in range(numCourses):
if not dfs(crs):return False
return True
# print(Solution().canFinish(2,[[1,0],[0,1]]))
print(Solution().canFinish(5,[[0,1],[0,2],[1,3],[1,4],[3,4]]))