Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f227774

Browse files
Added medium problem.
1 parent 9bb99f5 commit f227774

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""
2+
# ALL PATHS FROM SOURCE TO TARGET
3+
4+
Given a directed acyclic graph (DAG) of n nodes labeled from 0 to n - 1, find all possible paths from node 0 to node n - 1, and return them in any order.
5+
6+
The graph is given as follows: graph[i] is a list of all nodes you can visit from node i (i.e., there is a directed edge from node i to node graph[i][j]).
7+
8+
Example 1:
9+
10+
Input: graph = [[1,2],[3],[3],[]]
11+
Output: [[0,1,3],[0,2,3]]
12+
Explanation: There are two paths: 0 -> 1 -> 3 and 0 -> 2 -> 3.
13+
14+
Example 2:
15+
16+
Input: graph = [[4,3,1],[3,2,4],[3],[4],[]]
17+
Output: [[0,4],[0,3,4],[0,1,3,4],[0,1,2,3,4],[0,1,4]]
18+
19+
Example 3:
20+
21+
Input: graph = [[1],[]]
22+
Output: [[0,1]]
23+
24+
Example 4:
25+
26+
Input: graph = [[1,2,3],[2],[3],[]]
27+
Output: [[0,1,2,3],[0,2,3],[0,3]]
28+
29+
Example 5:
30+
31+
Input: graph = [[1,3],[2],[3],[]]
32+
Output: [[0,1,2,3],[0,3]]
33+
34+
Constraints:
35+
36+
n == graph.length
37+
2 <= n <= 15
38+
0 <= graph[i][j] < n
39+
graph[i][j] != i (i.e., there will be no self-loops).
40+
The input graph is guaranteed to be a DAG.
41+
"""
42+
43+
class Solution:
44+
def allPathsSourceTarget(self, graph):
45+
paths = []
46+
currentPath = [0]
47+
self.dfs(graph, 0, currentPath, paths)
48+
return paths
49+
50+
def dfs(self, graph, node, currentPath, paths):
51+
if node == len(graph) - 1:
52+
paths.append(currentPath[:])
53+
return
54+
55+
for neighbor in graph[node]:
56+
currentPath.append(neighbor)
57+
self.dfs(graph, neighbor, currentPath, paths)
58+
currentPath.pop()
59+
60+
return
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"""
2+
# FIND EVENTUAL SAFE STATES
3+
4+
We start at some node in a directed graph, and every turn, we walk along a directed edge of the graph. If we reach a terminal node (that is, it has no outgoing directed edges), we stop.
5+
6+
We define a starting node to be safe if we must eventually walk to a terminal node. More specifically, there is a natural number k, so that we must have stopped at a terminal node in less than k steps for any choice of where to walk.
7+
8+
Return an array containing all the safe nodes of the graph. The answer should be sorted in ascending order.
9+
10+
The directed graph has n nodes with labels from 0 to n - 1, where n is the length of graph. The graph is given in the following form: graph[i] is a list of labels j such that (i, j) is a directed edge of the graph, going from node i to node j.
11+
12+
Example 1:
13+
14+
Illustration of graph
15+
Input: graph = [[1,2],[2,3],[5],[0],[5],[],[]]
16+
Output: [2,4,5,6]
17+
Explanation: The given graph is shown above.
18+
19+
Example 2:
20+
21+
Input: graph = [[1,2,3,4],[1,2],[3,4],[0,4],[]]
22+
Output: [4]
23+
24+
Constraints:
25+
26+
n == graph.length
27+
1 <= n <= 104
28+
0 <= graph[i].legnth <= n
29+
graph[i] is sorted in a strictly increasing order.
30+
The graph may contain self-loops.
31+
The number of edges in the graph will be in the range [1, 4 * 104].
32+
"""
33+
34+
class Solution:
35+
def eventualSafeNodes(self, graph):
36+
safeNodes = []
37+
safe = {}
38+
for vertex in range(len(graph)):
39+
if vertex not in safe:
40+
self.checkForSafety(graph, vertex, safe)
41+
42+
safeNodes = [node for node in safe if safe[node] == True]
43+
return sorted(safeNodes)
44+
45+
def checkForSafety(self, graph, current, safe):
46+
if current in safe:
47+
return safe[current]
48+
49+
if current not in safe:
50+
safe[current] = False
51+
52+
isCurrentSafe = True
53+
for neighbor in graph[current]:
54+
safety = self.checkForSafety(graph, neighbor, safe)
55+
if not safety:
56+
isCurrentSafe = False
57+
58+
safe[current] = isCurrentSafe
59+
return safe[current]
60+
61+
## TIME LIMIT EXCEEDED ##
62+
63+
"""class Solution:
64+
def eventualSafeNodes(self, graph: List[List[int]]) -> List[int]:
65+
safeNodes = []
66+
for vertex in range(len(graph)):
67+
if not self.checkForCycles(graph, vertex, set()):
68+
safeNodes.append(vertex)
69+
70+
return safeNodes
71+
72+
def checkForCycles(self, graph, current, visited):
73+
#print("current:",current,"visited:",visited)
74+
visited.add(current)
75+
for neighbor in graph[current]:
76+
if neighbor in visited:
77+
return True
78+
check = self.checkForCycles(graph, neighbor, visited)
79+
if check:
80+
return check
81+
visited.remove(current)
82+
return False"""
83+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /