-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path210. Course Schedule II.py
29 lines (27 loc) · 1.08 KB
/
210. Course Schedule II.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
class Solution(object):
def findOrder(self, numCourses, prerequisites):
ans = []
cleared_courses = []
requirements = [0] * numCourses
for prerequisite in prerequisites:
requirements[prerequisite[0]] += 1
for i in range(len(requirements)):
if requirements[i] == 0:
cleared_courses.append(i)
ans += cleared_courses.copy()
while cleared_courses:
course = cleared_courses[0]
for this_course, needed_course in prerequisites:
if course == needed_course:
requirements[this_course] -= 1
if requirements[this_course] == 0:
cleared_courses.append(this_course)
ans.append(this_course)
del cleared_courses[0]
if len(ans) < numCourses:
return []
return ans
if __name__ == '__main__':
numCourses = 4
prerequisites = [[1, 0], [2, 0], [3, 1], [3, 2]]
print(Solution().findOrder(numCourses, prerequisites))