[Leetcode Week3]Course Schedule
Course Schedule题解
原创文章,拒绝转载
题目来源:https://leetcode.com/problems/course-schedule/description/
Description
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.
Solution
class Solution {
private:
bool **graph;
int* color;
int graphDim = 0;
public:
~Solution() {
if (graphDim == 0)
return;
delete[] color;
for (int i = 0; i < graphDim; i++)
delete[] graph[i];
delete graph;
}
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
if (numCourses <= 0)
return false;
if (prerequisites.size() == 0)
return true;
int i, j;
graphDim = numCourses;
graph = new bool*[numCourses];
color = new int[numCourses];
for (i = 0; i < numCourses; i++)
graph[i] = new bool[numCourses];
for (i = 0; i < numCourses; i++) {
color[i] = 0;
for (j = 0; j < numCourses; j++) {
graph[i][j] = false;
}
}
for (auto p: prerequisites) {
graph[p.first][p.second] = true;
}
int sv;
for (auto p: prerequisites) {
sv = p.first;
for (i = 0; i < numCourses; i++) {
if (graph[sv][i]) {
if (color[sv] == 2)
continue;
if (!dfs(sv))
return false;
else
color[i] = 2;
}
}
}
return true;
}
bool dfs(int v) {
color[v] = 1;
int i;
for (i = 0; i < graphDim; i++) {
if (graph[v][i]) {
if (color[i] == 2) {
break;
} else if (color[i] == 1) {
return false;
} else {
if (!dfs(i)) {
return false;
}
}
}
}
color[v] = 2;
return true;
}
};
解题描述
这道题我使用的是DFS,过程中通过对以访问的顶点进行染色来判断是否有环。不过提交后出现了几次TLE,检查代码发现好几处可以充分利用染色数组来减少充分访问。改了好几次才AC。现实利用中DFS和BFS处理的数据量往往会很大,这几次TLE还是提醒我要注意优化算法时间复杂度。
文章更新
这道题本质上是检查图是否有环,而如果图是可以拓扑排序的,则一定是无环的。通过BFS检查图是否能够拓扑排序也是可以解决的,且相对来说实现效率较高,避免了DFS的递归开销。下面给出实现:
class Solution
{
private:
map<int, int> inDegree;
public:
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
int i, curVertex;
for (i = 0; i < numCourses; i++)
inDegree[i] = 0;
for (auto p: prerequisites)
inDegree[p.second]++;
queue<int> vq;
for (i = 0; i < numCourses; i++) {
if (inDegree[i] == 0) {
vq.push(i);
}
}
if (vq.empty())
return false;
while (!vq.empty()) {
curVertex = vq.front();
vq.pop();
inDegree.erase(curVertex);
for (i = 0; i < prerequisites.size(); i++) {
if (curVertex == prerequisites[i].first) {
inDegree[prerequisites[i].second]--;
if (inDegree[prerequisites[i].second] == 0)
vq.push(prerequisites[i].second);
}
}
}
return inDegree.empty();
}
};
[Leetcode Week3]Course Schedule的更多相关文章
- Java for LeetCode 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 prer ...
- Java for LeetCode 207 Course Schedule【Medium】
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- LeetCode 210. Course Schedule II(拓扑排序-求有向图中是否存在环)
和LeetCode 207. Course Schedule(拓扑排序-求有向图中是否存在环)类似. 注意到.在for (auto p: prerequistites)中特判了输入中可能出现的平行边或 ...
- [LeetCode] 207. Course Schedule 课程安排
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- [Leetcode Week4]Course Schedule II
Course Schedule II题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/course-schedule-ii/description/ De ...
- [LeetCode] 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 prereq ...
- [LeetCode] 207. Course Schedule 课程清单
There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prereq ...
- LeetCode - 207. Course Schedule
207. Course Schedule Problem's Link ---------------------------------------------------------------- ...
- Leetcode 210 Course Schedule II
here are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prere ...
随机推荐
- NetBeans集成SVN代码管理实例
最近给银行做一个小工具,要求用Java做一个C端带界面的小工具,想来想去用NetBeans最合适,因为Eclipse,MyEclipse,IDEA这些做界面得要额外的UI插件,比较麻烦. 我跟同事两个 ...
- Jmeter非GUI命令参数说明
查看帮助 -h, --help print usage information and exit 查看版本 -v, --version print the version information an ...
- 核方法(Kernel Methods)
核方法(Kernel Methods) 支持向量机(SVM)是机器学习中一个常见的算法,通过最大间隔的思想去求解一个优化问题,得到一个分类超平面.对于非线性问题,则是通过引入核函数,对特征进行映射(通 ...
- PHP 用Symfony VarDumper Component 调试
Symfony VarDumper 类似 php var_dump() 官方文档写的安装方法 : 按照步骤 就可以在 running any PHP code 时候使用了 In order to h ...
- 学习shell script
摘要:概述.script的编写.test命令.[]判断符号.默认变量($1...).if...then条件判断式. 一.概述 [什么是shell script] 针对shell所写的脚本,将多个命令汇 ...
- 制作一个简易计算器——基于Android Studio实现
一个计算器Android程序的源码部分分为主干和细节两部分. 一.主干 1. 主干的构成 计算器的布局 事件(即计算器上的按钮.文本框)监听 实现计算 2. 详细解释 假设我们的项目名为Calcula ...
- Elicpse新建userLibrary导入jar包时抛出NotFoundException异常
最近刚开始学Struts2.0框架.再导入jar包的时候突然心血来潮.为方便区分jar包的功能,于是想在WEB-INF -> lib 目录下想新建一个struts文件夹,以方便分类查看. 像上图 ...
- 201621044079 week07-JAVA GUI类
作业07-Java GUI编程 1. 本周学习总结 1.1 思维导图:Java图形界面总结 1.2 可选:使用常规方法总结其他上课内容. 2.书面作业 1. GUI中的事件处理 1.1 写出事件处理模 ...
- asp.net中的cookie
一.cookie导读,理解什么是cookie 1.什么是cookie:cookie是一种能够让网站服务器把少量数据(4kb左右)存储到客户端的硬盘或内存.并且读可以取出来的一种技术. 2.当你浏览某网 ...
- Xcode & swift
swift-apps swift 2018 apps Xcode Swift Playground https://developer.apple.com/download/ https://deve ...