-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path210CourseScheduleII.py
More file actions
48 lines (46 loc) · 1.74 KB
/
210CourseScheduleII.py
File metadata and controls
48 lines (46 loc) · 1.74 KB
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
44
45
46
47
48
#BFS
class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
inDegree = [ 0 for _ in range(numCourses)]
neighbor = [ [] for _ in range(numCourses) ]
ans = []
queue = []
for depend in prerequisites:
inDegree[depend[0]] += 1
neighbor[depend[1]].append(depend[0])
for i in range(numCourses):
if inDegree[i] == 0:
queue.append(i)
while queue:
cur_class = queue.pop()
ans.append(cur_class)
for next_class in neighbor[cur_class]:
inDegree[next_class] -= 1
if inDegree[next_class] == 0:
queue.append(next_class)
return ans if len(ans) == numCourses else []
#DFS
class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
def dfs(cur , visited, isLooped , graph , ans):
if isLooped[cur] == 1:
return False
isLooped[cur] = 1
for nxt in graph[cur]:
if visited[nxt] == 0 and not dfs(nxt, visited, isLooped , graph , ans):
return False
isLooped[cur] = 0
visited[cur] = 1
ans.append(cur)
return True
visited = [0 for _ in range(numCourses)]
isLooped = [0 for _ in range(numCourses)]
graph = [[] for _ in range(numCourses)]
ans = []
for cur,nxt in prerequisites:
graph[cur].append(nxt)
for i in range(numCourses):
if visited[i] == 0 and not dfs(i, visited, isLooped , graph , ans):
return []
print (ans)
return ans if len(ans) == numCourses else []