Skip to content

Commit 263cd64

Browse files
committed
update 802 java
1 parent 47bb09d commit 263cd64

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ public class FindEventualSafeStates {
7474
public List<Integer> eventualSafeNodes(int[][] graph) {
7575
// init
7676
int n = graph.length;
77+
/**
78+
* NOTE !!!
79+
*
80+
* we init status array (states)
81+
* and set all init value as "UNKNOWN"
82+
*/
7783
State[] states = new State[n];
7884
for (int i = 0; i < n; i++) {
7985
states[i] = State.UNKNOWN;
@@ -82,6 +88,11 @@ public List<Integer> eventualSafeNodes(int[][] graph) {
8288
List<Integer> result = new ArrayList<>();
8389
for (int i = 0; i < n; i++) {
8490
// if node is with SAFE state, add to result
91+
/**
92+
* NOTE !!!
93+
*
94+
* the dfs parameter: we use index, state array as well
95+
*/
8596
if (dfs(graph, i, states) == State.SAFE) {
8697
result.add(i);
8798
}
@@ -100,6 +111,12 @@ private State dfs(int[][] graph, int node, State[] states) {
100111
* but is visited again (within the other iteration)
101112
* -> there must be a cycle
102113
* -> this node is UNSAFE
114+
*
115+
*
116+
* NOTE !!!
117+
*
118+
* we DON'T compare current status,
119+
* but compare state (states[node]) via index, and enum
103120
*/
104121
if (states[node] == State.VISITING) {
105122
return states[node] = State.UNSAFE;
@@ -108,6 +125,10 @@ private State dfs(int[][] graph, int node, State[] states) {
108125
* NOTE !!!
109126
* if a node is not with "UNKNOWN" state,
110127
* -> update its state
128+
*
129+
* NOTE !!!
130+
* we DON'T compare current status,
131+
* but compare state (states[node]) via index, and enum
111132
*/
112133
if (states[node] != State.UNKNOWN) {
113134
return states[node];
@@ -121,7 +142,7 @@ private State dfs(int[][] graph, int node, State[] states) {
121142
for (int next : graph[node]) {
122143
/**
123144
* NOTE !!!
124-
* for every sub node, if any one them
145+
* for every sub node, if any of them
125146
* has UNSAFE state,
126147
* -> set and return node state as UNSAFE directly
127148
*/

leetcode_java/src/main/java/dev/workspace5.java

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2201,11 +2201,55 @@ public boolean isNStraightHand(int[] hand, int groupSize) {
22012201
}
22022202

22032203
// LC 802
2204-
// 5,28 - 5.40 PM
2204+
// 6.15 pm - 6.30
22052205
// https://leetcode.com/problems/find-eventual-safe-states/
2206+
// dfs
2207+
List<String> states = new ArrayList<>();
22062208
public List<Integer> eventualSafeNodes(int[][] graph) {
22072209

2208-
return null;
2210+
for (int i = 0; i < graph.length; i++){
2211+
states.add("UNKNOWN");
2212+
}
2213+
2214+
if (graph.length==0){
2215+
return null;
2216+
}
2217+
2218+
List<Integer> res = new ArrayList<>();
2219+
2220+
for (int i = 0; i < graph.length; i++){
2221+
if (isSafe(i, graph)){
2222+
res.add(i);
2223+
}
2224+
}
2225+
2226+
return res;
2227+
}
2228+
2229+
// 4 states : UNKNOWN, VISITING, SAFE, UNSAFE
2230+
private boolean isSafe(int idx, int[][] graph){
2231+
String curState = states.get(idx);
2232+
if (curState.equals("SAFE")){
2233+
return true;
2234+
}
2235+
if (curState.equals("VISITING")){
2236+
return false;
2237+
}
2238+
// TODO : check
2239+
// if (!curState.equals("UNKNOWN")){
2240+
// return curState.equals("SAFE"); // ??
2241+
// }
2242+
2243+
curState = "VISITING";
2244+
2245+
for (int j = 0; j < graph.length; j++){
2246+
if (!this.isSafe(j, graph)){
2247+
return false;
2248+
}
2249+
}
2250+
2251+
//curState =
2252+
return true; // ??? curState.equals("SAFE"); // ??
22092253
}
22102254

22112255
// LC 1197

0 commit comments

Comments
 (0)