leetcode 207. Course Schedule 课程调度 + 拓扑排序

来源:互联网 发布:c语言预处理指令格式 编辑:程序博客网 时间:2024/05/19 18:15

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.

这道题考察的就是拓扑排序,关键是图是怎么存储,怎么判断前驱节点,我在网上看到了一个很不错的做法,可以参考一下。

代码如下:

import java.util.ArrayList;import java.util.Arrays;import java.util.HashSet;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Queue;import java.util.Set;/* * 本质就是判断图中是否有环,拓扑排序的应用 * http://blog.csdn.net/ljiabin/article/details/45846837 * */public class Solution {    public boolean canFinish(int numCourses, int[][] prerequisites)     {        if(numCourses<=1 || prerequisites.length<=1)            return true;        int []preCount=new int[numCourses];        List<Set<Integer>> list=new ArrayList<Set<Integer>>();        Arrays.fill(preCount, 0);        for(int i=0;i<numCourses;i++)            list.add(new HashSet<>());        /*         * list保存的是出度边,也即后继结点         * 这里使用是为了避免重复的边         * perCount保存的说前驱节点的数量         * */        for(int i=0;i<prerequisites.length;i++)        {            int to=prerequisites[i][0];            int from=prerequisites[i][1];            list.get(from).add(to);            preCount[to]++;        }        //使用队列来拓扑排序        Queue<Integer> queue=new LinkedList<Integer>();        for(int i=0;i<numCourses;i++)        {            if(preCount[i]==0)                queue.add(i);        }        int res=numCourses;        while(queue.isEmpty()==false)        {            int from=queue.poll();            res--;            Set<Integer> one=list.get(from);            Iterator<Integer> iter =one.iterator();            while(iter.hasNext())            {                int to=iter.next();                preCount[to]--;                if(preCount[to]==0)                    queue.add(to);            }        }        //这里通过res来判断是否所有的课程都可以满足        return res==0?true:false;    }}

下面是C++的做法,这是一道十分典型的拓扑排序的问题,看过答案就会发现本题十分的简单

代码如下:

#include <iostream>#include <vector>#include <queue>#include <stack>#include <string>#include <set>#include <map>using namespace std;class Solution {public:    bool canFinish(int numCourses, vector<pair<int, int>>& reque)     {        if (numCourses <=0 || reque.size() <= 1)            return true;        vector<int> preCount(numCourses,0);        vector<set<int>> next(numCourses, set<int>());        for (pair<int, int> key : reque)        {            int from = key.second;            int to = key.first;            next[from].insert(to);            preCount[to]++;        }        queue<int> que;        for (int i = 0; i < preCount.size(); i++)        {            if (preCount[i] == 0)                que.push(i);        }        int count = numCourses;        while (que.empty() == false)        {            int from = que.front();            que.pop();            count--;            for (set<int>::iterator i = next[from].begin(); i != next[from].end(); i++)            {                preCount[*i]--;                if (preCount[*i] == 0)                    que.push(*i);            }        }        return count == 0 ? true : false;    }};
原创粉丝点击