Skip to content

Commit

Permalink
Merge pull request #579 from aishwarya-chandra/day11q1
Browse files Browse the repository at this point in the history
day11q1
  • Loading branch information
kratika-Jangid authored Jan 27, 2024
2 parents b24309b + 61e86f6 commit 16ffb26
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Day-11/Find Eventual Safe States/aishwarya--py.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
```
class Solution:
def eventualSafeNodes(self, graph: List[List[int]]) -> List[int]:
n = len(graph)
visited = [False] * n
unsafe = [0] * n
for i in range(n):
if unsafe[i] == 0:
visited[i] = True
self.dfs(i, visited, graph, unsafe)
visited[i] = False
result = []
for i in range(len(unsafe)):
if unsafe[i] == 1:
result.append(i)
return result
def dfs(self, node: int, visited: List[bool], graph: List[List[int]], unsafe: List[int]) -> bool:
isSafe = True
for neighbor in graph[node]:
if visited[neighbor] or unsafe[neighbor] == 2:
isSafe = False
break
if unsafe[neighbor] == 1:
continue
visited[neighbor] = True
if not self.dfs(neighbor, visited, graph, unsafe):
isSafe = False
visited[neighbor] = False
unsafe[node] = 1 if isSafe else 2
return isSafe
```

0 comments on commit 16ffb26

Please sign in to comment.