LeetCode 210 course schedule 2
now instead of just checking on whether we can finish all the courses or not, we are now asked to return the order of courses that you should take to finish all the courses.
we only needs to return one path if there exist any of these paths
public int[] findOrder(int numCourses, int[][] prerequisites)
so the idea for this problem is:
we starting from each head we can found, and check them out on it’s every neighbor, and we go and go until the queue is empty(bfs) is finished. and during the whole process, keeps recording of every node we’ve been through, and in the last, check the number of different node we have been through, if the number is numCourses, then we found it, return the res.
BFS solution, using queue
class Solution {
public int[] findOrder(int numCourses, int[][] prerequisites) {
int[] indegree = new int[numCourses];
int[] res = new int[numCourses];
int k = 0;
for (int[] pair : prerequisites) { //get the indegree of each node
indegree[pair[0]]++;
}
Queue<Integer> queue = new LinkedList<>();
for (int i = 0; i < indegree.length; i++) { //check which of indgree is 0, if it is 0, we can start from it
if (indegree[i] == 0) {
queue.offer(i);
res[k++] = i;
}
}
//now all of our head is in the queue
while (!queue.isEmpty()) {
int pre = queue.poll();
for (int[] pair : prerequisites) { //we check each pair
if (pair[1] == pre) {
indegree[pair[0]]--;
if (indegree[pair[0]] == 0) { //this should be 0, because otherwise it means there are other node that into this too
queue.offer(pair[0]);
res[k++] = pair[0];
}
}
}
}
return (k == numCourses) ? res : new int[0];
}
}
相關文章
- [LeetCode] 210. Course Schedule IILeetCode
- leetcode【210】【Depth-first Search】Course Schedule II【c++版本】LeetCodeC++
- LeetCode-Course ScheduleLeetCode
- LeetCode-Course Schedule IILeetCode
- LeetCode 207 Course ScheduleLeetCode
- Copy client scheduleclient
- [譯][A crash course in WebAssembly] assemblyWeb
- BUU XSS COURSE 1
- BUU BURP COURSE 1
- Codeforces Round #210 (Div. 2) A. Levko and Table
- sql server schedule scriptSQLServer
- oracle schedule 使用大全Oracle
- Schedule Manager Tell 指令
- TVM:Schedule的理解
- TypeScript Crash Course: Property Access ModifiersTypeScript
- SAP outsource training courseAI
- schedule 定時任務
- pwn.college Fundementals Assembly Crash Course
- SpringBoot 中的 @ScheduleSpring Boot
- 在 Docker 中使用 Laravel scheduleDockerLaravel
- Taught Oracle database OCP training courseOracleDatabaseAI
- Oracle 10g OCM Practicum Course PrerequisiteOracle 10gUI
- LeetCode Week 2LeetCode
- LeetCode2:LeetCode
- Cocos2dx之定時器schedule,scheduleUpdate,scheduleOnce的使用定時器
- springboot整合schedule(深度理解)Spring Boot
- BIP Schedule相關資料表
- mysql計劃任務:event scheduleMySql
- QT210開發板學習(2): 透過DNW點亮LED燈QT
- WSL2連線USB裝置(以USRP B210為例)
- [譯][A crash course in WebAssembly] 創作並使用 WebAssembly 模組Web
- SAP ABAP Programming Tutorials - Free Training CourseAI
- Source Code for Unity3D Course Example ApplicationsUnity3DAPP
- 關於領取OCP證書Hands-on Course
- Uncode-Schedule框架原始碼分析框架原始碼
- oracle schedule 任務失敗處理Oracle
- Oracle Scheduler(4)job呼叫program和scheduleOracle
- python 定時任務之 schedulePython