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]
.Note:
The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
A:
仍然用了BSF
---------------初始解法-----------把constrain放到List中,以便删除---------
public class Solution { public int[] findOrder(int numCourses, int[][] prerequisites) { int n = prerequisites.length; int result[] = new int[numCourses]; int lastIndex = 0; List<int[]> list = new LinkedList(); for(int i =0;i<n;i++) list.add(new int[]{prerequisites[i][0],prerequisites[i][1]}); Set<Integer> findSet = new HashSet(); boolean isFoundOne = true; while(isFoundOne && lastIndex < numCourses){ isFoundOne = false; for(int i = 0;i<numCourses;i++){ if(findSet.contains(i)) continue; boolean hasConstrain = false; for(int j = 0;j<list.size();j++){ int[] tmp = list.get(j); if(findSet.contains(tmp[1])){ list.remove(j); j--; continue; }else if(tmp[0] == i){ hasConstrain = true; break; } } if( ! hasConstrain){ findSet.add(i); result[lastIndex++] = i; isFoundOne = true; break; } } } if( lastIndex == numCourses) return result; else return new int[]{}; } }
上面的解法超时。原因是不需要每次遍历所有的constrain,而只需要查看相应 class i 的constrain即可。
*************下面 是雷同于 I 的解法*************
public class Solution { public int[] findOrder(int numCourses, int[][] prerequisites) { int result[] = new int[numCourses]; int lastIndex = 0; Map<Integer, List<Integer>> map = new HashMap(); for(int i = 0;i<numCourses;i++) map.put(i, new LinkedList<Integer>()); for(int i =0;i<prerequisites.length;i++) map.get(prerequisites[i][0]).add(prerequisites[i][1]); Set<Integer> findSet = new HashSet(); boolean isFoundOne = true; while(isFoundOne && lastIndex < numCourses){ isFoundOne = false; for(Integer i : map.keySet()){ List<Integer> constrainList = map.get(i); for(int j = 0;j< constrainList.size();j++){ if(findSet.contains(constrainList.get(j))){ constrainList.remove(j); j--; }else break; } if( constrainList.isEmpty() ){ findSet.add(i); map.remove(i); result[lastIndex++] = i; isFoundOne = true; break; } } } return lastIndex == numCourses? result: new int[]{}; } }
Mistakes:
1: 上面的第二个解法中,忘记 map.remove(i)了。
No comments:
Post a Comment