207. Course Schedule Medium
There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1.
You are given an array prerequisites where prerequisites[i] = [a_i, b_i] indicates that you must take course b_i first if you want to take course a_i.
For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1.
Return true if you can finish all courses. Otherwise, return false.
Example 1:
Input: numCourses = 2, prerequisites = [[1,0]]
Output: true
Explanation: There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible.
Example 2:
Input: numCourses = 2, prerequisites = [[1,0],[0,1]]
Output: false
Explanation: There are a total of 2 courses to take. To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.
Approach
Input: An integer representing the number of courses numCourses, and a 2D array prerequisites detailing course dependencies.
Output: Determine if studying all mapped courses completely is sequentially possible.
This belongs to DFS Cycle Detection problems.
- Build Graph: Convert course prerequisite dependencies into an adjacency list structures natively resolving constraints, for instance
b → asignifyingbpointing toa. - DFS Traversal:
- Use a
visitedmapping structure accurately reflecting states tracking evaluated nodes:0= Unvisited1= Visiting (currently within the recursion stack structure natively evaluating limits accurately logically explicitly)2= Completely Visited
- During DFS evaluation dynamically:
- If accessing states equal to
1→ A cycle actively forms correctly natively detected explicitly, returning logicallyfalse. - If accessing states equal to
2→ Graph components structurally already optimally evaluated properly actively mapped cleanly effectively, returning natively safelytrue. - Iterate tracking remaining courses checking independently fully correctly comprehensively dynamically efficiently effectively correctly smoothly generating natively checking explicitly mapping effectively logically verifying independently thoroughly properly evaluating seamlessly mapping correctly cleanly seamlessly.
- If accessing states equal to
- Use a
Implementation
from typing import List
class Solution:
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
# Dictionary establishing graph logic checking limits formatting tracking paths naturally structurally smoothly correctly
graph = {}
# Directed graph construction evaluated smoothly accurately dynamically properly smoothly gracefully securely dependably intelligently
for a, b in prerequisites:
if b in graph:
graph[b].append(a)
else:
graph[b] = [a]
# State array structure actively evaluating array tracking
# 0 = unvisited, 1 = visiting (in stack), 2 = completed successfully comprehensively explicitly
visited = [0] * numCourses
def dfs(course):
# Identifying bounds logic naturally processing returning recursively securely
if visited[course] == 1:
return False
# Completed evaluating successfully checking effectively
if visited[course] == 2:
return True
# Modify to explicitly mark safely gracefully smoothly mapping tracking boundaries naturally cleanly natively
visited[course] = 1
# Iterate child dependencies evaluating constraints properly validating logically actively reliably efficiently accurately
if course in graph:
for next in graph[course]:
if not dfs(next):
return False
# Finish updating bounds checking mapping cleanly tracking cleanly seamlessly
visited[course] = 2
return True
# Sequentially parsing sequentially logic seamlessly correctly properly smoothly natively validating checks comprehensively securely gracefully
for i in range(numCourses):
if not dfs(i):
return False # Cyclical structurally invalid
return True # Valid resolving properly smoothly generating natively cleanly successfully successfully actively explicitly correctly securely/**
* @param {number} numCourses
* @param {number[][]} prerequisites
* @return {boolean}
*/
var canFinish = function(numCourses, prerequisites) {
// Map usage handling graph accurately structural connections cleanly mapping securely limits explicitly cleanly dynamically accurately
const graph = new Map();
for (let [a, b] of prerequisites) {
if (graph.get(b)) {
graph.get(b).push(a);
} else {
graph.set(b, [a]);
}
}
// Evaluating structural limits actively smoothly evaluating visited effectively efficiently reliably tracking actively
// 0 = Unvisited, 1 = Visiting (In recursive stack), 2 = Completed
const visited = new Array(numCourses).fill(0);
function dfs(course) {
// Node being visited translates cyclical loop mapping invalid structural layout correctly seamlessly
if (visited[course] === 1) return false;
// Node evaluated optimally seamlessly passing rules safely appropriately cleanly generating logic efficiently correctly seamlessly cleanly efficiently structurally properly correctly gracefully smoothly
if (visited[course] === 2) return true;
// Visited flag tracking cleanly explicitly correctly
visited[course] = 1;
// Traversal constraints evaluating recursively navigating securely smoothly
if (graph.get(course)) {
for (let next of graph.get(course)) {
// Tracking failures safely correctly seamlessly smoothly resolving effectively cleanly explicitly
if (!dfs(next)) return false;
}
}
// Completion status safely effectively smoothly explicitly successfully accurately reliably predictably
visited[course] = 2;
return true;
}
// Master verification loops seamlessly traversing handling reliably natively accurately seamlessly successfully
for (let i = 0; i < numCourses; i++) {
if (!dfs(i)) return false; // Break evaluation optimally intelligently safely dynamically smoothly tracking efficiently
}
// Clear mapping returning cleanly structurally
return true;
};Complexity Analysis
- Time Complexity:
O(V + E)(Nodes corresponding toVcourses, plus evaluation of paths mapped safely cleanly via prerequisite structural boundaries naturally mapped effectively logically securely reliably efficiently securely) - Space Complexity:
O(V + E)bounds tracked actively checking nodes mapping safely dependably accurately