LeetCode 207. Course Schedule

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

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.

This is actually to detect whether the graph has cycle in directed graph.

Solution 1 : union-find.

#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <iostream>
using namespace std;

int root(int a, vector<int>& dp) {
  while(a != dp[a]) {
    a = dp[a];
  }
  return a;
}

bool find(int a, int b, vector<int>& dp) {
  if(dp[a] == dp[b]) return true;
  else return false;
}

void unite(int a, int b, vector<int>& dp) {
  int root_a = root(a, dp);
  int root_b = root(b, dp);
  dp[root_a] = root_b;
}
bool canFinish(int numCourses, vector< pair<int, int> >& prerequisites) {
  // MakeSet.
  vector<int> dp;
  for(int i = 0; i < numCourses; ++i) {
    dp.push_back(i);
  }

  vector< vector<int> > schedules(numCourses);
  for(int i = 0; i < prerequisites.size(); ++i) {
    auto tmp = prerequisites[i];
    schedules[tmp.first].push_back(tmp.second);
  }

  for(int i = 0; i < schedules.size(); ++i) {
    for(int j = 0; j < schedules[i].size(); ++j) {
      if(find(i, schedules[i][j], dp)) return false;
      else unite(i, schedules[i][j], dp);
    }
  }
  return true;
}
int main(void) {
  vector<pair<int, int> > courses{{0,1}};
  vector<pair<int, int> > courses_1{{0,1}, {1, 0}};
  cout << "first test : " << canFinish(2, courses) << endl;
  cout << "second test : " << canFinish(2, courses_1) << endl;
}
DFS method:

bool dfsLoopDetection(const vector<vector<int> >& graph, vector<bool>& visited, int node, unordered_set<int>& startNodes) {
  visited[node] = true;
  for(int neighbor : graph[node]) {
    if(visited[neighbor]) return true;
    if(dfsLoopDetection(graph, visited, neighbor, startNodes)) return true;
  }
  visited[node] = false;
  startNodes.erase(node);
  return false;
}
bool canFinish(int numCourses, vector<pair<int, int> >& prerequisites) {
  if(numCourses == 0) return true;
  vector< vector<int> > graph(numCourses);
  for(auto p : prerequisites) {
    graph[p.first].push_back(p.second);
  }
  vector<bool> visited(numCourses, false);
  unordered_set<int> startNodes;
  for(int i = 0; i < numCourses; ++i) startNodes.insert(i);
  while(!startNodes.empty()) {
    int node = *(startNodes.begin());
    if(dfsLoopDetection(graph, visited, node, startNodes)) return false;
  }
  return true;
}


int main(void) {
  vector<pair<int, int> > courses {{0, 1}};
  cout << canFinish(2, courses) << endl;
}



Cadence Allegro新手避坑:从原理图到PCB,解决‘netrev.lst does not exist’的完整流程 本文详细介绍了Cadence Allegro从原理图设计到PCB导入网表的完整流程,特别针对新手常见的'netrev.lst does not exist'错误提供了系统解决方案。内容包括环境准备、原理图设计规范、网表生成与导入技巧,以及设计数据管理最佳实践,帮助工程师高效完成PCB设计任务。 阅读详情

相关推荐

案例分析|一篇文章带你看懂养鸡场智慧管理系统

养鸡场智慧管理系统是为了满足现代化养鸡业对精细化管理的需求而设计的一款专业软件。随着科技的发展和农业产业的升级,传统的手工记录和管理方式已无法满足高效、准确和实时的数据跟踪需求。本软件旨在通过集成各种功能模块,帮助养鸡场主实现从鸡苗孵化、饲养管理、饲料控制到销售结算等全链条的智能化操作。该系统的核心目标是提升养鸡场的运营效率和盈利能力,通过自动化数据处理,减少人工误差,节省时间和精力。例如,孵化报表和记录模块可以帮助管理者实时监控孵化过程,及时调整策略;

muyu2980的专栏 930

