码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode知识点总结 - 210


    LeetCode 210. Course Schedule II

    考点难度
    Topo SortMedium
    题目

    There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai.

    For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1.
    Return the ordering of courses you should take to finish all courses. If there are many valid answers, return any of them. If it is impossible to finish all courses, return an empty array.

    思路

    1 Form adjacency list graph from P & compute indegree for each node
    2 For the 1st level of BFS iteration, fill up the queue with courses with no prerequisites
    3 At each iteration, pop & add the course from queue to ordering ans
    4 Decrement indegree of each course for which current course was prerequisite. If the indegree for those courses becomes 0, we can take it next by adding it to queue
    5 Continue the process till queue isn’t empty
    6 return ans

    答案
    class Solution:
        def findOrder(self, N, P):
            G, indegree, q, ans = defaultdict(list), [0]*N, deque(), []
            for nxt, pre in P:
                G[pre].append(nxt)
                indegree[nxt] += 1
            
            for i in range(N):
                if indegree[i] == 0:
                    q.append(i)
            while q:
                cur = q.popleft()
                ans.append(cur)
                for nextCourse in G[cur]:
                    indegree[nextCourse] -= 1
                    if indegree[nextCourse] == 0: 
                        q.append(nextCourse)
                        
            return ans if len(ans) == N else []
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
  • 相关阅读:
    HTML5期末大作业:旅游网页设计与实现——旅游风景区网站HTML+CSS+JavaScript 景点静态网页设计 学生DW静态网页设计
    《web课程设计》 基于HTML+CSS+JavaScript实现中国水墨风的小学学校网站模板(6个网页)
    五年Java编程生涯,大专学历最终逆袭阿里,面试+学习+经历分享
    Selenium4+Python3系列(五) - 多窗口处理之句柄切换
    安全高效应对混合办公新趋势 腾讯四大协同办公产品亮相
    携创教育:自考本科要考哪些科目?自考专升本有什么优势?
    利用vue做一个倒计时抢购的组件
    制霸GitHub热榜的Spring Cloud Alibaba源码笔记,果然是阿里传出的
    PyQt5快速开发与实战 9.1 使用PyInstaller打包项目生成exe文件
    Java面试中Java基础面试题
  • 原文地址:https://blog.csdn.net/m0_59773145/article/details/127950610
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号