zoukankan      html  css  js  c++  java
  • lintcode616- Course Schedule II- medium

    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.

    Example

    Given n = 2, prerequisites = [[1,0]]
    Return [0,1]

    Given n = 4, prerequisites = [1,0],[2,0],[3,1],[3,2]]
    Return [0,1,2,3] or [0,2,1,3]

    和前面一题一模一样的做法,只不过直接把得到的拓扑序返回即可。还有ArrayList.add()改为数组里的添加只要用一个int pointer辅助即可。

    public class Solution {
        /*
         * @param numCourses: a total of n courses
         * @param prerequisites: a list of prerequisite pairs
         * @return: the course order
         */
        public int[] findOrder(int numCourses, int[][] prerequisites) {
            // write your code here
            int[] order = new int[numCourses];
            int pointer = 0;
            int[] indegrees = new int[numCourses];
            List[] edges = new List[numCourses];
            
            for (int i = 0; i < numCourses; i++) {
                indegrees[i] = 0;
                edges[i] = new ArrayList<Integer>();
            }
            
            for (int i = 0; i < prerequisites.length; i++) {
                int c1 = prerequisites[i][0];
                int c2 = prerequisites[i][1];
                indegrees[c1]++;
                edges[c2].add(c1);
            }
            
            Queue<Integer> queue = new LinkedList<Integer>();
            Set<Integer> set = new HashSet<Integer>();
            for (int i = 0; i < numCourses; i++) {
                if (indegrees[i] == 0) {
                    queue.offer(i);
                    set.add(i);
                }
            }
            while (!queue.isEmpty()) {
                int course = queue.poll();
                order[pointer++] = course;
                for (int i = 0; i < edges[course].size(); i++) {
                    int next = (int) edges[course].get(i);
                    indegrees[next]--;
                    if (indegrees[next] == 0) {
                        queue.offer(next);
                        set.add(next);
                    }
                }
            }
            
            if (pointer == numCourses) {
                return order;
            }
            return new int[0];
        }
    }
  • 相关阅读:
    安装HyperV后VirtualBox打开故障
    TortoiseGit或TortoiseSVN在软件或系统更新后图标丢失的一个解决办法
    WSL2 Ubuntu1604 安装 GUI图形库 和 Qt Creator
    时序约束(小梅哥)
    FPGA加速
    吴恩达机器学习笔记
    Neural Network and Deep Learning 笔记【第二章;反向传播算法如何⼯作】
    Tcl学习记录
    Neural Network and Deep Learning 笔记【第一章;手写数字识别】
    操作符重载
  • 原文地址:https://www.cnblogs.com/jasminemzy/p/7734869.html
Copyright © 2011-2022 走看看