-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path207CourseSchedule.py
36 lines (26 loc) · 1.31 KB
/
207CourseSchedule.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
class Solution:
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
# course which have nomout edge means dont have any prerequisite
# https://www.youtube.com/watch?v=EgI5nU9etnU&list=PLot-Xpze53ldBT_7QA8NVot219jFNr_GI&index=3
pre_map = {crs_num: [] for crs_num in range(numCourses)}
for course, prereq in prerequisites:
pre_map[course].append(prereq)
# print(pre_map)
visited_set = set()
def dfs(current_course):
if current_course in visited_set:
return False # loop here
if pre_map[current_course] == []:
return True
visited_set.add(current_course) # add to visted set before doing dfs
for pre_req in pre_map[current_course]:
if not dfs(pre_req):
return False
visited_set.remove(current_course) #remove current_course to visted set after doing dfs
pre_map[current_course] = [] # important step to avoid TLE
return True
for course in pre_map:
# needed because grapgh can be disconnected
if not dfs(course):
return False
return True