Skip to content

Commit f251f9f

Browse files
committed
update lc 802 java, add pic, cheatsheet
1 parent 6c891a1 commit f251f9f

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

doc/cheatsheet/graph.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,4 +315,82 @@ class Solution(object):
315315
seen.add(j)
316316
routes[i] = [] # seen route
317317
return -1
318+
```
319+
320+
### 2-5) Find Eventual Safe States
321+
```java
322+
// java
323+
// LC 802
324+
325+
// V1-0
326+
// IDEA : DFS
327+
// KEY : check if there is a "cycle" on a node
328+
// https://www.youtube.com/watch?v=v5Ni_3bHjzk
329+
// https://zxi.mytechroad.com/blog/graph/leetcode-802-find-eventual-safe-states/
330+
public List<Integer> eventualSafeNodes(int[][] graph) {
331+
// init
332+
int n = graph.length;
333+
State[] states = new State[n];
334+
for (int i = 0; i < n; i++) {
335+
states[i] = State.UNKNOWN;
336+
}
337+
338+
List<Integer> result = new ArrayList<>();
339+
for (int i = 0; i < n; i++) {
340+
// if node is with SAFE state, add to result
341+
if (dfs(graph, i, states) == State.SAFE) {
342+
result.add(i);
343+
}
344+
}
345+
return result;
346+
}
347+
348+
private enum State {
349+
UNKNOWN, VISITING, SAFE, UNSAFE
350+
}
351+
352+
private State dfs(int[][] graph, int node, State[] states) {
353+
/**
354+
* NOTE !!!
355+
* if a node with "VISITING" state,
356+
* but is visited again (within the other iteration)
357+
* -> there must be a cycle
358+
* -> this node is UNSAFE
359+
*/
360+
if (states[node] == State.VISITING) {
361+
return states[node] = State.UNSAFE;
362+
}
363+
/**
364+
* NOTE !!!
365+
* if a node is not with "UNKNOWN" state,
366+
* -> update its state
367+
*/
368+
if (states[node] != State.UNKNOWN) {
369+
return states[node];
370+
}
371+
372+
/**
373+
* NOTE !!!
374+
* update node state as VISITING
375+
*/
376+
states[node] = State.VISITING;
377+
for (int next : graph[node]) {
378+
/**
379+
* NOTE !!!
380+
* for every sub node, if any one them
381+
* has UNSAFE state,
382+
* -> set and return node state as UNSAFE directly
383+
*/
384+
if (dfs(graph, next, states) == State.UNSAFE) {
385+
return states[node] = State.UNSAFE;
386+
}
387+
}
388+
389+
/**
390+
* NOTE !!!
391+
* if can pass all above checks
392+
* -> this is node has SAFE state
393+
*/
394+
return states[node] = State.SAFE;
395+
}
318396
```

doc/pic/lc_802.png

1.17 MB
Loading

leetcode_java/src/main/java/LeetCodeJava/DFS/FindEventualSafeStates.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,79 @@ public class FindEventualSafeStates {
6565
//
6666
// }
6767

68+
// V1-0
69+
// IDEA : DFS
70+
// KEY : check if there is a "cycle" on a node
71+
// https://www.youtube.com/watch?v=v5Ni_3bHjzk
72+
// https://zxi.mytechroad.com/blog/graph/leetcode-802-find-eventual-safe-states/
73+
// https://github.com/yennanliu/CS_basics/tree/master/doc/pic/lc_802.png
74+
public List<Integer> eventualSafeNodes(int[][] graph) {
75+
// init
76+
int n = graph.length;
77+
State[] states = new State[n];
78+
for (int i = 0; i < n; i++) {
79+
states[i] = State.UNKNOWN;
80+
}
81+
82+
List<Integer> result = new ArrayList<>();
83+
for (int i = 0; i < n; i++) {
84+
// if node is with SAFE state, add to result
85+
if (dfs(graph, i, states) == State.SAFE) {
86+
result.add(i);
87+
}
88+
}
89+
return result;
90+
}
91+
92+
private enum State {
93+
UNKNOWN, VISITING, SAFE, UNSAFE
94+
}
95+
96+
private State dfs(int[][] graph, int node, State[] states) {
97+
/**
98+
* NOTE !!!
99+
* if a node with "VISITING" state,
100+
* but is visited again (within the other iteration)
101+
* -> there must be a cycle
102+
* -> this node is UNSAFE
103+
*/
104+
if (states[node] == State.VISITING) {
105+
return states[node] = State.UNSAFE;
106+
}
107+
/**
108+
* NOTE !!!
109+
* if a node is not with "UNKNOWN" state,
110+
* -> update its state
111+
*/
112+
if (states[node] != State.UNKNOWN) {
113+
return states[node];
114+
}
115+
116+
/**
117+
* NOTE !!!
118+
* update node state as VISITING
119+
*/
120+
states[node] = State.VISITING;
121+
for (int next : graph[node]) {
122+
/**
123+
* NOTE !!!
124+
* for every sub node, if any one them
125+
* has UNSAFE state,
126+
* -> set and return node state as UNSAFE directly
127+
*/
128+
if (dfs(graph, next, states) == State.UNSAFE) {
129+
return states[node] = State.UNSAFE;
130+
}
131+
}
132+
133+
/**
134+
* NOTE !!!
135+
* if can pass all above checks
136+
* -> this is node has SAFE state
137+
*/
138+
return states[node] = State.SAFE;
139+
}
140+
68141
// V1-1
69142
// https://leetcode.com/problems/find-eventual-safe-states/editorial/
70143
// IDEA : DFS

0 commit comments

Comments
 (0)