• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
neverlandly
博客园    首页    新随笔    联系   管理    订阅  订阅

Leetcode: Course Schedule II

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].

第二遍做法:Build a graph

 1 public class Solution {
 2     public int[] findOrder(int numCourses, int[][] prerequisites) {
 3         int[] result = new int[numCourses];        
 4         if (numCourses <= 0) return new int[0];
 5         
 6         ArrayList<ArrayList<Integer>> graph = new ArrayList<ArrayList<Integer>>();
 7         for (int i=0; i<numCourses; i++) graph.add(new ArrayList<Integer>());
 8         
 9         int len = prerequisites.length;
10         int[] indegree = new int[numCourses];
11         for (int i=0; i<len; i++) {
12             indegree[prerequisites[i][0]]++;
13             graph.get(prerequisites[i][1]).add(prerequisites[i][0]);
14         }
15         Queue<Integer> q = new LinkedList<Integer>();
16         for (int i=0; i<numCourses; i++) {
17             if (indegree[i] == 0) {
18                 q.offer(i);
19             }
20         }
21         int count = 0;
22         while (!q.isEmpty()) {
23             int cur = q.poll();
24             result[count++] = cur;
25             ArrayList<Integer> neibor = graph.get(cur);
26             for (int t=0; t<neibor.size(); t++) {
27                 indegree[neibor.get(t)]--;
28                 if (indegree[neibor.get(t)] == 0) {
29                     q.offer(neibor.get(t));
30                 } 
31             }
32         }
33         //if cycle exists
34         if (count != numCourses) return new int[0];
35         return result;
36     }
37 }

Because we just need one feasible topological sorting of all the courses, we can simply denote each course while we do BFS.

 1 public class Solution {
 2     public int[] findOrder(int numCourses, int[][] prerequisites) {
 3         int[] result = new int[numCourses];        
 4         if (numCourses <= 0) return new int[0];
 5         
 6         int len = prerequisites.length;
 7         int[] countPre = new int[numCourses];
 8         for (int i=0; i<len; i++) {
 9             countPre[prerequisites[i][0]]++;
10         }
11         Queue<Integer> q = new LinkedList<Integer>();
12         for (int i=0; i<numCourses; i++) {
13             if (countPre[i] == 0) {
14                 q.offer(i);
15             }
16         }
17         int count = 0;
18         while (!q.isEmpty()) {
19             int cur = q.poll();
20             result[count++] = cur;
21             for (int i=0; i<len; i++) {
22                 if (prerequisites[i][1] == cur) {
23                     countPre[prerequisites[i][0]]--;
24                     if (countPre[prerequisites[i][0]] == 0) {
25                         q.offer(prerequisites[i][0]);
26                     }
27                 }
28             }
29         }
30         //if cycle exists
31         if (count != numCourses) return new int[0];
32         return result;
33     }
34 }

 

posted @ 2015-12-16 12:25  neverlandly  阅读(318)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3