LeetCode - Course Schedule 解题报告
以前从来没有写过解题报告,只是看到大肥羊河delta写过不少。最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手。
原题链接
https://leetcode.com/problems/course-schedule/
题目大意
有n个课程,编号分别是0到n-1。我们的目标是修完所有课程。然而有些课程有前置课程的,我们必须修完前置课程才能修该门课程。题目给了我们课程之间的前置关系,让我们判断是否能修完所有课程。
题目原型
这个题目的描述简单粗暴,我们不难发现,其实是给了我们一个有向图,然后问我们这个图里面是否存在环。
解题思路
我们的目的也非常直观,就是判断一个有向图是否存在环。
我想到的是用dfs。首先构造出一棵树(当然不一定是真正的树,因为有可能存在环;也有可能是多棵树)。然后对每棵树进行深搜,一旦发现某个节点和它的祖先节点相同,就存在环。这里给出一份伪代码。其中processed状态并不是必须的,只是为了避免一些不必要的重复搜索。
// 伪代码
foreach node
{
if (node is not processed)
dfs(node);
} dfs(node)
{
mark node as processed
mark node as visiting
foreach childNode
{
if (node is visiting)
{
find circle and stop;
}
if (childNode is not processed)
{
dfs(childNode)
}
}
mark node as not visiting
}
不过我最后并没有用这种方法,而是用了一个叫做Kahn的拓扑排序典型算法。让我来介绍一下这个算法的流程(其实很简单,一看包会)。
// L 储存最终有序结果的List
// S 储存所有不存在入边的节点,即入度为0的点的集合
while S is not empty
get a node x from S
append x to list L
foreach node that has an edge from x(e.g. x -> y)
remove that edge
if y doesn't contain any income edges
add y to set S if L contains all the nodes
succeed
else
fail
这个算法的精髓在于维护了一个入度为0的点的集合(这个集合可以是set,array,list等,非常自由),每次处理掉一个0入度的点,然后把新产生的0入度的点添加到该集合。
结合我们的题目,可以发现这个算法可以直接应用到我们这个题上来,而不需要任何的额外改变。所以我就直接贴代码了。
public boolean canFinish(int numCourses, int[][] prerequisites) {
// 个人习惯,判断一下特殊情况
if (numCourses <= 1 || prerequisites == null)
{
return true;
}
Stack<Integer> out[] = new Stack[numCourses]; // 所有的边
for (int i = 0; i < numCourses; i++)
{
out[i] = new Stack<Integer>();
}
int[] in = new int[numCourses]; // 统计入度的数组
for (int i = 0; i < prerequisites.length; i++)
{
out[prerequisites[i][0]].push(prerequisites[i][1]);
in[prerequisites[i][1]]++;
}
Stack<Integer> noneIn = new Stack<Integer>(); // 集合S
int res = 0; // 由于并不需要最终的排序结果,所以只记录了L中的个数
for (int i = 0; i < numCourses; i++)
{
if (in[i] == 0)
{
noneIn.push(i);
}
}
while (!noneIn.isEmpty())
{
int x = noneIn.pop();
res++;
while (!out[x].isEmpty())
{
int y = out[x].pop();
if (--in[y] == 0)
{
noneIn.push(y);
}
}
}
return res == numCourses;
}
LeetCode - Course Schedule 解题报告的更多相关文章
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- 【LeetCode】207. Course Schedule 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/course-s ...
- LeetCode: Permutation Sequence 解题报告
Permutation Sequence https://oj.leetcode.com/problems/permutation-sequence/ The set [1,2,3,…,n] cont ...
- Leetcode:Interleaving String 解题报告
Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...
- Leetcode:Scramble String 解题报告
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
- LeetCode: Gas Station 解题报告
Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...
- LeetCode: Palindrome Partitioning 解题报告
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
随机推荐
- Tachyon:Spark生态系统中的分布式内存文件系统
转自: http://www.csdn.net/article/2015-06-25/2825056 摘要:Tachyon把内存存储的功能从Spark中分离出来, 使Spark可以更专注计算的本身, ...
- WPF圆角透明无边框窗体
<Window x:Class="ImportData.MainWindow" xmlns="http://schemas.microsoft.com/winfx/ ...
- 修饰器Decorator
类的修饰 许多面向对象的语言都有修饰器(Decorator)函数,用来修改类的行为.目前,有一个提案将这项功能,引入了 ECMAScript. @testable class MyTestableCl ...
- Flutter新手第一个坑:Could not find com.android.tools.lint:lint-gradle:26.1.1.
解决方法1:修改build.gradle,注释掉jcenter(),google().使用阿里的镜像.原因是jcenter google库无法访问到导致的问题.虽然我有万能的爬墙工具,开启全局代理依然 ...
- css中pt、px、em、ex、in等这类长度单位详细说明
在CSS样式表中,我们经常会看到pt, px,em,ex,in等这类长度单位.它们各是什么意思,有什么区别呢? 在CSS样式表中,长度单位分两种: 相对长度单位,如px, em等 绝对长度单位,如pt ...
- IdentityServer4在Asp.Net Core中的应用(二)
继续上次授权的内容,客户端模式后我们再说以下密码模式,先回顾下密码模式的流程: 我们还是使用上次的代码,在那基础上修改,在IdentityServer4里面有一个IdentityServer4.Tes ...
- 《高级Web应用程序设计》作业(20170904)
作业1(类型-理论学习,上传ftp,截止日期9月20日) 1.请写出ASP.NET MVC的优点. 2.请写出默认项目模板中以下文件夹或文件的作用.App_Data文件夹.Content文件夹.Con ...
- 互换CapsLock和Ctrl键
如果你没有HHKB键盘,完全可以利用系统自身的功能交换CapsLock和Ctrl键. macOS系统 在系统偏好设置里,点击“键盘”,在出现的画面点击右下角的“修饰键...”按钮,在这里可以配置这两个 ...
- 《深入理解mybatis原理4》 MyBatis缓存机制的设计与实现
<深入理解mybatis原理> MyBatis缓存机制的设计与实现 本文主要讲解MyBatis非常棒的缓存机制的设计原理,给读者们介绍一下MyBatis的缓存机制的轮廓,然后会分别针对缓存 ...
- BZOJ 1010 [HNOI2008]玩具装箱 (斜率优化DP)
题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=1010 思路 [斜率优化DP] 我们知道,有些DP方程可以转化成DP[i]=f[j]+x[i ...