210. Course Schedule II dfs
210. Course Schedule II https://www.youtube.com/watch?v=Qqgck2ijUjU // dfs class Solution { public int[] findOrder(int numCourses, int[][] prerequisites) { List<List<Integer>> list = new ArrayList<>(); for(int i = 0; i < numCourses; i++){ list.add(new ArrayList<>()); } for(int i = 0; i < prerequisites.length; i++){ list.get(prerequisites[i][1]).add(prerequisites[i][0]); } int[] visited = new int[numCourses]; List<Integer> result = new ArrayList<>(); for(int i = 0; i < numCourses; i++){ if(dfs(i, list, result, visited)){ return new int[]{}; ////// } } Collections.reverse(result); int[] resultArray = toArray(result, numCourses); return resultArray; } private boolean dfs(int i, List<List<Integer>> list, List<Integer> result, int[] visited){ if(visited[i] == 1) return true; // 1 means visiting, found a cycle if(visited[i] == 2) return false; // 2 means visited, this path has no cycle visited[i] = 1; // this node was unexplored, initial default value is 0, now mark it as visiting, 1 // visitng its next class for(int nei: list.get(i)){ if(dfs(nei, list, result, visited)){ return true; // dfs backtracking if the future nodes found a cycle, returned true } } visited[i] = 2; // visited result.add(i); // the first node that is mark as visited is first one added to the list, more is added during backtracking return false; // this path doesn't have a cycle } private int[] toArray(List<Integer> result, int numCourses){ int[] resultArray = new int[numCourses]; for(int i = 0; i < numCourses; i++){ resultArray[i] = result.get(i); } return resultArray; } } // bfs class Solution { public int[] findOrder(int numCourses, int[][] prerequisites) { List<Integer> result = new ArrayList<>(); Queue<Integer> queue = new LinkedList<>(); List<List<Integer>> list = new ArrayList<>(); int[] indegree = new int[numCourses]; for(int i = 0; i < prerequisites.length; i++){ int course = prerequisites[i][0]; indegree[course]++; } for(int i = 0; i < numCourses; i++){ list.add(new ArrayList<>()); } for(int i = 0; i < prerequisites.length; i++){ list.get(prerequisites[i][1]).add(prerequisites[i][0]); } for(int i = 0; i < numCourses; i++){ if(indegree[i] == 0){ queue.offer(i); } } while(!queue.isEmpty()){ int cur = queue.poll(); result.add(cur); for(int nei: list.get(cur)){ indegree[nei]--; if(indegree[nei] == 0){ queue.offer(nei); } } } if(result.size() == numCourses){ int[] array = new int[numCourses]; for(int i = 0; i < numCourses; i++){ array[i] = result.get(i); } return array; } return new int[]{}; } }
There are a total of n courses you have to take, labeled from 0 to n-1.
Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]
Given the total number of courses and a list of prerequisite pairs, return the ordering of courses you should take to finish all courses.
There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.
Example 1:
Input: 2, [[1,0]] Output:[0,1]Explanation: There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is[0,1] .
Example 2:
Input: 4, [[1,0],[2,0],[3,1],[3,2]] Output:[0,1,2,3] or [0,2,1,3]Explanation: There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is[0,1,2,3]. Another correct ordering is[0,2,1,3] .
posted on 2018-08-09 17:51 猪猪🐷 阅读(147) 评论(0) 收藏 举报
浙公网安备 33010602011771号