Middle-题目86:207. Course Schedule

原创 2016年05月31日 17:20:14

题目原文:
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, is it possible for you to finish all courses?
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 it is possible.

2, [[1,0],[0,1]]
There are a total of 2 courses to take. To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.
题目大意:
你要上n门课,课的编号分别是0,…n-1.
有一些课程有先修课,例如要学习课程0需先学习课程1,这样用一个有序数对[0,1]表示。
给出课程的数目和先修课程的有序数对列表,判断是否能修完所有课程。
题目分析:
这也是一道关于图的问题。用有向图来表示课程的先修关系,例如数对[0.1]对应有向图中0->1的边。判断是否存在拓扑序列即可。
源码:(language:java)

public class Solution {
    public  boolean canFinish(int numCourses, int[][] prerequisites) {
        Map<Integer, Set<Integer>> adjList = new HashMap<Integer,Set<Integer>>();
        int[] indegree = new int[numCourses];
        for(int i = 0; i<numCourses;i++)
            adjList.put(i, new HashSet<Integer>());
        for(int[] edge : prerequisites) {
            if(!adjList.get(edge[0]).contains(edge[1])) {
                adjList.get(edge[0]).add(edge[1]);
                indegree[edge[1]]++;
            }
        }
        int topoVertical = findZeroIndegree(indegree);
        while(topoVertical!=-1) {
            indegree[topoVertical] = -1; // -1 stands for visited
            for(Integer vertical : adjList.get(topoVertical)) {
                indegree[vertical]--;
            }
            adjList.remove(topoVertical);
            topoVertical = findZeroIndegree(indegree);
        }       
        return isAllVisited(indegree);
    }
    private int findZeroIndegree(int[] indegree) {
        for(int i = 0;i<indegree.length;i++)
            if(indegree[i]==0)
                return i;
        return -1;
    }
    private boolean isAllVisited(int[] indegree) {
        for(int i : indegree)
            if(i!=-1)
                return false;
        return true;
    }
}

成绩:
24ms,beats 47.84%,众数12ms,6.33%
Cmershen的碎碎念:
首先复习拓扑排序的定义和算法:对一个有向无环图(Directed Acyclic Graph简称DAG)G进行拓扑排序,是将G中所有顶点排成一个线性序列,使得图中任意一对顶点u和v,若边(u,v)∈E(G),则u在线性序列中出现在v之前。通常,这样的线性序列称为满足拓扑次序(Topological Order)的序列,简称拓扑序列。
算法:
(1) 选择一个入度为0的顶点并输出之;
(2) 从网中删除此顶点及所有出边。
循环结束后,若输出的顶点数小于网中的顶点数,则输出“有回路”信息,否则输出的顶点序列就是一种拓扑序列。
而课程先修(或一个工程中各个子工程先后完成关系)是拓扑排序中最基本的问题。

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

LeetCode-207/210. Course Schedule (JAVA) (课程安排)

LeetCode-207/210. Course Schedule (JAVA) (课程安排)

【Leetcode】207. Course Schedule

Description:There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses ...

207 Course Schedule

//WA 开始以为同一门课只有一门前置, class Solution { public: bool canFinish(int numCourses, vector& prerequisit...

【LeetCode】207. Course Schedule

题目There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have pre...

算法:LeetCode207 Course Schedule

题目There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have pre...

leetcode-207. Course Schedule

There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have...
  • qj30212
  • qj30212
  • 2016年08月30日 23:15
  • 136

LeetCode 207 Course Schedule

题目描述There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have p...

LeetCode 207.Course Schedule

description: There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses ...

[LeetCode] 207. Course Schedule

LeetCode dfs bfs

207&210. Course Schedule

一、题目简述 共有nn门课需要选择,使用0……n−10……n-1进行标记。其中部分课程存在依赖关系,例如课程0必须在修学过课程1的基础上可以上,使用对[0,1][0,1]表示。 给定课程数量和依赖关系...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Middle-题目86:207. Course Schedule
举报原因:
原因补充:

(最多只允许输入30个字)