Skip to main content

Posts

207. Course Schedule

My solution for leetcode problem called course schedule (problem no.207)  Depth first search Approach: Build adjacency list Perform depth first search on each node and check if cycle is found Within dfs for each node,  Check if already marked as visiting (-1) return false Check if visited in another iteration (1) return true If not, perform dfs on subsequent course nodes Reset to 1, to indicate visited nodes Return result Solution : class Solution { public boolean canFinish ( int numCourses , int [][] prerequisites ) { if ( numCourses <= 1 ) return true ; // build adjacency list Map < Integer , List < Integer >> map = new HashMap <>(); for ( int [] p: prerequisites ) { if (! map . containsKey ( p [ 1 ])) map . put ( p [ 1 ], new ArrayList < Integer >()); map . get ( p [ 1 ]). add ( p [ 0 ]); } int [] ...
Recent posts