劍指 Offer II 113. 課程順序


comments: true
edit_url: https://github.com/doocs/leetcode/edit/main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20113.%20%E8%AF%BE%E7%A8%8B%E9%A1%BA%E5%BA%8F/README.md

劍指 Offer II 113. 課程順序

題目描述

現在總共有 numCourses?門課需要選,記為?0?到?numCourses-1

給定一個數組?prerequisites ,它的每一個元素?prerequisites[i]?表示兩門課程之間的先修順序。?例如?prerequisites[i] = [ai, bi]?表示想要學習課程 ai?,需要先完成課程 bi?。

請根據給出的總課程數 ?numCourses 和表示先修順序的?prerequisites?得出一個可行的修課序列。

可能會有多個正確的順序,只要任意返回一種就可以了。如果不可能完成所有課程,返回一個空數組。

?

示例?1:

輸入: numCourses = 2, prerequisites = [[1,0]] 
輸出: [0,1]
解釋:?總共有 2 門課程。要學習課程 1,你需要先完成課程 0。因此,正確的課程順序為 [0,1] 。

示例?2:

輸入: numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]
輸出: [0,1,2,3] or [0,2,1,3]
解釋:?總共有 4 門課程。要學習課程 3,你應該先完成課程 1 和課程 2。并且課程 1 和課程 2 都應該排在課程 0 之后。因此,一個正確的課程順序是?[0,1,2,3] 。另一個正確的排序是?[0,2,1,3]

示例 3:

輸入: numCourses = 1, prerequisites = [] 
輸出: [0]
解釋:?總共 1 門課,直接修第一門課就可。

?

提示:

  • 1 <= numCourses <= 2000
  • 0 <= prerequisites.length <= numCourses * (numCourses - 1)
  • prerequisites[i].length == 2
  • 0 <= ai, bi < numCourses
  • ai != bi
  • prerequisites?中不存在重復元素

?

注意:本題與主站 210?題相同:https://leetcode.cn/problems/course-schedule-ii/

解法

方法一:拓撲排序

拓撲排序的思路是,先統計每個節點的入度,然后從入度為 0 的節點開始,依次刪除這些節點,同時更新與這些節點相連的節點的入度,直到所有節點都被刪除。

這里使用隊列來存儲入度為 0 的節點,每次從隊列中取出一個節點,將其加入結果數組中,然后遍歷與這個節點相連的節點,將這些節點的入度減 1,如果減 1 后入度為 0,則將這些節點加入隊列中。

最后判斷結果數組的長度是否等于節點的個數,如果等于則返回結果數組,否則返回空數組。

時間復雜度 O ( n + m ) O(n + m) O(n+m),空間復雜度 O ( n + m ) O(n + m) O(n+m)。其中 n n n m m m 分別是節點的個數和邊的個數。

