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. C#3DES加密了解一下

    最近一个项目中,因为服务端是用的java开发的,客户端是用的C#,由于通信部分采用到了3DES加密,所以做个记录,以备以后需要的时候直接用. 这是对方(java)的加密算法,和网上流传的代码也差不多( ...

  2. Lambda表达式在Kotlin中怎样工作的:setOnClickListener的转换(KAD 18)

    作者:Antonio Leiva 时间:Mar 28, 2017 原文链接:https://antonioleiva.com/lambdas-kotlin-android/ 虽然,我在其它文章讲过一点 ...

  3. 【性能监控】虚拟内存监控命令vmstat详解

    一.Vmstat说明 vmstat是Virtual Meomory Statistics(虚拟内存统计)的缩写,可对操作系统的虚拟内存.进程.CPU活动进行监控.vmstat 工具提供了一种低开销的系 ...

  4. Java 访问权限控制 小结

    总所周知,Java提供了访问权限修饰词,以供类库开发人员向客户端程序员指明哪些是可用的,哪些是不可用的. 访问权限控制的等级,从最大权限到最小权限依次为:public.protected.包访问权限( ...

  5. pta函数作业

    7-10 设计思路:本题需要判断一个正整数数是否为素数,所谓素数,就是除一和本身外没有其他因数的数.具体判断过程如下:对于一个大于一的整数,从2开始用循环计数i去除此数,若余数不为零,则循环计数i自加 ...

  6. xml解析标签

    //获取两个标签之间的值 private static string GetStr(string message, string strStart, string strEnd) { ; ; star ...

  7. DataGridView使用

    DataGridView控件概述 DataGridView 控件代码目录(Windows 窗体) 未绑定数据列 定义:可能想要显示并非来自数据源的一列数据,这种列称为未绑定列. 数据格式示例 如何:设 ...

  8. 批处理之FOR命令

  9. 从CUBIC/BBR的TCP ACK失速说起

    上周有同事问,延迟ACK到底对应用层会产生什么后果,我也不知道该如何作答,于是丢了一个链接: TCP之Delay ACK在Linux和Windows上实现的异同-Linux的自适应ACK: 是的,这是 ...

  10. hdu 6203 ping ping ping(LCA+树状数组)

    hdu 6203 ping ping ping(LCA+树状数组) 题意:给一棵树,有m条路径,问至少删除多少个点使得这些路径都不连通 \(1 <= n <= 1e4\) \(1 < ...