210. 課程表 II (JAVA)
阿新 • • 發佈:2020-08-02
現在你總共有 n 門課需要選,記為0到n-1。
在選修某些課程之前需要一些先修課程。例如,想要學習課程 0 ,你需要先完成課程1 ,我們用一個匹配來表示他們: [0,1]
給定課程總量以及它們的先決條件,返回你為了學完所有課程所安排的學習順序。
可能會有多個正確的順序,你只要返回一種就可以了。如果不可能完成所有課程,返回一個空陣列。
示例1:
輸入: 2, [[1,0]]
輸出: [0,1]
解釋:總共有 2 門課程。要學習課程 1,你需要先完成課程 0。因此,正確的課程順序為 [0,1] 。
示例2:
輸入: 4, [[1,0],[2,0],[3,1],[3,2]]
輸出: [0,1,2,3] or [0,2,1,3]
因此,一個正確的課程順序是[0,1,2,3] 。另一個正確的排序是[0,2,1,3] 。
說明:
輸入的先決條件是由邊緣列表表示的圖形,而不是鄰接矩陣。詳情請參見圖的表示法。
你可以假定輸入的先決條件中沒有重複的邊。
提示:
這個問題相當於查詢一個迴圈是否存在於有向圖中。如果存在迴圈,則不存在拓撲排序,因此不可能選取所有課程進行學習。
通過 DFS 進行拓撲排序 - 一個關於Coursera的精彩視訊教程(21分鐘),介紹拓撲排序的基本概念。
拓撲排序也可以通過BFS完成。
思路:
本題實質是對有向無環圖的拓撲排序。
拓撲排序的方法步驟:
1. 整理每個節點的後續節點
2. 計算每個點的入度,將入度為0的點放入result,並將其後續節點的入度-1
3. 依此類推
實現方法可以是DFS(通過棧)或是BFS(通過佇列)
方法I:BFS。出佇列一個,就將它後續節點中入度為0的節點放入佇列 (for迴圈包 + while迴圈)
class Solution { public int[] findOrder(int numCourses, int[][] prerequisites) { int[] result = new int[numCourses];int rIndex = 0; //result的下標 int[] pathIn = new int[numCourses]; //預設初始化值為0 Queue<Integer> noPathInQueue = new LinkedList<Integer>(); Map<Integer, List<Integer>> followersMap = new HashMap<>(); List<Integer> followers; int index; //遍歷prerequisites,構建followersMap和pathIn for(int i = 0; i < prerequisites.length; i++){ if(followersMap.containsKey(prerequisites[i][1])){ followers = followersMap.get(prerequisites[i][1]); } else { followers = new ArrayList<Integer>(); } followers.add(prerequisites[i][0]); followersMap.put(prerequisites[i][1], followers); pathIn[prerequisites[i][0]]++; } //遍歷尋找入度為0的點 for(int i = 0; i < numCourses; i++){ if(pathIn[i]==0) { noPathInQueue.offer(i); //佇列滿時返回false;add()則會直接丟擲異常 } } //進行拓撲排序 while(!noPathInQueue.isEmpty()) { index = noPathInQueue.peek(); result[rIndex++] = index; noPathInQueue.poll(); pathIn[index] = -1; //表示已新增到佇列 for(int j = 0; followersMap.containsKey(index) && j < followersMap.get(index).size(); j++){ //將followers的入度-1 pathIn[followersMap.get(index).get(j)]--; if(pathIn[followersMap.get(index).get(j)] == 0){ noPathInQueue.offer(followersMap.get(index).get(j)); } } } if ( rIndex != numCourses) { return new int[0]; } return result; } }
方法II:DFS。出棧一個,就將它後續節點中入度為0的節點入棧,只要棧不為空就還停留在當前for迴圈中(for迴圈包while迴圈)
class Solution { public int[] findOrder(int numCourses, int[][] prerequisites) { int[] result = new int[numCourses]; int rIndex = 0; //result的下標 int[] pathIn = new int[numCourses]; //預設初始化值為0 Stack<Integer> noPathInStack = new Stack<Integer>(); Map<Integer, List<Integer>> followersMap = new HashMap<>(); List<Integer> followers; int index; //遍歷prerequisites,構建followersMap和pathIn for(int i = 0; i < prerequisites.length; i++){ if(followersMap.containsKey(prerequisites[i][1])){ followers = followersMap.get(prerequisites[i][1]); } else { followers = new ArrayList<Integer>(); } followers.add(prerequisites[i][0]); followersMap.put(prerequisites[i][1], followers); pathIn[prerequisites[i][0]]++; } //進行拓撲排序 for(int i = 0; i < numCourses; i++){ //遍歷尋找入度為0的點 if(pathIn[i]==0) { noPathInStack.push(i); pathIn[i] = -1; //表示已新增到棧 } while(!noPathInStack.isEmpty()){ index = noPathInStack.peek(); result[rIndex++] = index; noPathInStack.pop(); for(int j = 0; followersMap.containsKey(index) && j < followersMap.get(index).size(); j++){ //將followers的入度-1 pathIn[followersMap.get(index).get(j)]--; if(pathIn[followersMap.get(index).get(j)] == 0) { noPathInStack.push(followersMap.get(index).get(j)); pathIn[followersMap.get(index).get(j)] = -1; } } } } if ( rIndex != numCourses) { return new int[0]; } return result; } }