[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

OpenCV图像拼接实战:用SIFT+透视变换打造无缝全景图(附完整代码)

本文详细介绍了使用OpenCV和SIFT算法实现图像拼接的全过程,包括特征匹配、透视变换和高级融合技术。通过实战代码演示,帮助开发者掌握无缝全景图生成的核心技术,适用于旅行摄影、无人机航拍等多种场景。

eee77的博客 997

LeetCode 207. Course Schedule 题解

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

cannonjinx的博客 4264

SAP入门技术分享四:模块化程序

SAP入门技术分享三:模块化程序

ITxiaoangzai的博客 1152

【数据攻略】字节面试真题(含答案)+100道面试题库

数据攻略

qq_40002713的博客 4102

leetcode题集: 207. Course Schedule

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

wx_14678的博客 298

STM32CubeMX、keil、simulink联合开发MCU教程

一、整体思路 利用Matlab的simulink搭建模型生成C代码,通过stm32cubemx生成工程,最后在KEIL或者IAR等工具里面编译生成代码下载至MCU中执行。 本文以流水灯建立模型和简单使用定时器为例进行演示。 二、开发环境 1、安装Matlab Matlab版本建议在2013B以上,本文使用Matlab 2016a。 2、安装STM32硬件支持包 下载地址(https://www.stmcu.org.cn/document/detail/index/id-216053) 安装完成后重启MAT

tzlaa的博客 7734

[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

第 8 篇:TMC2240 电机正反转实现|DIR 引脚控制 + 代码优化(稳定不抖动)

本文详细介绍了TMC2240电机驱动芯片的正反转实现方法,重点解决方向切换时的抖动和冲击问题。文章首先明确了硬件连接、基础配置和使能状态三个前提条件,然后阐述了DIR引脚电平控制方向的核心原理,提供了STM32的GPIO配置代码。针对方向切换时的电流冲击问题,作者提出了"停止-延迟-切换-启动"的平滑切换逻辑,并给出了包含方向枚举、转速控制和方向切换函数的完整代码实现方案。通过这种优化方法,可以有效避免电机在正反转切换时产生的抖动、冲击声和发热问题,实现稳定的双向控制。文章还提示了后续可

BackCatK Chen的博客 2073

北斗导航 | 卡尔曼滤波与因子图优化对比(附图优化matlab代码)

- 历史数据利用:因子图通过滑动窗口保留历史状态,优化时联合多帧数据;卡尔曼滤波仅依赖当前和上一帧状态 。 - 迭代优化:因子图通过多次迭代逼近最优解,卡尔曼滤波仅单次更新 。 - 误差抑制:因子图的多次线性化可有效抑制非线性误差(如GNSS伪距观测的非线性) 。

尘世冰封的专栏 1299

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, which is expressed as

无名山丘,崛起成峰 1059

YOLOv13添加DRB模块map暴涨12.33!准确率提升8.56!召回率提升15.66

本文介绍了在YOLOv13模型中添加DRB模块的改进方法。实验结果显示,添加DRB模块后模型性能显著提升,mAP暴涨12.33%,准确率提升8.56%。文章详细说明了移植过程,包括创建yolov13-DRB.yaml配置文件、修改tasks.py和__init__.py文件,以及引入UniRepLKNet模块的具体实现。其中重点描述了GRNwithNHWC、NCHWtoNHWC等核心组件的实现细节,并提供了完整的模型结构配置代码。这些改进使得YOLOv13模型在保持原有架构优势的同时,获得了更强大的特征提取

FJN110的博客 2819

Leetcode 207 Course Schedule 课程表

你需要上n个课程,标记为0~n-1。有些课程有前导课,比如你要上课程0必须先上课程1,我们用一个整数对[0,1]来表示。给出课程的总数和一系列的课程先导关系对,判断是否可能上完所有的课程。

(ง •̀_•́)ง 6515

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

leetcode 207: Course Schedule

leetcode 207 : Course Schedule

西施豆腐渣 5423

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

LeetCode 207 Course Schedule

LeetCode 207 Course Schedule 题意 有0~n-1共n门课你需要进修,其中一些课程需要在修完其他课程后才能进修,例如(0,1)表示要进修课程0你必须先修完课程1。给出课程的总数以及一个列表表示所有的先决条件,问是否可能修完所有课程。 注: 课程先决条件可以看作是由一个图的边(edge)组成 不会出现重复的边 思路 假如将每个先决条件(a,b)看作是b到a的有向边,整个...

EricJeffrey的博客 164

Statistics and Machine Learning Toolbox

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

LeetCode207 Course Schedule

LeetCode207 Course Schedule问题描述 here 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 co

yanqinghe123的专栏 373

【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, which is expressed as a p

aliceyangxi1987的博客 746

Leetcode [207. Course Schedule]

Problem:207. Course ScheduleQuestionThere 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 cou

a1042651820的博客 300

LeetCode 207. Course Schedule && LeetCode 210. Course Schedule II

LeetCode 207. Course Schedule && LeetCode 210. Course Schedule II (Medium) 主要知识点:拓扑排序、BFS、DFS;优先级:5

温迪今天刷题了吗 981

LeetCode 207. Course Schedule - 拓扑排序(Topological Sort)系列题1

定义记不住不重要,只要知道拓扑排序(Topological Sort)通常是用来排序具有依赖关系的任务。在LeetCode中207. Course Schedule是拓扑排序最经典的一题。

hgq522的博客 869

Leetcode207. Course Schedule

给定 n 组先修课的关系,[m,n] 代表在上 m 这门课之前必须先上 n 这门课。输出能否成功上完所有课。

magic_jiayu的博客 321

【LeetCode 207】Course Schedule

题意: 学习一门课程,要有先置课程。给出每门课程需要先置的课程,问是否能全部修完。 思路: 递归。建图,对每个节点,判断是否存在环。标记数组,标记已判断过的节点和当前环是否遍历到,如果重复遍历,表示有环。 代码: class Solution { public: bool canFinish(int numCourses, vector<vector<int>>&a...

iCode_girl的博客 181
上一篇: LeetCode 350. Intersection of Two Arrays II
下一篇: LeetCode 31. Next Permutation
ActiveCoder
博客等级 码龄11年 4粉丝 345原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值