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;
}
}
相關文章
- 207. Course Schedule
- LeetCode 210 course schedule 2LeetCode
- [LeetCode] 210. Course Schedule IILeetCode
- leetcode【210】【Depth-first Search】Course Schedule II【c++版本】LeetCodeC++
- LeetCode 207. 課程表(Medium)LeetCode
- kaggle course --NLP
- BUU XSS COURSE 1
- BUU BURP COURSE 1
- TVM:Schedule的理解
- SpringBoot 中的 @ScheduleSpring Boot
- cocos2dx update scheduleUpdate to update or schedule(schedule_selector(fun),dt)
- [譯][A crash course in WebAssembly] assemblyWeb
- 207. 課程表
- springboot整合schedule(深度理解)Spring Boot
- schedule 定時任務
- TypeScript Crash Course: Property Access ModifiersTypeScript
- ErrorException In FilesystemAdapter.php line 207ErrorExceptionAPTPHP
- pwn.college Fundementals Assembly Crash Course
- python 定時任務之 schedulePython
- Uncode-Schedule框架原始碼分析框架原始碼
- ZOJ 3956——Course Selection System(01揹包)
- 使用 telescope 檢視 schedule 執行狀態
- STM32F207時鐘系統解析
- STM32F207DAC實驗記錄
- Schedule 排程系統設計(單機版)
- setInterval 、 settimeout 、clearInterval 用法(特殊情況下代替schedule)
- [轉帖]Release Schedule of Current Database Releases (Doc ID 742060.1)Database
- Laravel-Schedule 計劃任務「原理了解」Laravel
- scrapyd schedule.json setting 傳入多個值JSON
- LiteOS核心原始碼分析:任務LOS_Schedule原始碼
- [CS131] Lecture 1 Course Introduction課程介紹
- [譯][A crash course in WebAssembly] 創作並使用 WebAssembly 模組Web
- [譯][A crash course in WebAssembly] 為什麼WebAssembly這麼快Web
- [譯][A crash course in WebAssembly] WebAssembly的進度和計劃Web
- 力扣刷題筆記:207. 課程表力扣筆記
- Python定時任務輕量解決方案---SchedulePython
- TVM Compiler中文教程:TVM排程原語(Schedule Primitives)CompileMIT
- [譯][A crash course in WebAssembly] Just-in-time(JIT)編譯器速成課Web編譯