Skip to content

Commit decb469

Browse files
authored
Merge pull request #155 from leokim0922/main
[Leo] 9th Week solutions
2 parents 554e092 + 7fc0c7d commit decb469

File tree

5 files changed

+149
-0
lines changed

5 files changed

+149
-0
lines changed

clone-graph/Leo.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def cloneGraph(self, node: 'Node') -> 'Node':
3+
oldToNew = {}
4+
5+
def dfs(node):
6+
if node in oldToNew:
7+
return oldToNew[node]
8+
9+
copy = Node(node.val)
10+
oldToNew[node] = copy
11+
12+
for nei in node.neighbors:
13+
copy.neighbors.append(dfs(nei))
14+
15+
return copy
16+
17+
return dfs(node) if node else None
18+
19+
## TC: O(num(node) + num(edge))
20+
## SC: O(num(node))

course-schedule/Leo.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution:
2+
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
3+
indegree = collections.defaultdict(int)
4+
adj_list = collections.defaultdict(list)
5+
6+
for pre in prerequisites:
7+
indegree[pre[0]] += 1
8+
adj_list[pre[1]].append(pre[0])
9+
10+
starts = deque([i for i in range(numCourses) if indegree[i] == 0])
11+
visited = set()
12+
13+
for start in starts:
14+
self.dfs(indegree, adj_list, start, visited)
15+
16+
return len(visited) == numCourses
17+
18+
def dfs(self, indegree, adj_list, start, visited):
19+
if start in visited:
20+
return
21+
visited.add(start)
22+
for neigh in adj_list[start]:
23+
indegree[neigh] -= 1
24+
if indegree[neigh] == 0:
25+
self.dfs(indegree, adj_list, neigh, visited)
26+
27+
## TC & SC: O(num(node)+num(edge))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class TrieNode:
2+
def __init__(self):
3+
self.links = {}
4+
self.end = False
5+
6+
7+
class WordDictionary:
8+
def __init__(self):
9+
self.root = TrieNode()
10+
self.maxL = 0
11+
12+
def addWord(self, word: str) -> None:
13+
node = self.root
14+
l = 0
15+
16+
for w in word:
17+
if w not in node.links:
18+
node.links[w] = TrieNode()
19+
node = node.links[w]
20+
l += 1
21+
22+
self.maxL = max(self.maxL, l)
23+
node.end = True
24+
25+
## TC: O(len(word)), SC: O(1)
26+
27+
def search(self, word: str) -> bool:
28+
if len(word) > self.maxL:
29+
return False
30+
31+
def helper(index, node):
32+
for inn in range(index, len(word)):
33+
c = word[inn]
34+
if c == ".":
35+
for child in node.links.values():
36+
if helper(inn + 1, child):
37+
return True
38+
return False
39+
else:
40+
if c not in node.links:
41+
return False
42+
node = node.links[c]
43+
44+
return node.end
45+
46+
return helper(0, self.root)
47+
48+
## TC: O(num(node)), SC: O(len(word))

number-of-islands/Leo.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def numIslands(self, grid: List[List[str]]) -> int:
3+
res = 0
4+
rows, cols, = len(grid), len(grid[0])
5+
6+
def dfs(x, y):
7+
if x < 0 or y < 0 or x >= rows or y >= cols or grid[x][y] == "0":
8+
return
9+
10+
if grid[x][y] == "1":
11+
grid[x][y] = "0"
12+
dfs(x + 1, y)
13+
dfs(x - 1, y)
14+
dfs(x, y + 1)
15+
dfs(x, y - 1)
16+
17+
for i in range(rows):
18+
for j in range(cols):
19+
if grid[i][j] == "1":
20+
dfs(i, j)
21+
res += 1
22+
23+
return res
24+
25+
## TC & SC: O(m*n)

pacific-atlantic-water-flow/Leo.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution:
2+
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
3+
if not heights or not heights[0]:
4+
return []
5+
6+
m, n = len(heights), len(heights[0])
7+
p_visited = set()
8+
a_visited = set()
9+
directions = [(-1, 0), (1, 0), (0, 1), (0, -1)]
10+
11+
def dfs(visited, x, y):
12+
visited.add((x, y))
13+
for dx, dy in directions:
14+
new_x, new_y = x + dx, y + dy
15+
if 0 <= new_x < m and 0 <= new_y < n and (new_x, new_y) not in visited and heights[new_x][new_y] >= \
16+
heights[x][y]:
17+
dfs(visited, new_x, new_y)
18+
19+
for i in range(m):
20+
dfs(p_visited, i, 0)
21+
dfs(a_visited, i, n - 1)
22+
23+
for j in range(n):
24+
dfs(p_visited, 0, j)
25+
dfs(a_visited, m - 1, j)
26+
27+
return list(p_visited.intersection(a_visited))
28+
29+
## TC: O(mn), SC: O(mn)

0 commit comments

Comments
 (0)