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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. LeetCode 210. Course Schedule II(拓扑排序-求有向图中是否存在环)

    和LeetCode 207. Course Schedule(拓扑排序-求有向图中是否存在环)类似. 注意到.在for (auto p: prerequistites)中特判了输入中可能出现的平行边或 ...

  4. [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 ...

  5. [Leetcode Week4]Course Schedule II

    Course Schedule II题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/course-schedule-ii/description/ De ...

  6. [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 ...

  7. [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 ...

  8. LeetCode - 207. Course Schedule

    207. Course Schedule Problem's Link ---------------------------------------------------------------- ...

  9. 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 ...

随机推荐

  1. Qt Demo Http 解析网址 Openssl

    今天练习了一下Qt 解析http协议,在Demo中使用到了Openssl 一上午的时间都是编译openssl,不过还是没有成功,很遗憾,这里整理了有关这个Demo的本件 网盘连接:见下方评论吧,长传太 ...

  2. 一丶人生苦短,我用python【第一篇】

    1 解释器 解释器(英语:Interpreter),又译为直译器,是一种电脑程序,能够把高级编程语言一行一行直接转译运行.解释器不会一次把整个程序转译出来,只像一位"中间人",每次 ...

  3. Python第三方库之openpyxl(3)

    Python第三方库之openpyxl(3) 区域图 区域图类似于折线图,绘图线下面的区域会被填充,通过将分组设置为“standard”.“stacked”或“percentStacked”,可以获得 ...

  4. 深度学习-CNN tensorflow 可视化

    tf.summary模块的简介 在TensorFlow中,最常用的可视化方法有三种途径,分别为TensorFlow与OpenCv的混合编程.利用Matpltlib进行可视化.利用TensorFlow自 ...

  5. Ubuntu16.04 + CUDA9.0 + cuDNN7.3 + Tensorflow-gpu-1.12 + Jupyter Notebook 深度学习环境配置

    目录 一.Ubuntu16.04 LTS系统的安装 二.设置软件源的国内镜像 1. 设置方法 2.关于ubuntu镜像的小知识 三.Nvidia显卡驱动的安装 1. 首先查看显卡型号和推荐的显卡驱动 ...

  6. Spring Boot学习(一):入门篇

    目录 Spring Boot简介 Spring Boot快速搭建 1 新建项目 2 运行项目 3 设置spring boot可以热部署(修改后端代码后,自动部署,不用手动部署) 3.1:配置pom.x ...

  7. PAT 1084 外观数列

    https://pintia.cn/problem-sets/994805260223102976/problems/994805260583813120 外观数列是指具有以下特点的整数序列: d, ...

  8. poj 1753 Flip Game (dfs)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28805   Accepted: 12461 Descr ...

  9. BZOJ4290 传送门

    昨天考试考了这道题,学校评测不开O2被卡的一愣一愣的. 这种题线性复杂度就线性复杂度,为什么要卡常数. 顺便提一句,GRH大爷O(m*n*ans)的算法有90分,我的O(m*n)算法75.(万恶的ST ...

  10. thymeleaf支持java8的日期实例

    一.实体 @Entity public class Customer { @Id @GenericGenerator(name="generator",strategy = &qu ...