Course Schedule II
题目地址:
https://leetcode.com/problems/course-schedule-ii/description/
题目:
解题思路:
这道题可以借鉴course schedule1来做。
代码:
revised version of BFS get TLE:
improved version of BFS:
https://leetcode.com/problems/course-schedule-ii/description/
题目:
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.
For example:
2, [[1,0]]
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]4, [[1,0],[2,0],[3,1],[3,2]]
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].解题思路:
这道题可以借鉴course schedule1来做。
代码:
revised version of BFS get TLE:
public int[] findOrder(int numCourses, int[][] prerequisites) { int[] rst = new int[numCourses]; if(numCourses == 0 || prerequisites == null){ return rst; } if(numCourses == 1 && prerequisites.length == 0){ return rst; } Map<Integer, Set<Integer>> graph = new HashMap<>(); for(int i = 0 ; i <= numCourses - 1; i++){ graph.put(i, new HashSet<>()); } // put prerequisite into the graph for(int i = 0; i <= prerequisites.length - 1; i++){ int curr = prerequisites[i][0]; int pre = prerequisites[i][1]; graph.get(curr).add(pre); } int[] record = new int[numCourses]; // update the rst array for(int curr : graph.keySet()){ record[curr] = graph.get(curr).size(); } int index = 0; Queue<Integer> queue = new LinkedList<>(); for(int i = 0; i <= numCourses - 1; i++){ if(graph.get(i). size() == 0){ queue.offer(i); } } // start bfs here while(!queue.isEmpty()){ int pre = queue.poll(); if(index <= numCourses - 1){ rst[index++] = pre; } for(int i = 0; i <= numCourses - 1; i++){ if(graph.get(i).contains(pre)){ graph.get(i).remove(pre); if(graph.get(i).size() == 0){ record[i] = 0; queue.offer(i); } } } } int count = 0; for(int i = 0; i <= numCourses - 1; i++){ if(record[i] == 0){ count++; } } if(count == numCourses){ return rst; } return new int[0]; }
improved version of BFS:
public int[] findOrder(int numCourses, int[][] prerequisites) { int[] rst = new int[numCourses]; if(numCourses == 0 || prerequisites == null){ return rst; } if(numCourses == 1 && prerequisites.length == 0){ return rst; } Map<Integer, Set<Integer>> graph = new HashMap<>(); for(int i = 0 ; i <= numCourses - 1; i++){ graph.put(i, new HashSet<>()); } // put prerequisite into the graph for(int i = 0; i <= prerequisites.length - 1; i++){ int curr = prerequisites[i][0]; int pre = prerequisites[i][1]; graph.get(curr).add(pre); } int[] record = new int[numCourses]; // update the rst array for(int curr : graph.keySet()){ record[curr] = graph.get(curr).size(); } int index = 0; Queue<Integer> queue = new LinkedList<>(); for(int i = 0; i <= numCourses - 1; i++){ if(graph.get(i). size() == 0){ queue.offer(i); } } // start bfs here while(!queue.isEmpty()){ int pre = queue.poll(); if(index <= numCourses - 1){ rst[index++] = pre; } for(int i = 0; i <= numCourses - 1; i++){ if(record[i] == 0){ continue; } if(graph.get(i).contains(pre)){ graph.get(i).remove(pre); if(graph.get(i).size() == 0){ record[i] = 0; queue.offer(i); } } } } int count = 0; for(int i = 0; i <= numCourses - 1; i++){ if(record[i] == 0){ count++; } } if(count == numCourses){ return rst; } return new int[0]; } public static void main(String[] args){ CourseSchedule2 courseSchedule2 = new CourseSchedule2(); int[][] nums = new int[1][2]; int[] temp = new int[2]; temp[0] = 1; temp[1] = 0; nums[0] = temp; int[] rst = courseSchedule2.findOrder(2, nums); System.out.println(Arrays.toString(rst)); }

Comments
Post a Comment