-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
945d82a
commit 7fc0c7d
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Solution: | ||
def cloneGraph(self, node: 'Node') -> 'Node': | ||
oldToNew = {} | ||
|
||
def dfs(node): | ||
if node in oldToNew: | ||
return oldToNew[node] | ||
|
||
copy = Node(node.val) | ||
oldToNew[node] = copy | ||
|
||
for nei in node.neighbors: | ||
copy.neighbors.append(dfs(nei)) | ||
|
||
return copy | ||
|
||
return dfs(node) if node else None | ||
|
||
## TC: O(num(node) + num(edge)) | ||
## SC: O(num(node)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class Solution: | ||
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool: | ||
indegree = collections.defaultdict(int) | ||
adj_list = collections.defaultdict(list) | ||
|
||
for pre in prerequisites: | ||
indegree[pre[0]] += 1 | ||
adj_list[pre[1]].append(pre[0]) | ||
|
||
starts = deque([i for i in range(numCourses) if indegree[i] == 0]) | ||
visited = set() | ||
|
||
for start in starts: | ||
self.dfs(indegree, adj_list, start, visited) | ||
|
||
return len(visited) == numCourses | ||
|
||
def dfs(self, indegree, adj_list, start, visited): | ||
if start in visited: | ||
return | ||
visited.add(start) | ||
for neigh in adj_list[start]: | ||
indegree[neigh] -= 1 | ||
if indegree[neigh] == 0: | ||
self.dfs(indegree, adj_list, neigh, visited) | ||
|
||
## TC & SC: O(num(node)+num(edge)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class Solution: | ||
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]: | ||
if not heights or not heights[0]: | ||
return [] | ||
|
||
m, n = len(heights), len(heights[0]) | ||
p_visited = set() | ||
a_visited = set() | ||
directions = [(-1, 0), (1, 0), (0, 1), (0, -1)] | ||
|
||
def dfs(visited, x, y): | ||
visited.add((x, y)) | ||
for dx, dy in directions: | ||
new_x, new_y = x + dx, y + dy | ||
if 0 <= new_x < m and 0 <= new_y < n and (new_x, new_y) not in visited and heights[new_x][new_y] >= \ | ||
heights[x][y]: | ||
dfs(visited, new_x, new_y) | ||
|
||
for i in range(m): | ||
dfs(p_visited, i, 0) | ||
dfs(a_visited, i, n - 1) | ||
|
||
for j in range(n): | ||
dfs(p_visited, 0, j) | ||
dfs(a_visited, m - 1, j) | ||
|
||
return list(p_visited.intersection(a_visited)) | ||
|
||
## TC: O(mn), SC: O(mn) |