码农知识堂 - 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
  • 相关阅读:
    Day17_10 JavaWeb基础之IDEA、JavaWeb项目部署
    分析主题帆软决策报表控件实现点击查询按钮后才能查询
    大数据培训课程之fold(num)(func)案例
    django报错--Not Found The requested URL was not found on the server.
    【Python画图】Matplotlib中fig、ax、plt的区别及其用法(入门)
    颠覆传统:探索Web3对传统计算机模式的冲击
    Java 多线程共享模型之管程(上)
    uni-app入门:全局数据共享方案之mobx
    Nodejs和ES6的模块化 import ,export
    【进程间通信】进程间通信方式汇总
  • 原文地址:https://blog.csdn.net/m0_59773145/article/details/127950610
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号