-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path207.py
43 lines (41 loc) · 1.41 KB
/
207.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
37
38
39
40
41
42
43
'''
Date: 2020-08-10 19:31:10
LastEditors: FrankCast1e
LastEditTime: 2020-08-11 11:39:51
FilePath: /Leetcode/207.py
'''
from typing import List
class Solution:
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
# 邻接表建立
neighbour_table = []
for i in range(numCourses):
neighbour_table.append([0 for _ in range(numCourses)])
for request in prerequisites:
neighbour_table[request[1]][request[0]] = 1
# 入度表
in_degree_list = []
queue = []
for i in range(numCourses):
in_degree = 0
for j in range(numCourses):
in_degree += neighbour_table[j][i]
in_degree_list.append(in_degree)
if in_degree == 0:
queue.append(i)
rest_courses_num = numCourses
while queue != []:
course = queue.pop(0)
rest_courses_num -= 1
for i in range(numCourses):
if neighbour_table[course][i] == 1:
in_degree_list[i] -= 1
neighbour_table[course][i] = 0
if in_degree_list[i] == 0:
queue.append(i)
if rest_courses_num != 0:
return False
else:
return True
x = Solution()
print(x.canFinish(5, [[1,0],[4,0],[1,2],[4,1],[2,4],[3,4]]))