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
- BUU BURP COURSE 1
- Format of Index Blocks (207)ORMIndexBloC
- 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
- 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
- STM32F207時鐘系統解析
- Codeforces Round #207 (Div. 2) A. Group of Students
- [譯][A crash course in WebAssembly] 為什麼WebAssembly這麼快Web