Middle-题目86:207. Course Schedule

题目原文:
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) 从网中删除此顶点及所有出边。
循环结束后,若输出的顶点数小于网中的顶点数,则输出“有回路”信息,否则输出的顶点序列就是一种拓扑序列。
而课程先修(或一个工程中各个子工程先后完成关系)是拓扑排序中最基本的问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值