• 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 }
  • 相关阅读:
    angular4(2-1)angular脚手架引入第三方类库(jquery)
    angular4(1)angular脚手架
    vue-cli中的ESlint配置文件eslintrc.js详解
    咕着的题(慢慢补吧)
    图解Js event对象offsetX, clientX, pageX, screenX, layerX, x区别
    乐视手机H5项目总结
    解决ios下的微信打开的页面背景音乐无法自动播放
    html2canvas手机端模糊问题
    H5 canvas绘制出现模糊的问题
    hammer.js中文文档
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/5050779.html
Copyright © 2020-2023  润新知