Skip to content

Commit af1acbb

Browse files
committed
Update 1462. Course Schedule IV.py
1 parent 156328e commit af1acbb

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,26 @@
11
from typing import List
2+
from collections import deque, defaultdict
23
class Solution:
34
def checkIfPrerequisite(self, numCourses: int, prerequisites: List[List[int]], queries: List[List[int]]) -> List[bool]:
4-
5+
adj = [[] for _ in range(numCourses)]
6+
indegree = [0] * numCourses
7+
for p in prerequisites:
8+
adj[p[0]].append(p[1])
9+
indegree[p[1]] += 1
10+
11+
q = deque()
12+
for i in range(numCourses):
13+
if indegree[i] == 0:
14+
q.append(i)
15+
16+
mp = defaultdict(set)
17+
while q:
18+
curr = q.popleft()
19+
for next_course in adj[curr]:
20+
mp[next_course].add(curr)
21+
mp[next_course].update(mp[curr])
22+
indegree[next_course] -= 1
23+
if indegree[next_course] == 0:
24+
q.append(next_course)
25+
26+
return [q[0] in mp[q[1]] for q in queries]

0 commit comments

Comments
 (0)