LeetCode Course Schedule

课程调度问题:LeetCode 630. Course Schedule III 题目描述: There are n different online courses numbered from 1 to n. Each course has some duration(course length) t and closed on dth day. A course should be taken continuously for t days and must be fini 阅读详情

LeetCode Course Schedule

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.



此题用基本的图论搜索方法可以解决(BFS或者DFS),判断是否存在环。

解法一:BFS(拓扑排序)

bool canFinish(int numCourses, vector<pair<int, int> >& prerequisites)
{
	vector<set<int> > matrix(numCourses);//用矩阵(二维数组)的方式保存图
	for (int i = 0; i < prerequisites.size(); i++)
		matrix[prerequisites[i].second].insert(prerequisites[i].first);

	vector<int> degree(numCourses, 0);
	for (int i = 0; i < numCourses; i++)
	{
		for (set<int>::iterator it = matrix[i].begin(); it != matrix[i].end(); ++it)
		{
			++degree[*it];//入度增一
		}
	}

	for (int i = 0; i < numCourses; i++)
	{
		int j;
		for (j = 0; j < numCourses && degree[j] != 0; j++){}//找出入度为0的顶点

		if (j == numCourses)	//没有入度为0的点,则不能完成
			return false;
		
		degree[j] = -1;
		//将所有以该顶点为起始顶点的入度减一
		for (set<int>::iterator it = matrix[j].begin(); it != matrix[j].end(); ++it)
		{
			--degree[*it];
		}
	}

	return true;
}

解法二:DFS(深搜判断是否存在环)
bool DFS(vector<set<int> >& matrix, set<int>& visited, int v, vector<bool>& flag)
{
	flag[v] = true;//访问标记设为true
	visited.insert(v);//将顶点v插入已访问的顶点集合
	
	for (set<int>::iterator it = matrix[v].begin(); it != matrix[v].end(); ++it)
	{
		if (visited.find(*it) != visited.end() || DFS(matrix, visited, *it, flag))//如果已经访问过,则存在环
		{
			return true;
		}
	}
	visited.erase(v);//回溯
	return false;
}

bool canFinish(int numCourses, vector<pair<int, int> >& prerequisites) 
{
	vector<set<int> > matrix(numCourses);//用矩阵(二维数组)的方式保存图
	for (int i = 0; i < prerequisites.size(); i++)
		matrix[prerequisites[i].second].insert(prerequisites[i].first);

	set<int> visited;//已访问的顶点组成的集合
	vector<bool> flag(numCourses, false);//顶点的访问标记,false表示未访问
	for (int i = 0; i < numCourses; i++)
	{
		if (!flag[i])//该顶点为访问,则以该顶点为起始点进行深搜
		{
			if (DFS(matrix, visited, i, flag))//如果遍历到相同的顶点,则存在环,返回false
				return false;
		}
	}
	return true;
}



