LeetCode 207 Course Schedule
given a 2D array, represents for the class and it’s prerequesite class
like [0,1], it means that we should take class 1 before class 0.
so it is a problem about cycle detection.
public boolean canFinish(int numCourses, int[][] prerequisites)
return if it is possible for us to finish all the courses.
idea:
just test if there exist any cycle in starting from each node, if there is, then we can’t iterate every class, but if there isn’t, then we can.
class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
HashMap<Integer, List<Integer>> map = new HashMap<>();
for (int[] class_pre: prerequisites) {
if (map.containsKey(class_pre[1])) {
map.get(class_pre[1]).add(class_pre[0]);
} else {
map.put(class_pre[1], new ArrayList<>());
map.get(class_pre[1]).add(class_pre[0]);
}
}
boolean[] visited = new boolean[numCourses];
for (int i = 0; i < numCourses; i++) {
if (isCycle(i, map, visited)) {
return false;
}
}
return true; //if there is no cycle in this graph, then we can finish all the courses
}
private boolean isCycle(int currentNode, HashMap<Integer, List<Integer>> map, boolean[] visited) {
if (visited[currentNode]) {
return true;
}
if (!map.containsKey(currentNode)) {
return false;//it actually means that we reached onee path's end
}
visited[currentNode] = true;
boolean res = false;
for (Integer neighbor: map.get(currentNode)) {
res = isCycle(neighbor, map, visited);
if (res) {
break;
}
}
//if any of the res is true, then it is true
visited[currentNode] = false;
return res;
}
}
相關文章
- LeetCode-Course ScheduleLeetCode
- LeetCode-Course Schedule IILeetCode
- LeetCode 210 course schedule 2LeetCode
- [LeetCode] 210. Course Schedule IILeetCode
- leetcode【210】【Depth-first Search】Course Schedule II【c++版本】LeetCodeC++
- LeetCode 207. 課程表(Medium)LeetCode
- Copy client scheduleclient
- [譯][A crash course in WebAssembly] assemblyWeb
- BUU XSS COURSE 1
- Format of Index Blocks (207)ORMIndexBloC
- TypeScript Crash Course: Property Access ModifiersTypeScript
- SAP outsource training courseAI
- sql server schedule scriptSQLServer
- oracle schedule 使用大全Oracle
- Schedule Manager Tell 指令
- TVM:Schedule的理解
- pwn.college Fundementals Assembly Crash Course
- schedule 定時任務
- Taught Oracle database OCP training courseOracleDatabaseAI
- Oracle 10g OCM Practicum Course PrerequisiteOracle 10gUI
- SpringBoot 中的 @ScheduleSpring Boot
- 在 Docker 中使用 Laravel scheduleDockerLaravel
- springboot整合schedule(深度理解)Spring Boot
- BIP Schedule相關資料表
- mysql計劃任務:event scheduleMySql
- ErrorException In FilesystemAdapter.php line 207ErrorExceptionAPTPHP
- [譯][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
- [譯][A crash course in WebAssembly] 為什麼WebAssembly這麼快Web
- [譯][A crash course in WebAssembly] WebAssembly的進度和計劃Web
- 使用 telescope 檢視 schedule 執行狀態
- java定時器schedule和scheduleAtFixedRate區別Java定時器