LeetCode Course Schedule II
2015-10-11 04:56
357 查看
原题链接在这里:https://leetcode.com/problems/course-schedule-ii/
题目:
There are a total of n courses you have to take, labeled from
Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair:
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:
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
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
题解:
与Course Schedule类似。用BFS based topological sort. 若是最后res的size 没有加到 numCourses, 说明没有可行方案,返回空的array.
若是可行,就把res转化成array.
Time Complexity: O(V + E). Space: O(V).
AC Java:
题目:
There are a total of n courses you have to take, labeled from
0to
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 Schedule类似。用BFS based topological sort. 若是最后res的size 没有加到 numCourses, 说明没有可行方案,返回空的array.
若是可行,就把res转化成array.
Time Complexity: O(V + E). Space: O(V).
AC Java:
public class Solution { public int[] findOrder(int numCourses, int[][] prerequisites) { List<Integer> res = new ArrayList<Integer>(); List<List<Integer>> graph = new ArrayList<List<Integer>>(); for(int i = 0; i<numCourses; i++){ graph.add(new ArrayList<Integer>()); } for(int [] edge : prerequisites){ graph.get(edge[1]).add(edge[0]); } int [] inDegree = new int[numCourses]; for(int [] edge : prerequisites){ inDegree[edge[0]]++; } LinkedList<Integer> que = new LinkedList<Integer>(); for(int i = 0; i<inDegree.length; i++){ if(inDegree[i] == 0){ que.add(i); } } while(!que.isEmpty()){ int source = que.poll(); res.add(source); for(int dest : graph.get(source)){ inDegree[dest]--; if(inDegree[dest] == 0){ que.add(dest); } } } if(res.size() != numCourses){ return new int[0]; } int [] resArr = new int[numCourses]; for(int i = 0; i<numCourses; i++){ resArr[i] = res.get(i); } return resArr; } }
相关文章推荐
- Android ViewPager前两页显示空白
- Container With Most Water
- 84款 数据备份开源软件
- Rails读书笔记第二章
- 合并子目录所有文本文件的批处理
- excel减肥过滤广告
- excel 宏(VB)实现复制一列并按第二列的单元格长度排序
- Rails读书笔记第一章
- grunt快速上手(转)
- txt文档内容入mysql
- 一次熬夜解决的java乱码问题
- 3DMAX中CryEngine导出插件的配置方法
- 转基因食品与我国《食品安全法》
- 信息安全系统设计基础第四周学习总结
- ARX程序的升级及多版本编译须知
- 逆向破解学习二之<TraceMe>
- PHP第一季视频教程.李炎恢.学习笔记(三)(第2章 基本语法(2))
- Leetcode Reverse Words in a String II
- 12家人均创收最高的科技公司
- 【资源共享】香瓜的常用开发资源