Python3
class Solution:def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:graph=defaultdict(list)indg={} #其實目的還是為bfs第一層服務的for i in range(numCourses): #【注意】初始化,要不然第一層為空indg[i]=0 for b,a in prerequisites:graph[a].append(b)indg[b]+=1# 第一層q=deque()for node,ind in indg.items():if ind==0:q.append(node)print(q,indg)#bfsres=[]while q:for _ in range(len(q)):cur=q.popleft()res.append(cur)for nx in graph[cur]:indg[nx]-=1if indg[nx]==0:q.append(nx)return res if len(res)==numCourses else []
Java
class Solution {public int[] findOrder(int numCourses, int[][] prerequisites) {List<Integer>[] g = new List[numCourses];Arrays.setAll(g, k -> new ArrayList<>());int[] indeg = new int[numCourses];for (var p : prerequisites) {int a = p[0], b = p[1];g[b].add(a);++indeg[a];}Deque<Integer> q = new ArrayDeque<>();for (int i = 0; i < numCourses; ++i) {if (indeg[i] == 0) {q.offer(i);}}int[] ans = new int[numCourses];int cnt = 0;while (!q.isEmpty()) {int i = q.poll();ans[cnt++] = i;for (int j : g[i]) {if (--indeg[j] == 0) {q.offer(j);}}}return cnt == numCourses ? ans : new int[0];}
}
C++
class Solution {
public:vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {vector<int> g[numCourses];vector<int> indeg(numCourses);for (auto& p : prerequisites) {int a = p[0], b = p[1];g[b].push_back(a);++indeg[a];}queue<int> q;for (int i = 0; i < numCourses; ++i) {if (indeg[i] == 0) {q.push(i);}}vector<int> ans;while (q.size()) {int i = q.front();q.pop();ans.push_back(i);for (int j : g[i]) {if (--indeg[j] == 0) {q.push(j);}}}return ans.size() == numCourses ? ans : vector<int>();}
};
Go
func findOrder(numCourses int, prerequisites [][]int) []int {g := make([][]int, numCourses)indeg := make([]int, numCourses)for _, p := range prerequisites {a, b := p[0], p[1]g[b] = append(g[b], a)indeg[a]++}q := []int{}for i, v := range indeg {if v == 0 {q = append(q, i)}}ans := []int{}for len(q) > 0 {i := q[0]q = q[1:]ans = append(ans, i)for _, j := range g[i] {indeg[j]--if indeg[j] == 0 {q = append(q, j)}}}if len(ans) == numCourses {return ans}return []int{}
}
TypeScript
function findOrder(numCourses: number, prerequisites: number[][]): number[] {const g: number[][] = Array.from({ length: numCourses }, () => []);const indeg: number[] = Array(numCourses).fill(0);for (const [a, b] of prerequisites) {g[b].push(a);++indeg[a];}const q: number[] = indeg.map((v, i) => (v === 0 ? i : -1)).filter(v => v !== -1);const ans: number[] = [];while (q.length) {const i = q.pop()!;ans.push(i);for (const j of g[i]) {if (--indeg[j] === 0) {q.push(j);}}}return ans.length === numCourses ? ans : [];
}
C#
public class Solution {public int[] FindOrder(int numCourses, int[][] prerequisites) {List<int>[] g = new List<int>[numCourses];for (int i = 0; i < numCourses; i++) {g[i] = new List<int>();}int[] indeg = new int[numCourses];foreach (var p in prerequisites) {int a = p[0], b = p[1];g[b].Add(a);++indeg[a];}Queue<int> q = new Queue<int>();for (int i = 0; i < numCourses; ++i) {if (indeg[i] == 0) {q.Enqueue(i);}}int[] ans = new int[numCourses];int cnt = 0;while (q.Count > 0) {int i = q.Dequeue();ans[cnt++] = i;foreach (int j in g[i]) {if (--indeg[j] == 0) {q.Enqueue(j);}}}return cnt == numCourses ? ans : new int[0];}
}
Swift
class Solution {func findOrder(_ numCourses: Int, _ prerequisites: [[Int]]) -> [Int] {var graph = Array(repeating: [Int](), count: numCourses)var indegree = Array(repeating: 0, count: numCourses)for prereq in prerequisites {let course = prereq[0]let prereqCourse = prereq[1]graph[prereqCourse].append(course)indegree[course] += 1}var queue = [Int]()for i in 0..<numCourses {if indegree[i] == 0 {queue.append(i)}}var order = [Int]()while !queue.isEmpty {let course = queue.removeFirst()order.append(course)for nextCourse in graph[course] {indegree[nextCourse] -= 1if indegree[nextCourse] == 0 {queue.append(nextCourse)}}}return order.count == numCourses ? order : []}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/pingmian/72923.shtml
繁體地址,請注明出處:http://hk.pswp.cn/pingmian/72923.shtml
英文地址,請注明出處:http://en.pswp.cn/pingmian/72923.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【C++】STL庫面試常問點

STL庫 什么是STL庫 C標準模板庫&#xff08;Standard Template Libiary&#xff09;基于泛型編程&#xff08;模板&#xff09;&#xff0c;實現常見的數據結構和算法&#xff0c;提升代碼的復用性和效率。 STL庫有哪些組件 STL庫由以下組件構成&#xff1a; ● 容器&#xf…

【問題解決】Postman 測試報錯 406

現象 Tomcat 日志 org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.logException Resolved org.springframework.web.HttpMediaTypeNotAcceptableException: No acceptable representation HTTP狀態 406 - 不可接收 的報錯&#xff0c;核心原因 客…

第3節:AWK的特點和優勢

1 第3節&#xff1a;AWK的特點和優勢 AWK是一種功能強大的文本處理工具&#xff0c;具有以下特點和優勢&#xff1a; 1.1.1 簡潔性 AWK的語法簡潔明了&#xff0c;對于簡單的數據處理任務&#xff0c;通常只需編寫簡短的命令即可完成。例如&#xff0c;要從一個文本文件中提…

Flutter 打包 ipa出現錯誤問題 exportArchive

一、錯誤信息: Encountered error while creating the IPA: error: exportArchive: "Runner.app" requires a provisioning profile with the Push Notifications feature. Try distributing the app in Xcode: open /project/your_app/build/ios/archive/Runner.…

STC89C52單片機學習——第28節: [12-2] AT24C02數據存儲秒表(定時器掃描按鍵數碼管)

寫這個文章是用來學習的,記錄一下我的學習過程。希望我能一直堅持下去,我只是一個小白,只是想好好學習,我知道這會很難&#xff0c;但我還是想去做&#xff01; 本文寫于&#xff1a;2025.03.20 51單片機學習——第28節: [12-2] AT24C02數據存儲&秒表&#xff08;定時器掃…

Verilog-HDL/SystemVerilog/Bluespec SystemVerilog vscode 配置

下載 verible https://github.com/chipsalliance/verible的二進制包 然后配置 vscode

STM32使用HAL庫,模擬UART輸出字符串

測試芯片是STM32F103C8T6&#xff0c;直接封裝好了&#xff0c;波特率是 9600 MyDbg.h #ifndef __MYDBG_H #define __MYDBG_H #include "stm32f1xx_hal.h" #include <stdio.h> #include <stdarg.h>/*使用GPIO口 模擬 UART 輸出字符串 */ //初始化調試…

[工控機安全] 使用DriverView快速排查不可信第三方驅動(附詳細圖文教程)

導語&#xff1a; 在工業控制領域&#xff0c;設備驅動程序的安全性至關重要。第三方驅動可能存在兼容性問題、安全漏洞甚至惡意代碼&#xff0c;威脅設備穩定運行。本文將手把手教你使用 DriverView工具&#xff0c;高效完成工控機驅動安全檢查&#xff0c;精準識別可疑驅動&a…

HTML5響應式使用css媒體查詢

HTML 負責搭建頁面結構&#xff0c;CSS 負責樣式設計&#xff0c;并且通過媒體查詢實現了較好的響應式效果&#xff0c;能夠適應不同屏幕尺寸下面就是寫了一個詳細的實例。 CSS 部分 * {margin: 0;padding: 0;box-sizing: border-box; } * 是通配選擇器&#xff0c;會選中頁面…

洛谷P1434 [SHOI2002] 滑雪

P1434 [SHOI2002] 滑雪 - 洛谷 代碼區&#xff1a; #include<algorithm> #include<iostream> #include<cstring> using namespace std;const int MAX 105; int r, c; int arr[MAX][MAX], dp[MAX][MAX]; int xindex[4] {-1,1,0,0};//上下左右 int yindex[…

【操作系統】進程間通信方式

進程間通信方式 前言 / 概述一、管道管道命名管道 二、消息隊列三、共享內存四、信號量信號量概述互斥訪問條件同步信號 五、socket總結 前言 / 概述 每個進程的用戶地址空間都是獨立的&#xff0c;?般而言是不能互相訪問的&#xff0c;但內核空間是每個進程都共享的&#xff…

WPF 布局中的共性尺寸組(Shared Size Group)

1. 什么是共性尺寸組&#xff1f; 在 WPF 的 Grid 布局中&#xff0c;SharedSizeGroup 允許多個 Grid 共享同一列或行的尺寸&#xff0c;即使它們屬于不同的 Grid 也能保持大小一致。這樣可以保證界面元素的對齊性&#xff0c;提高布局的一致性。 SharedSizeGroup 主要用于需…

Netty源碼—2.Reactor線程模型二

大綱 1.關于NioEventLoop的問題整理 2.理解Reactor線程模型主要分三部分 3.NioEventLoop的創建 4.NioEventLoop的啟動 4.NioEventLoop的啟動 (1)啟動NioEventLoop的兩大入口 (2)判斷當前線程是否是NioEventLoop線程 (3)創建一個線程并啟動 (4)NioEventLoop的啟動總結 (…

前端項目中應該如何選擇正確的圖片格式

在前端項目中選擇正確的圖片格式是優化頁面性能、提升用戶體驗的關鍵步驟之一。以下是常見圖片格式的特點、適用場景及選擇建議&#xff0c;幫助你在不同場景下做出最優決策&#xff1a; 一、常見圖片格式對比 格式特點適用場景不適用場景JPEG- 有損壓縮&#xff0c;文件小- 不…

保姆級 STM32 HAL 庫外部中斷教學

1. 外部中斷概述 為什么用外部中斷&#xff1f; 當按鍵按下時&#xff0c;CPU 無需輪詢檢測引腳狀態&#xff0c;而是通過中斷機制立即響應&#xff0c;提高效率&#xff0c;適用于實時性要求高的場景。 關鍵概念 EXTI (External Interrupt/Event Controller)&#xff1a;ST…

Postman高級功能深度解析:Mock Server與自動化監控——構建高效API測試與監控體系

引言&#xff1a;Postman在API開發中的核心價值 在數字化時代&#xff0c;API&#xff08;應用程序編程接口&#xff09;已成為系統間交互的“神經網絡”&#xff0c;其質量直接影響用戶體驗與業務連續性。然而&#xff0c;傳統API測試面臨兩大挑戰&#xff1a; 開發階段依賴…

【程序人生】成功人生架構圖(分層模型)

文章目錄 ?前言?一、根基層——價值觀與使命?二、支柱層——健康與能量?三、驅動層——學習與進化?四、網絡層——關系系統?五、目標層——成就與財富?六、頂層——意義與傳承?外層&#xff1a;調節環——平衡與抗風險?思維導圖 標題詳情作者JosieBook頭銜CSDN博客專家…

【最后203篇系列】020 rocksdb agent

今天還是挺開心的一天&#xff0c;又在工具箱里加了一個工具。嗯&#xff0c;但是快下班的時候也碰到一些不太順心的事&#xff0c;讓我有點惱火。我還真沒想到一個專職的前端&#xff0c;加測試&#xff0c;以及其他一堆人&#xff0c;竟然不知道后端返回的markdown,在前端渲染…

10-- 網絡攻擊防御原理全景解析 | 從單包攻防到DDoS軍團作戰(包你看一遍全記住)

&#x1f6e1;? 網絡攻擊防御原理全景解析 | 從單包攻防到DDoS軍團作戰 如果你也對網絡工程師的內容感興趣的話&#xff0c;歡迎看我的最新文章9–BGP路由黑洞&#xff08;超萬字大解析&#xff09;&#xff1a;網絡世界的“百慕大三角“逃生指南(BGP路由配置實驗含路由黑洞,…

解鎖Python print()函數高級用法

print() 是 Python 中最常用的函數之一,用于將內容輸出到控制臺。雖然它的基本用法非常簡單,但 print() 函數還支持許多高級功能,如格式化輸出、重定向輸出、控制分隔符和結束符等。 1. print() 函數的基本用法 1.1 語法 print() 函數的基本語法如下: print(*objects, …