Skip to content

Commit e3bcac8

Browse files
committed
idk
1 parent ba7584c commit e3bcac8

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from typing import List
2+
3+
4+
# leetcode submit region begin(Prohibit modification and deletion)
5+
class Solution:
6+
def checkIfPrerequisite(self, numCourses: int, prerequisites: List[List[int]], queries: List[List[int]]) -> List[
7+
bool]:
8+
# Create an adjacency matrix to track reachability
9+
reachable = [[False] * numCourses for _ in range(numCourses)]
10+
11+
# Mark direct prerequisites
12+
for u, v in prerequisites:
13+
reachable[u][v] = True
14+
15+
# Use Floyd-Warshall to compute transitive closure
16+
for k in range(numCourses):
17+
for i in range(numCourses):
18+
for j in range(numCourses):
19+
reachable[i][j] = reachable[i][j] or (reachable[i][k] and reachable[k][j])
20+
21+
# Answer the queries
22+
return [reachable[u][v] for u, v in queries]
23+
24+
25+
# leetcode submit region end(Prohibit modification and deletion)
26+
27+
28+
class CourseScheduleIv(Solution):
29+
pass
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import collections
2+
from typing import List
3+
4+
5+
# leetcode submit region begin(Prohibit modification and deletion)
6+
class Solution:
7+
def findRedundantConnection(self, edges: List[List[int]]) -> List[int]:
8+
9+
parents = collections.defaultdict(-1)
10+
11+
def find(i):
12+
path_comp = []
13+
while parents[i] >= 0:
14+
path_comp.append(i)
15+
i = parents[i]
16+
while path_comp:
17+
parents[path_comp.pop()] = i
18+
return i
19+
20+
def union(i, j):
21+
if i == j:
22+
return
23+
if parents[j] < parents[i]:
24+
return union(j, i)
25+
parents[i] += parents[j]
26+
parents[j] = i
27+
28+
ans = ()
29+
for u, v in edges:
30+
pu, pv = find(u), find(v)
31+
if pu == pv:
32+
ans = (u, v)
33+
else:
34+
union(pu, pv)
35+
return ans
36+
37+
38+
# leetcode submit region end(Prohibit modification and deletion)
39+
40+
41+
class RedundantConnection(Solution):
42+
pass

main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from leetcode.editor.en.Q1765.MapOfHighestPeak import MapOfHighestPeak
1+
from leetcode.editor.en.Q1462.CourseScheduleIv import CourseScheduleIv
22

33
if __name__ == '__main__':
4-
print(MapOfHighestPeak().highestPeak(isWater=[[0, 0, 1], [1, 0, 0], [0, 0, 0]]))
4+
print(CourseScheduleIv().checkIfPrerequisite(numCourses=2, prerequisites=[[1, 0]], queries=[[0, 1], [1, 0]]))

0 commit comments

Comments
 (0)