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 ...
随机推荐
- UVa 10765 鸽子和炸弹(割点)
https://vjudge.net/problem/UVA-10765 题意: 给一个n个点的无向图,求每个点删去后形成的连通分量数. 思路: 判断割点,如果是割点的话,在dfs的时候计算出删去它后 ...
- 锁(3)-- DB锁
1 前言 数据库大并发操作要考虑死锁和锁的性能问题.看到网上大多语焉不详(尤其更新锁),所以这里做个简明解释,为下面描述方便,这里用T1代表一个数据库执行请求,T2代表另一个请求,也可以理解为T1为一 ...
- POJ3768 Katu Puzzle
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- js 弹出层,以及在javascript里定义层样式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 1-25-循环控制符break、continue和函数详解
大纲: 1-for循环补充 1-1-for循环实战---类C格式应用 2-break.continue循环控制符 2-1实战:帮助理解break.continue作用 3-函数详解 3-1.脚本文件中 ...
- wordcount程序出现map 100% reduce 0%问题的解决方法
运行wordcount程序一直停在map 100% reduce 0%, input文件夹的内容: 其中: f1.txt中的内容为:hello hadoop f2.txt中的内容为:hello had ...
- OC与JS的交互详解
事情的起因还是因为项目需求驱动.折腾了两天,由于之前没有UIWebView与JS交互的经历,并且觉得这次在功能上有一定的创造性,特此留下一点文字,方便日后回顾. 我要实现这样一个需求:按照本地的CSS ...
- day37 爬虫2(web微信、高性能相关、Scrapy)
s16day37 爬虫2 参考博客:http://www.cnblogs.com/wupeiqi/articles/6229292.html 课堂代码:https://github.com/liyon ...
- Putty实现Linux与Windows互传文件
putty远程连接VPS,先开一贴,有空来整理. 从putty官网下载putty,选择[A Windows installer for everything except PuTTYtel]安装包,下 ...
- iptables详解(13):iptables动作总结之二
概述 阅读这篇文章需要站在前文的基础上,如果你在阅读时遇到障碍,请参考之前的文章. 前文中,我们已经了解了如下动作 ACCEPT.DROP.REJECT.LOG 今天,我们来认识几个新动作,它们是: ...