Skip to content

Commit 7947894

Browse files
committed
update
1 parent f39be36 commit 7947894

File tree

1 file changed

+90
-36
lines changed

1 file changed

+90
-36
lines changed

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

Lines changed: 90 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2277,58 +2277,112 @@ public boolean isNStraightHand(int[] hand, int groupSize) {
22772277
}
22782278

22792279
// LC 802
2280-
// 6.15 pm - 6.30
2280+
// 8.36 pm - 8.50 pm
22812281
// https://leetcode.com/problems/find-eventual-safe-states/
2282-
// dfs
2283-
List<String> states = new ArrayList<>();
2282+
// topological sort
22842283

2285-
public List<Integer> eventualSafeNodes(int[][] graph) {
2284+
List<Integer> states = new ArrayList<>();
22862285

2287-
for (int i = 0; i < graph.length; i++){
2288-
states.add("UNKNOWN");
2289-
}
2286+
public List<Integer> eventualSafeNodes(int[][] graph) {
22902287

2291-
if (graph.length==0){
2292-
return null;
2293-
}
2288+
return null;
2289+
}
22942290

2295-
List<Integer> res = new ArrayList<>();
2291+
private List<Integer> topoSort_2(int size, int[][] graph){
22962292

2297-
for (int i = 0; i < graph.length; i++){
2298-
if (isSafe(i, graph)){
2299-
res.add(i);
2293+
// init
2294+
Map<Integer, List<Integer>> map = new HashMap<>();
2295+
int[] degree = new int[graph.length]; // ??
2296+
2297+
for (int[] x : graph){
2298+
int prev = x[1];
2299+
int next = x[0];
2300+
if(!map.containsKey(prev)){
2301+
degree[prev] = 0;
2302+
map.put(prev, new ArrayList<>());
2303+
}else{
2304+
degree[prev] += 1; // ??
2305+
List<Integer> curList = map.get(prev);
2306+
curList.add(next);
2307+
map.put(prev, curList);
23002308
}
23012309
}
23022310

2303-
return res;
2304-
}
2305-
2306-
// 4 states : UNKNOWN, VISITING, SAFE, UNSAFE
2307-
private boolean isSafe(int idx, int[][] graph){
2308-
String curState = states.get(idx);
2309-
if (curState.equals("SAFE")){
2310-
return true;
2311-
}
2312-
if (curState.equals("VISITING")){
2313-
return false;
2311+
Queue<Integer> queue = new LinkedList<>();
2312+
for(int y : degree){
2313+
if(y == 0){
2314+
queue.add(y);
2315+
}
23142316
}
2315-
// TODO : check
2316-
// if (!curState.equals("UNKNOWN")){
2317-
// return curState.equals("SAFE"); // ??
2318-
// }
2319-
2320-
curState = "VISITING";
23212317

2322-
for (int j = 0; j < graph.length; j++){
2323-
if (!this.isSafe(j, graph)){
2324-
return false;
2318+
while(!queue.isEmpty()){
2319+
int curVal = queue.poll();
2320+
states.add(curVal); // ??
2321+
for(Integer x : map.get(curVal)){
2322+
degree[x] -= 1;
2323+
if (degree[x] == 0){
2324+
queue.add(x);
2325+
}
23252326
}
23262327
}
23272328

2328-
//curState =
2329-
return true; // ??? curState.equals("SAFE"); // ??
2329+
if (states.size() != size){
2330+
throw new RuntimeException("Input can't be sorted by topological sorting");
2331+
}
2332+
2333+
return states;
23302334
}
23312335

2336+
2337+
// List<String> states = new ArrayList<>();
2338+
//
2339+
// public List<Integer> eventualSafeNodes(int[][] graph) {
2340+
//
2341+
// for (int i = 0; i < graph.length; i++){
2342+
// states.add("UNKNOWN");
2343+
// }
2344+
//
2345+
// if (graph.length==0){
2346+
// return null;
2347+
// }
2348+
//
2349+
// List<Integer> res = new ArrayList<>();
2350+
//
2351+
// for (int i = 0; i < graph.length; i++){
2352+
// if (isSafe(i, graph)){
2353+
// res.add(i);
2354+
// }
2355+
// }
2356+
//
2357+
// return res;
2358+
// }
2359+
//
2360+
// // 4 states : UNKNOWN, VISITING, SAFE, UNSAFE
2361+
// private boolean isSafe(int idx, int[][] graph){
2362+
// String curState = states.get(idx);
2363+
// if (curState.equals("SAFE")){
2364+
// return true;
2365+
// }
2366+
// if (curState.equals("VISITING")){
2367+
// return false;
2368+
// }
2369+
// // TODO : check
2370+
//// if (!curState.equals("UNKNOWN")){
2371+
//// return curState.equals("SAFE"); // ??
2372+
//// }
2373+
//
2374+
// curState = "VISITING";
2375+
//
2376+
// for (int j = 0; j < graph.length; j++){
2377+
// if (!this.isSafe(j, graph)){
2378+
// return false;
2379+
// }
2380+
// }
2381+
//
2382+
// //curState =
2383+
// return true; // ??? curState.equals("SAFE"); // ??
2384+
// }
2385+
23322386
// LC 1197
23332387
// https://leetcode.ca/all/1197.html
23342388
// 6.25 pm - 6.40 pm

0 commit comments

Comments
 (0)