-
Notifications
You must be signed in to change notification settings - Fork 0
/
092.py
91 lines (74 loc) · 2.46 KB
/
092.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""
Problem:
We're given a hashmap with a key courseId and value a list of courseIds, which
represents that the prerequsite of courseId is courseIds. Return a sorted ordering of
courses such that we can finish all courses.
Return null if there is no such ordering.
For example, given {'CSC300': ['CSC100', 'CSC200'], 'CSC200': ['CSC100'], 'CSC100': []},
should return ['CSC100', 'CSC200', 'CSCS300'].
"""
from typing import Dict, List, Optional, Set, Tuple
def get_order_helper(
course_map: Dict[str, str],
course: str,
order: List[str],
processed: Set[str],
break_limit: Optional[int] = None,
curr: int = 0,
) -> Tuple[Optional[List[int]], Optional[Set[int]]]:
if not break_limit:
break_limit = len(course_map)
if break_limit < curr:
return None, None
if course_map[course] == []:
# if the course doesn't have any pre-req
if course not in processed:
order.append(course)
processed.add(course)
return order, processed
for prerequisite in course_map[course]:
order, processed = get_order_helper(
course_map, prerequisite, order, processed, break_limit, curr + 1
)
if order is None:
return None, None
order.append(course)
processed.add(course)
return order, processed
def get_order(course_map: Dict[str, str]) -> Optional[List[str]]:
order = []
processed = set()
for course in course_map:
if course not in processed:
for prerequisite in course_map[course]:
if prerequisite not in processed:
order, processed = get_order_helper(
course_map, prerequisite, order, processed
)
if order is None:
return None
order.append(course)
processed.add(course)
return order
if __name__ == "__main__":
prereqs = {"CSC300": ["CSC100", "CSC200"], "CSC200": ["CSC100"], "CSC100": []}
print(get_order(prereqs))
prereqs = {
"CSC400": ["CSC300"],
"CSC300": ["CSC100", "CSC200"],
"CSC200": ["CSC100"],
"CSC100": [],
}
print(get_order(prereqs))
prereqs = {
"CSC400": ["CSC300"],
"CSC300": ["CSC100", "CSC200"],
"CSC200": ["CSC100"],
"CSC100": ["CSC400"],
}
print(get_order(prereqs))
"""
SPECS:
TIME COMPLEXITY: O(courses x prerequisites)
SPACE COMPLEXITY: O(courses x prerequisites)
"""