Skip to content

Instantly share code, notes, and snippets.

@Desolve
Created June 5, 2023 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Desolve/182d787f4f43559379a39a192c1ffe24 to your computer and use it in GitHub Desktop.
Save Desolve/182d787f4f43559379a39a192c1ffe24 to your computer and use it in GitHub Desktop.
0802 Find Eventual Safe States
class Solution:
def eventualSafeNodes(self, graph: List[List[int]]) -> List[int]:
def dfs(curr, vis, path):
vis[curr] = 1
path[curr] = 1
for u in graph[curr]:
if vis[u] == 0:
if dfs(u, vis, path):
return True
elif path[u]:
return True
path[curr] = 0
return False
n = len(graph)
vis = [0] * n
path = [0] * n
res = []
for i in range(n):
if vis[i] == 0:
dfs(i, vis, path)
for i in range(n):
if path[i] == 0:
res.append(i)
return res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment