Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 27, 2024
1 parent f39be36 commit 7947894
Showing 1 changed file with 90 additions and 36 deletions.
126 changes: 90 additions & 36 deletions leetcode_java/src/main/java/dev/workspace5.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> states = new ArrayList<>();
// topological sort

public List<Integer> eventualSafeNodes(int[][] graph) {
List<Integer> states = new ArrayList<>();

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

if (graph.length==0){
return null;
}
return null;
}

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

for (int i = 0; i < graph.length; i++){
if (isSafe(i, graph)){
res.add(i);
// init
Map<Integer, List<Integer>> 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<Integer> 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<Integer> 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<String> states = new ArrayList<>();
//
// public List<Integer> eventualSafeNodes(int[][] graph) {
//
// for (int i = 0; i < graph.length; i++){
// states.add("UNKNOWN");
// }
//
// if (graph.length==0){
// return null;
// }
//
// List<Integer> 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
Expand Down

0 comments on commit 7947894

Please sign in to comment.