Leetcode Course S
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.
典型的拓扑排序。原理也很简单,在一个有向图中,每次找到一个没有前驱节点的节点(也就是入度为0的节点),然后把它指向其他节点的边都去掉,重复这个过程(BFS),直到所有节点已被找到,或者没有符合条件的节点(如果图中有环存在)代码:
class Solution {
public:
bool canFinish(int numCourses, vector<pair<int,int> >&prerequisites){
vector<int>input_degree(numCourses,0);
multimap<int,int>st;
int i,j;
for(i=0;i<prerequisites.size();i++){
input_degree[prerequisites[i].first]++;
st.insert(make_pair(prerequisites[i].second,prerequisites[i].first));
}
for(i=0;i<numCourses;i++){
for(j=0;j<numCourses;j++)
if(input_degree[j]==0)break;
if(j==numCourses)return 0;
input_degree[j]--;
multimap<int,int>::iterator it=st.find(j);
int k=0;
int size=st.count(j);
while(k<size){
input_degree[it->second]--;
k++;
it++;
}
}
return 1;
}
};
210. 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]
.
class Solution {
public:
vector<int> findOrder(int numCourses, vector<pair<int,int> >&prerequisites){
vector<int>res;
vector<int> res1;
if(numCourses==0)return res;
vector<int>input_degree(numCourses,0);
multimap<int,int>st;
int i,j;
for(i=0;i<prerequisites.size();i++)
{
input_degree[prerequisites[i].first]++;
st.insert(pair<int,int>(prerequisites[i].second,prerequisites[i].first));
}
for(i=0;i<numCourses;i++){
for(j=0;j<numCourses;j++)
if(input_degree[j]==0)break;
if(j==numCourses)break;
res.push_back(j);
input_degree[j]--;
multimap<int,int>::iterator it=st.find(j);
int size=st.count(j);int k=0;
while(k<size){input_degree[it->second]--;k++;it++;}
}
return res.size()==numCourses?res:res1;
}
};
上一篇: (一)Tushare Pro教程:股票基本数据接口
下一篇: 软件测试基础(一)
推荐阅读
-
Leetcode Course S
-
Course Schedule
-
LeetCode380 常数时间插入、删除和获取随机元素
-
Checkra1n越狱发布:覆盖iPhone 5s到iPhone X、支持iOS 13.2.2
-
三星Galaxy S11系列细节曝光:至少三种配色
-
三星Galaxy S11系列渲染图曝光:双曲面挖孔屏/骁龙865
-
Virtual Users And Domains With Postfix, Courier, MySQL And S_MySQL
-
请问这个字符串里的空白字符是什么编码? 用"s"替换不了
-
this exceeds GitHub's file size limit of 100 MB
-
三星在印度发布Galaxy Tab S6 Lite:2600元 自带SPen