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, return the ordering of courses you should take to finish all courses.
There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.
Example 1:
Input: 2, [[1,0]] Output:[0,1] Explanation: There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is[0,1] .
Example 2:
Input: 4, [[1,0],[2,0],[3,1],[3,2]] Output:[0,1,2,3] or [0,2,1,3] Explanation: There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is[0,1,2,3] . Another correct ordering is[0,2,1,3] .
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.
想法如下
- 這題是 207. Course Schedule 的衍生提,但其實觀念是一樣的,都是要用 topological sorting 來解
- 不一樣的點是,這次是要輸出 sorting 後的結果,所以改變的地方只在於,要多用一個 array 來儲存歷遍的結果
code 如下
Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int[] findOrder(int numCourses, int[][] prerequisites) { | |
int[][] DAG = new int[numCourses][numCourses]; | |
int[] indegree = new int[numCourses]; | |
int course, preCourse; | |
for(int i = 0; i < prerequisites.length; i++) { | |
course = prerequisites[i][0]; | |
preCourse = prerequisites[i][1]; | |
DAG[preCourse][course] = 1; | |
indegree[course] ++; | |
} | |
Queue<Integer> queue = new LinkedList<>(); | |
for(int i = 0; i < numCourses; i++) { | |
if(indegree[i] == 0) { | |
queue.offer(i); | |
} | |
} | |
int[] ans = new int[numCourses]; | |
int index = 0; | |
while(!queue.isEmpty()) { | |
course = queue.poll(); | |
ans[index++] = course; | |
for(int i = 0; i < numCourses; i++) { | |
if(DAG[course][i] == 1) { | |
indegree[i]--; | |
if(indegree[i] == 0) { | |
queue.offer(i); | |
} | |
} | |
} | |
} | |
return index == numCourses ? ans : new int[0]; | |
} | |
} |
Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
fun findOrder(numCourses: Int, prerequisites: Array<IntArray>): IntArray { | |
val graph = Array(numCourses) { BooleanArray(numCourses) {false} } | |
val requirement = IntArray(numCourses) {0} | |
var course: Int | |
var preCourse: Int | |
for(i in prerequisites.indices) { | |
course = prerequisites[i][0] | |
preCourse = prerequisites[i][1] | |
graph[preCourse][course] = true | |
++requirement[course] | |
} | |
//>> find course without requirement | |
val queue = mutableListOf<Int>() | |
for(i in requirement.indices) { | |
if(requirement[i] == 0) { | |
queue.add(i) | |
} | |
} | |
val ans = mutableListOf<Int>() | |
while(queue.isNotEmpty()) { | |
course = queue.first() | |
queue.removeAt(0) | |
ans.add(course) | |
for(i in 0 until numCourses) { | |
if(graph[course][i]) { | |
graph[course][i] = false | |
--requirement[i] | |
if(requirement[i] == 0) { | |
queue.add(i) | |
} | |
} | |
} | |
} | |
return if (ans.size == numCourses) { | |
ans.toIntArray() | |
} else { | |
intArrayOf() | |
} | |
} | |
} |
沒有留言:
張貼留言