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 ...
随机推荐
- spring boot将jar包转换成war包发布
spring boot将jar包转换成war包发布步骤 将<packaging>jar</packaging>修改为<packaging>war</packa ...
- 《Django By Example》
<Django By Example>第六章 中文 翻译 (个人学习,渣翻) 书籍出处:https://www.packtpub.com/web-development/django-ex ...
- MySQL表锁和行锁
锁粒度 MySQL 不同的存储引擎支持不同的锁机制,所有的存储引擎都以自己的方式显现了锁机制,服务器层完全不了解存储引擎中的锁实现: InnoDB 存储引擎既支持行级锁(row-level locki ...
- C++11 auto类型说明符的使用
编程的时候常常需要把表达式的值赋给变量,这就要求在声明变量的时候清楚地知道表达式的类型.然而做到这一点很难,有时候根本做不到.为了解决这个问题.C++11新标准引入了auto类型说明符,用它就 能让编 ...
- 搭建etcd集群
一 介绍 etcd 高可用一致性键值存储系统,使用Raft一直算法处理日志复制以保证数据一致性.主要在搭建kubernates时关注到etcd来研究部署etcd.使用golang语言编写,和zooke ...
- 使用lock锁或Monitor.Enter的目的
锁定的目的:由于多个线程 并行/并发 处理同一个“数据对象”(比如:在其它线程的某个地方发生了Clear.Add.Remove.Change等操作),导致“数据对象”不断变化,没法用了,所以,为了保证 ...
- 设计模式--组合模式C++实现
组合模式C++实现 1定义 将对象组合成树形结构以表示“部分-整体”的层次结构,使得用户对单个对象和组合对象的使用具有一致性 2类图 角色分析 Component抽象构建角色 定义参加组合独享的共同方 ...
- [nodejs]npm国内npm安装nodejs modules终极解决方案
此方案用于设置代理和修改镜像地址都不能解决问题使用 1.npm root 确认node模块的根文件夹,全局要加-g. osx同样是此命令,先清除缓存. npm cache clean C:\Users ...
- S16课件
Python之路,Day1 - Python基础1 介绍.基本语法.流程控制 Python之路,Day2 - Python基础2 列表.字典.集合 Python之路,Day3 - Python基础3 ...
- NPOI自定义单元格背景颜色
经常在NPOI群里聊天时发现有人在问NPOI设置单元格背景颜色的问题,而Tony Qu大神的博客里没有相关教程,刚好最近在做项目时研究了一下这一块,在这里总结一下. 在NPOI中默认的颜色类是HSSF ...