Skip to content

Commit

Permalink
Time: 4 ms (91.62%), Space: 45.4 MB (77.67%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipendra-Raghav committed May 10, 2024
1 parent a208851 commit 8f4b68c
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions 0210-course-schedule-ii/0210-course-schedule-ii.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Solution {
public boolean dfs(int i,List<List<Integer>> adj,boolean[] vis,boolean[] path,Stack<Integer> st)
{
vis[i]=true;
path[i]=true;
for(int x:adj.get(i)){
if(!vis[x])
{if(dfs(x,adj,vis,path,st))
return true;
}
else if(path[x])
return true;
}
path[i]=false;
st.push(i);
return false;
}

public int[] findOrder(int n, int[][] pre) {
List<List<Integer>> adj=new ArrayList<>();
boolean vis[]=new boolean[n];
boolean path[]=new boolean[n];
Stack<Integer> st=new Stack<>();
int[] ans=new int[n];
for(int i=0;i<n;i++)
adj.add(new ArrayList<>());
for(int i=0;i<pre.length;i++)
adj.get(pre[i][1]).add(pre[i][0]);

for(int i=0;i<n;i++){
if(!vis[i]){
if(dfs(i,adj,vis,path,st))
return new int[0];
}
}
int i=0;
while(!st.isEmpty())
ans[i++]=st.pop();
return ans;
}
}

0 comments on commit 8f4b68c

Please sign in to comment.