您的位置:首页 > 其它

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

2017-09-21 09:37 316 查看
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.

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

建议和leetcode 210. Course Schedule II 拓扑排序 + HashSetleetcode 630. Course Schedule III 课程调度 + 贪心算法 一起学习

需要注意的是,由于这道题更新了测试用例所以原先的代码可能出现问题,情况是这样的,记录后继结点要使用vector,不要使用set,因为假如存在(1,9)和(1,9)多次出现,这样就会出现问题,所以要使用vector

代码如下:

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 <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <stack>
#include <string>
#include <climits>
#include <algorithm>
#include <sstream>
#include <functional>
#include <bitset>
#include <numeric>
#include <cmath>
#include <regex>

using namespace std;

class Solution
{
public:
bool canFinish(int numCourses, vector<pair<int, int>>& reque)
{
vector<int> preCount(numCourses, 0);
vector<vector<int>> next(numCourses, vector<int>());
for (pair<int, int> key : reque)
{
int from = key.second;
int to = key.first;
next[from].push_back(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 (int i: next[from])
{
preCount[i]--;
if (preCount[i] == 0)
que.push(i);
}
}

return count == 0 ? true : false;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: