[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 ...
随机推荐
- bugku 字符正则
字符?正则? <?php highlight_file('2.php'); $key='KEY{********************************}'; $IM= preg_mat ...
- POI HSS 合并重复的列
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; i ...
- PostgreSQL基本配置
记一下Postgresql的基本操作,在Ubuntu下使用apt-get安装是不会像MySQL那样都配置好了,而是要安装后再配置: 1. 基本安装 # 安装postgresql和pgadmin(一个管 ...
- ipfs02笔记
IPFS-day02 其他常用操作 添加文件并用文件夹包裹 ipfs add xxx -w 把內容快取到本地,并提供给他人.官网文档 ipfs pin add QmT7TX5vGmFz86V8cDkP ...
- NO3——BFS
#include <stdio.h> #include <string.h> #include <queue> using namespace std; struc ...
- VS2010历史记录清理
把如下粘贴到文本文件里,另存为批处理文件.(后缀为 *.bat)双击执行就可 @echo off cd \ @echo on @REG Delete HKEY_CURRENT_USER\Softwar ...
- 详细介绍javascript中的几种for循环的区别
偶然间见到了forEach循环,感觉很新奇,就研究了一下,顺带着把js中的几种for循环做了一个比较. 首先,简单说一下,js中一共大概有四种for循环:(1).那种简单常见的for循环:(2).fo ...
- Spring和SpringMVC配置中父子WebApplicationContext的关系
一.前言 有这么一个故事:一辆装满石头的板车,一根绳子系着,起初绳子没有拉直,拉绳的人以为很轻,等真的绷直了才发现自己的力气根本不够~人往往喜欢得过且过,但是有些东西真的是绕不过的,所以现在必须努力的 ...
- [剑指Offer] 22.从上往下打印二叉树
[思路]广度优先遍历,队列实现 class Solution { public: vector<int> PrintFromTopToBottom(TreeNode* root) { qu ...
- ES 1.7安装ik分词elasticsearch-analysis-ik-1.2.5中文同义词实现
ElasticSearch 中文同义词实现 https://blog.csdn.net/xsdxs/article/details/52806499 参考以下两个网址,但运行报错,以下是我自己改进方式 ...