Problem
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.Note:
1.The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
2.You may assume that there are no duplicate edges in the input prerequisites.
具体详见Leetcode
Course Schedule II
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.
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 the correct course order is [0,1]
4, [[1,0],[2,0],[3,1],[3,2]]
There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is [0,1,2,3]. Another correct ordering is[0,2,1,3].Note:
1.The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
2.You may assume that there are no duplicate edges in the input prerequisites.
具体详见Leetcode
Analysis
Thoughts
两道题的本质是一样的,所以做完第二题,第一题的答案就出来了。所以直接思考第二题。
一开始的想法就是这是一个图的问题,而且是一个有向图的问题,由于每门课可能都有一个前驱的课,所以就形成了一种优先关系。把问题拨开来看就是一个拓扑排序的问题。
首先知道有n个点,然后边就由vector< int >prerequisites给出。需要思考的第一个问题是,如何存储这个图。有两种方式,第一种是邻接矩阵,第二种是邻接链表。因为没有说明数据量的大小,假设数据量很大的情况下(顶点很多),用邻接矩阵可能会超出内存空间,所以,考虑用邻接链表。
解决了存储问题,该题的思路就是如何进行拓扑排序。由于这是抽象成图的题,所以从图的角度来看,我们可以抽象成图的入度问题。用一个队列把入度为0的点存起来,然后每次访问完,把它能到的点的入度减一,然后发现更新后点的入度变成0后加入队列。
注意的问题还有,注意有没有环,如果有环就不存在解。
Complexity
时间复杂度:
O(VE)
空间复杂度:
O(V+E)
Code
Course Schedule
struct Node {
int num;
Node* next;
Node(int n) {
num = n;
next = NULL;
}
};
class Solution {
public:
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
vector<pair<int, int>> edges(prerequisites);
int n = numCourses;
vector<int> ins;
vector<Node*> outDegree;
std::vector<int> v;
for (int i = 0; i < n; i++) {
ins.push_back(0);
outDegree.push_back(NULL);
}
for (int i = 0; i < edges.size(); i++) {
int in = edges[i].second;
int out = edges[i].first;
ins[in]++;
Node* temp = outDegree[out];
if (temp != NULL) {
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = new Node(in);
}
else {
outDegree[out] = new Node(in);
}
}
int j = 0;
queue<int> q;
for (j = 0; j < n; j++) {
if (ins[j] == 0) {
q.push(j);
}
}
if (q.size() == 0) return false;
while (v.size() < n) {
int nxt = q.front();
q.pop();
v.push_back(nxt);
Node* tmp = outDegree[nxt];
while (tmp != NULL) {
ins[tmp->num]--;
if (ins[tmp->num] == 0) q.push(tmp->num);
tmp = tmp->next;
}
if (q.size() == 0 && v.size() < n) return false;
}
return true;
}
};
Course Schedule II
struct Node {
int num;
Node* next;
Node(int n) {
num = n;
next = NULL;
}
};
class Solution {
public:
vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) {
int n = numCourses;
vector<pair<int, int>> edges(prerequisites);
vector<int> ins;
vector<Node*> outDegree;
std::vector<int> v;
for (int i = 0; i < n; i++) {
ins.push_back(0);
outDegree.push_back(NULL);
}
for (int i = 0; i < edges.size(); i++) {
int out = edges[i].second;
int in = edges[i].first;
ins[in]++;
Node* temp = outDegree[out];
if (temp != NULL) {
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = new Node(in);
}
else {
outDegree[out] = new Node(in);
}
}
int j = 0;
queue<int> q;
for (j = 0; j < n; j++) {
if (ins[j] == 0) {
q.push(j);
}
}
vector<int> empty;
if (q.size() == 0) return empty;
while (v.size() < n) {
int nxt = q.front();
q.pop();
v.push_back(nxt);
Node* tmp = outDegree[nxt];
while (tmp != NULL) {
ins[tmp->num]--;
if (ins[tmp->num] == 0) q.push(tmp->num);
tmp = tmp->next;
}
if (q.size() == 0 && v.size() < n) return empty;
}
return v;
}
};