Leetcode 207. Course Schedule(Python实现) 思路:将边集合构成一个有向图,判断有向图中是否存在环,如果存在环,则一定不能完成该课程;如果不存在环,还需判断,能否遍历到每个结点,如果能遍历到,那么课程能全部完成,则不然。这里判断有向图中是否存在环,用拓扑排序来实现。 class Solution: def canFinish(self, numCourses: int, prerequisites: List[List[int]]... 阅读详情

相关推荐

Leetcode 630. Course Schedule III

题目 解法:greedy+heap 总体思路是优先greedy的选择结束时间早的课程来进行完成,这样能保证完成最多的课程 将所有课程按照结束时间来排序 遍历每一节课,假设上了这节课,如果加完当前这节课发现不满足当前的end时间,那么就从选的课里面去掉一节时间最长的课程,最长的课程有可能是现在这节课,也有可能是之前的课程,这样就能时间重新满足条件,这个时候就是对于遍历到现在的课程的最优解。同时因为会移除时间最长的那节课,就保证了之后的课程会有更多的时间 注意:python的heapq默认是小根堆,需要改变

qq_37821701的博客 343

630. Course Schedule III

题目: There are n different online courses numbered from 1 to n. Each course has some duration(course length) t and closed on dth day. A course should be taken continuously for t days and must

Sdtin的博客 437

C8T6和ZET6选型更换过程中出现.\Obj\Project.sct(7): error: L6235E: More than one section matches selector - cann

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录前言一、问题原因二、解决方法三、现象总结 前言 又一件离谱的事情发生了,我之前转换过程中没有发生的错误,今天拿出来程序重新编译了一下,结果他奶奶滴一个新的错误出现了,而且我还完全不知道是什么原因导致的,先上图: 一、问题原因 之前我已经尝试过更换芯片选型,但是没有出现此类问题,今天更改的时候发现了,我还以为是个偶然错误,结果换来换去,还是有这个问题,那我之前说c8t6的程序更改成zet6上跑只需改芯片选型和宏定义岂不是啪啪打.

wx18045623090的博客 1488

leetcode_c++:图:Course Schedule(207)

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:

LandscapeMi 701

Linux系统下奥比中光Astra Pro相机SDK部署与避坑指南

本文详细指导了在Linux系统下部署奥比中光Astra Pro深度相机SDK的全过程,涵盖系统准备、SDK安装、设备权限配置及常见问题排查。重点解析了USB设备规则配置这一关键步骤,并提供了Python与C++项目的集成方法,旨在帮助开发者快速完成相机配置,避免常见陷阱。

weixin_28455801的博客 298

深入解析ACS SPiiPlus运动控制器的托管API架构与实战应用

本文深入解析了ACS SPiiPlus运动控制器的托管API架构,详细介绍了其核心模块、设计哲学与实战应用。通过C#/.NET封装的托管接口,开发者能够高效实现多轴协同运动控制、EtherCAT总线集成以及事件驱动的异步编程,显著提升高精度工业自动化设备的开发效率与系统稳定性。

jwt8token的博客 861

LeetCode#210 Course Schedule II(C++版)

题干: 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 expr

zc2985716963的博客 453

Unity与IOS交互,调用IOS系统相机和相册

与IOS的交互,在步骤上要比android简单许多,不会产生因为系统版本,或者包名导致的许多问题。过程中最头疼的一个问题是IOS回调Unity,很简单的一个问题,搞了半天。

Hello Mingo 1万+

LeetCode //C - 630. Course Schedule III

【代码】LeetCode //C - 630. Course Schedule III。

Made in Code 1758

Android实现自动输入文本效果 (附完整源码)

Android实现自动输入文本效果 (附完整源码)

希望我的博客,能帮上你解决学习中工作中所遇到的问题 414

老款 NUC 复活计划:装 Ubuntu 部署 OpenClaw 本地 AI

老款 NUC 变身本地 AI 服务器指南 本文详细介绍了如何将老旧华硕 NUC 设备改造为本地 AI 服务器。主要内容包括: 硬件评估:列出老款 NUC 的最低配置要求(i3 6代/8GB内存/256GB SSD),并提供兼容性检查命令和性能预期表 系统准备:指导备份数据、下载 Ubuntu 22.04 LTS 和制作启动盘的方法(Windows/Linux双平台) 安装流程:从 BIOS 设置到 Ubuntu 安装的完整步骤,包括推荐的分区方案 系统优化:禁用不必要的服务、内核参数调优、Swap 配置和

博客 7607

[leetcode] 207. Course Schedule 解题报告

题目链接:https://leetcode.com/problems/course-schedule/ 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

小榕流光的专栏 1939

Unreal Engine Profiler:动画系统性能分析教程_2024-07-23_03-05-38.Tex

Unreal Engine Profiler 是一个内置的性能分析工具,它可以帮助开发者识别和解决游戏中的性能瓶颈。Profiler 提供了详细的性能数据,包括 CPU 使用率、GPU 负载、内存使用情况等,特别针对动画系统,可以查看动画蓝图的执行时间、骨骼动画的计算成本、以及动画混合和重定向的效率。通过这些数据,开发者可以定位到具体的问题,比如哪些动画状态消耗了过多的 CPU 时间,或者哪些动画资源导致了内存泄漏。

824

Leetcode 207. Course Schedule 210. Course Schedule II

Leetcode 207. Course Schedule  import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class Solution { public boolean canFinish(int numCour

独钓寒江雪 318

YOLO11改进 | 卷积模块 | 轻量化GSConv替换普通的conv

yolo11,GSConv,卷积模块

kay_545 3955

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

leetcode题集: 207. Course Schedule

上次写了关于拓扑排序的写法,今天这个题就是关于拓扑排序的应用与改进。 第12题:207.Course Schedule 文章大意:课程安排,课程之间有依赖性。给定课程数,和课程之间的依赖,判断课程能否顺利完成。 题目分析:这不就是之前写的拓扑排序能干嘛吗?所以这是一个拓扑排序的问题,所以啊直接开始干。要注意的是,后面给数组中,后面一个表示前驱节点。即[1,0]表示的是0->1. ...

wx_14678的博客 298

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

LeetCode 207. Course Schedule

图和dfs

Llunnn的博客 339

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

leetcode题集: 210. Course Schedule II

第13题:210.Course Schedule II 类似之前写的那篇,这里就不分析了。 题目具体分析请见leetcode题集: 207. Course Schedule和 拓扑排序 两篇文章。就是拓扑排序,加个打印输出拓扑序列。这里直接贴代码了: class Solution { public: vector<int> findOrder(int numC...

wx_14678的博客 205

leetcode 207: Course Schedule

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 prerequisites, for example to take course 0 you have to first take course 1,...

qq_38677031的博客 182

LeetCode 207. Course Schedule 题解

本题是拓扑排序的经典问题,主要考察对拓扑排序算法的理解和应用。通过构建邻接表和入度数组,然后使用队列进行拓扑排序,我们可以判断是否存在环,从而确定是否可以完成所有课程的学习。拓扑排序的核心思想是:按照依赖关系的顺序,依次处理入度为 0 的节点。如果最终所有节点都被处理,说明不存在环;否则,说明存在环。这种方法不仅适用于课程表问题,还可以应用于许多其他需要处理依赖关系的场景,例如任务调度、编译顺序等。掌握拓扑排序的思想,对于解决这类问题非常重要。

cannonjinx的博客 4264

[leetcode] 207. Course Schedule

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 expresse...

Learning from the mistakes 240

【Leetcode】Course Schedule

题目链接: 题目:https://leetcode.com/problems/course-schedule/题目: 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

业精于勤,荒于嬉 3923

leetcode Course Schedule

题目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 pai

不积跬步,无以至千里 1962

LeetCode 210. Course Schedule II 题解

本题是拓扑排序的经典问题,是课程表问题的延伸,主要考察对拓扑排序算法的理解和应用。通过构建邻接表和入度数组,然后使用队列进行拓扑排序,我们可以不仅判断是否可以完成所有课程的学习,还可以返回具体的课程学习顺序。拓扑排序的核心思想是:按照依赖关系的顺序,依次处理入度为 0 的节点。如果最终所有节点都被处理,说明不存在环,返回处理顺序;否则,说明存在环,返回空数组。这种方法不仅适用于课程表 II 问题,还可以应用于许多其他需要处理依赖关系并返回处理顺序的场景,例如任务调度、编译顺序等。

cannonjinx的博客 4256

leetcode Course Schedule II

题目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 pai

不积跬步,无以至千里 1767
上一篇: LeetCode Word Search II
下一篇: LeetCode Contains Duplicate II Contains Duplicate III
FightForProgrammer
博客等级 码龄13年 239粉丝 75原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值