以前从来没有写过解题报告,只是看到大肥羊河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 解题报告的更多相关文章

  1. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  2. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  3. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  4. 【LeetCode】207. Course Schedule 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/course-s ...

  5. LeetCode: Permutation Sequence 解题报告

    Permutation Sequence https://oj.leetcode.com/problems/permutation-sequence/ The set [1,2,3,…,n] cont ...

  6. Leetcode:Interleaving String 解题报告

    Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...

  7. Leetcode:Scramble String 解题报告

    Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...

  8. LeetCode: Gas Station 解题报告

    Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...

  9. LeetCode: Palindrome Partitioning 解题报告

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

随机推荐

  1. poi 取excel单元格内容时,需要判断单元格的类型,才能正确取出

    以下内容非原创,原文链接http://blog.sina.com.cn/s/blog_4b5bc01101015iuq.html ate String getCellValue(HSSFCell ce ...

  2. [Vue]Vue实例的选项props传递数据props为驼峰式命名

    在vue的中文官网有这样的说明: HTML 中的特性名是大小写不敏感的,所以浏览器会把所有大写字符解释为小写字符.这意味着当你使用 DOM 中的模板时,camelCase (驼峰命名法) 的 prop ...

  3. Linux命令详解-cp

    cp 命令用来复制文件或者目录,是Linux系统中最常用的命令之一.一般情况下,shell会设置一个别名,在命令行下复制文件时,如果目标文件已经存在, 就会询问是否覆盖,不管你是否使用-i参数.但是如 ...

  4. LightOJ - 1151概率dp+高斯消元

    概率dp+高斯消元 https://vjudge.net/problem/LightOJ-1151 题意:刚开始在1,要走到100,每次走的距离1-6,超过100重来,有一些点可能有传送点,可以传送到 ...

  5. 启用/禁用以太网的批处理,用于一个网卡切换本地网络和wifi使用(Win10)

    注意下面时英文版上默认网络使用,同时接入了网线和wifi时,本地网络优先wifi. 所以禁用本地网络就会自动连接到wifi,启用本地网络,就会禁用wifi. 批处理支持 -y 参数,跳过用户输入y,代 ...

  6. Linux运维第二天:安装虚拟机软件及RHEL7.2 64位系统

    第一步:安装虚拟机 一路默认就行啦(还是要改下安装路径,嘿嘿) 第二步:安装RHEL7.2 64位系统 1.新建一个虚拟机 典型和自定义随便选(最后都可以调的) 一般大婶都喜欢选自定义(自己可以设置的 ...

  7. 最近ES遇到discover老是失败问题,ping主节点和node节点正常,抓包发现了大量的retransmission、tcp out of order、dup ack问题。

    最近ES遇到discover老是失败问题,ping主节点和node节点正常,抓包发现了大量的retransmission.tcp out of order.dup ack问题. Explanation ...

  8. Redis数据结构:跳跃表

    1. 跳跃表是有序集合(zset)的底层实现之一: 2. 由zskiplist和zskiplistNode组成: 3. 每个跳跃表节点的层数都是1-32之间的随机数(每创建一个节点的时候,程序会随机生 ...

  9. UITextField 限制用户输入小数点后位数的方法

    UITextField 限制用户输入小数点后位数的方法 位数限制: limited 在UITextField的代理方法中添加类似如下代码 - (BOOL)textField:(UITextField ...

  10. MoreEffectiveC++Item35(效率)(条款16-24)

    条款16 谨记80-20法则 条款17 考虑使用 lazy evaluation(缓释评估) 条款18 分期摊还预期的计算成本 条款19 了解临时对象的来源 条款20 协助完成"返回值的优化 ...