C++實(shí)現(xiàn)LeetCode(207.課程清單)
[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 pair: [0,1]
Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?
Example 1:
Input: 2, [[1,0]]
Output: true
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0. So it is possible.
Example 2:
Input: 2, [[1,0],[0,1]]
Output: false
Explanation: 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.
Note:
- The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
- You may assume that there are no duplicate edges in the input prerequisites.
Hints:
- This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses.
- There are several ways to represent a graph. For example, the input prerequisites is a graph represented by a list of edges. Is this graph representation appropriate?
- Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort.
- Topological sort could also be done via BFS.
這道課程清單的問(wèn)題對(duì)于我們學(xué)生來(lái)說(shuō)應(yīng)該不陌生,因?yàn)樵谶x課的時(shí)候經(jīng)常會(huì)遇到想選某一門課程,發(fā)現(xiàn)選它之前必須先上了哪些課程,這道題給了很多提示,第一條就告訴了這道題的本質(zhì)就是在有向圖中檢測(cè)環(huán)。 LeetCode 中關(guān)于圖的題很少,有向圖的僅此一道,還有一道關(guān)于無(wú)向圖的題是 Clone Graph。個(gè)人認(rèn)為圖這種數(shù)據(jù)結(jié)構(gòu)相比于樹啊,鏈表啊什么的要更為復(fù)雜一些,尤其是有向圖,很麻煩。第二條提示是在講如何來(lái)表示一個(gè)有向圖,可以用邊來(lái)表示,邊是由兩個(gè)端點(diǎn)組成的,用兩個(gè)點(diǎn)來(lái)表示邊。第三第四條提示揭示了此題有兩種解法,DFS 和 BFS 都可以解此題。先來(lái)看 BFS 的解法,定義二維數(shù)組 graph 來(lái)表示這個(gè)有向圖,一維數(shù)組 in 來(lái)表示每個(gè)頂點(diǎn)的入度。開始先根據(jù)輸入來(lái)建立這個(gè)有向圖,并將入度數(shù)組也初始化好。然后定義一個(gè) queue 變量,將所有入度為0的點(diǎn)放入隊(duì)列中,然后開始遍歷隊(duì)列,從 graph 里遍歷其連接的點(diǎn),每到達(dá)一個(gè)新節(jié)點(diǎn),將其入度減一,如果此時(shí)該點(diǎn)入度為0,則放入隊(duì)列末尾。直到遍歷完隊(duì)列中所有的值,若此時(shí)還有節(jié)點(diǎn)的入度不為0,則說(shuō)明環(huán)存在,返回 false,反之則返回 true。代碼如下:
解法一:
class Solution {
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
vector<vector<int>> graph(numCourses, vector<int>());
vector<int> in(numCourses);
for (auto a : prerequisites) {
graph[a[1]].push_back(a[0]);
++in[a[0]];
}
queue<int> q;
for (int i = 0; i < numCourses; ++i) {
if (in[i] == 0) q.push(i);
}
while (!q.empty()) {
int t = q.front(); q.pop();
for (auto a : graph[t]) {
--in[a];
if (in[a] == 0) q.push(a);
}
}
for (int i = 0; i < numCourses; ++i) {
if (in[i] != 0) return false;
}
return true;
}
};
下面來(lái)看 DFS 的解法,也需要建立有向圖,還是用二維數(shù)組來(lái)建立,和 BFS 不同的是,像現(xiàn)在需要一個(gè)一維數(shù)組 visit 來(lái)記錄訪問(wèn)狀態(tài),這里有三種狀態(tài),0表示還未訪問(wèn)過(guò),1表示已經(jīng)訪問(wèn)了,-1 表示有沖突。大體思路是,先建立好有向圖,然后從第一個(gè)門課開始,找其可構(gòu)成哪門課,暫時(shí)將當(dāng)前課程標(biāo)記為已訪問(wèn),然后對(duì)新得到的課程調(diào)用 DFS 遞歸,直到出現(xiàn)新的課程已經(jīng)訪問(wèn)過(guò)了,則返回 false,沒(méi)有沖突的話返回 true,然后把標(biāo)記為已訪問(wèn)的課程改為未訪問(wèn)。代碼如下:
解法二:
class Solution {
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
vector<vector<int>> graph(numCourses, vector<int>());
vector<int> visit(numCourses);
for (auto a : prerequisites) {
graph[a[1]].push_back(a[0]);
}
for (int i = 0; i < numCourses; ++i) {
if (!canFinishDFS(graph, visit, i)) return false;
}
return true;
}
bool canFinishDFS(vector<vector<int>>& graph, vector<int>& visit, int i) {
if (visit[i] == -1) return false;
if (visit[i] == 1) return true;
visit[i] = -1;
for (auto a : graph[i]) {
if (!canFinishDFS(graph, visit, a)) return false;
}
visit[i] = 1;
return true;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/207
參考資料:
https://leetcode.com/problems/course-schedule/
https://leetcode.com/problems/course-schedule/discuss/58524/Java-DFS-and-BFS-solution
https://leetcode.com/problems/course-schedule/discuss/58516/Easy-BFS-Topological-sort-Java
到此這篇關(guān)于C++實(shí)現(xiàn)LeetCode(207.課程清單)的文章就介紹到這了,更多相關(guān)C++實(shí)現(xiàn)課程清單內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
數(shù)據(jù)結(jié)構(gòu) 紅黑樹的詳解
這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu) 紅黑樹的詳解的相關(guān)資料,數(shù)據(jù)結(jié)構(gòu)中的二叉樹查找,紅黑樹的講解,需要的朋友可以參考下2017-07-07
C語(yǔ)言 如何求兩整數(shù)的最大公約數(shù)與最小公倍數(shù)
這篇文章主要介紹了C語(yǔ)言中如何求兩整數(shù)的最大公約數(shù)與最小公倍數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
C語(yǔ)言數(shù)組應(yīng)用實(shí)現(xiàn)掃雷游戲
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言數(shù)組應(yīng)用實(shí)現(xiàn)掃雷游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
C語(yǔ)言聯(lián)合體類型的實(shí)現(xiàn)
聯(lián)合體也是一種構(gòu)造數(shù)據(jù)類型,和結(jié)構(gòu)體類型一樣,它也是由各種不同類型的數(shù)據(jù)組成,本文主要介紹了C語(yǔ)言聯(lián)合體類型的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02
VC實(shí)現(xiàn)ODBC數(shù)據(jù)庫(kù)操作實(shí)例解析
這篇文章主要介紹了VC實(shí)現(xiàn)ODBC數(shù)據(jù)庫(kù)操作的方法,非常有實(shí)用價(jià)值,需要的朋友可以參考下2014-07-07
C語(yǔ)言中動(dòng)態(tài)內(nèi)存分配malloc、calloc和realloc函數(shù)解析
C語(yǔ)言跟內(nèi)存申請(qǐng)相關(guān)的函數(shù)主要有 alloca、calloc、malloc、free、realloc等,下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言中動(dòng)態(tài)內(nèi)存分配malloc、calloc和realloc函數(shù)的相關(guān)資料,需要的朋友可以參考下2022-03-03

