diff --git a/leetcode_java/src/main/java/dev/workspace5.java b/leetcode_java/src/main/java/dev/workspace5.java index 9bad95fb..1778e281 100644 --- a/leetcode_java/src/main/java/dev/workspace5.java +++ b/leetcode_java/src/main/java/dev/workspace5.java @@ -2277,58 +2277,112 @@ public boolean isNStraightHand(int[] hand, int groupSize) { } // LC 802 - // 6.15 pm - 6.30 + // 8.36 pm - 8.50 pm // https://leetcode.com/problems/find-eventual-safe-states/ - // dfs - List states = new ArrayList<>(); + // topological sort - public List eventualSafeNodes(int[][] graph) { + List states = new ArrayList<>(); - for (int i = 0; i < graph.length; i++){ - states.add("UNKNOWN"); - } + public List eventualSafeNodes(int[][] graph) { - if (graph.length==0){ - return null; - } + return null; + } - List res = new ArrayList<>(); + private List topoSort_2(int size, int[][] graph){ - for (int i = 0; i < graph.length; i++){ - if (isSafe(i, graph)){ - res.add(i); + // init + Map> map = new HashMap<>(); + int[] degree = new int[graph.length]; // ?? + + for (int[] x : graph){ + int prev = x[1]; + int next = x[0]; + if(!map.containsKey(prev)){ + degree[prev] = 0; + map.put(prev, new ArrayList<>()); + }else{ + degree[prev] += 1; // ?? + List curList = map.get(prev); + curList.add(next); + map.put(prev, curList); } } - return res; - } - - // 4 states : UNKNOWN, VISITING, SAFE, UNSAFE - private boolean isSafe(int idx, int[][] graph){ - String curState = states.get(idx); - if (curState.equals("SAFE")){ - return true; - } - if (curState.equals("VISITING")){ - return false; + Queue queue = new LinkedList<>(); + for(int y : degree){ + if(y == 0){ + queue.add(y); + } } - // TODO : check -// if (!curState.equals("UNKNOWN")){ -// return curState.equals("SAFE"); // ?? -// } - - curState = "VISITING"; - for (int j = 0; j < graph.length; j++){ - if (!this.isSafe(j, graph)){ - return false; + while(!queue.isEmpty()){ + int curVal = queue.poll(); + states.add(curVal); // ?? + for(Integer x : map.get(curVal)){ + degree[x] -= 1; + if (degree[x] == 0){ + queue.add(x); + } } } - //curState = - return true; // ??? curState.equals("SAFE"); // ?? + if (states.size() != size){ + throw new RuntimeException("Input can't be sorted by topological sorting"); + } + + return states; } + +// List states = new ArrayList<>(); +// +// public List eventualSafeNodes(int[][] graph) { +// +// for (int i = 0; i < graph.length; i++){ +// states.add("UNKNOWN"); +// } +// +// if (graph.length==0){ +// return null; +// } +// +// List res = new ArrayList<>(); +// +// for (int i = 0; i < graph.length; i++){ +// if (isSafe(i, graph)){ +// res.add(i); +// } +// } +// +// return res; +// } +// +// // 4 states : UNKNOWN, VISITING, SAFE, UNSAFE +// private boolean isSafe(int idx, int[][] graph){ +// String curState = states.get(idx); +// if (curState.equals("SAFE")){ +// return true; +// } +// if (curState.equals("VISITING")){ +// return false; +// } +// // TODO : check +//// if (!curState.equals("UNKNOWN")){ +//// return curState.equals("SAFE"); // ?? +//// } +// +// curState = "VISITING"; +// +// for (int j = 0; j < graph.length; j++){ +// if (!this.isSafe(j, graph)){ +// return false; +// } +// } +// +// //curState = +// return true; // ??? curState.equals("SAFE"); // ?? +// } + // LC 1197 // https://leetcode.ca/all/1197.html // 6.25 pm - 6.40 pm