Course Schedule
Detailed guide and Python implementation for the 'Course Schedule' problem.
1. Concept Overview
The 'Course Schedule' problem is a key challenge in the Graphs section.
This implementation focuses on easy-level logic in Python.
We prioritize technical accuracy and code readability in our provided solutions.
2. Real-World Applications
3. Visual Intuition
Visualizing the logic flow for Course Schedule.
4. Prerequisites
5. Step-by-Step Thinking
1. Understand the problem
Read the problem statement for Course Schedule carefully.
2. Formulate brute force
Draft a simple iterative solution.
3. Identify inefficiency
Look for redundant calculations.
4. Optimize search path
Use hashing or sorting to speed up the process.
5. Final Implementation
Clean up the code for production standards.
Problem Statement
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] = [ai, bi] indicates that you must take course bi first if you want to take course ai.
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.
Write a function canFinish(numCourses: int, prerequisites: List[List[int]]) -> bool.
- •1 <= numCourses <= 2000
- •0 <= len(prerequisites) <= 5000
- •prerequisites[i].length == 2
- •0 <= ai, bi < numCourses
Examples
numCourses = 2, prerequisites = [[1,0]]
True
To take course 1 you should have finished course 0. So it is possible.
numCourses = 2, prerequisites = [[1,0],[0,1]]
False
To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. This is a cycle and therefore impossible.
Need a Hint?
Edge Cases to Watch
- Empty input structures
- Single element inputs
- Large numerical bounds
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
Interview Insights & Variations
Complexity Analysis Breakdown
Why Time: Directly evaluates all possibilities.
Why Space: Uses standard local memory.
Why Time: Optimized paths reduce total operations.
Why Space: May trade memory for speed.
Optimized Solution Python Code
Optimized Solution Python Code
def can_finish_opt(numCourses, prerequisites):
preMap = {i: [] for i in range(numCourses)}
for crs, pre in prerequisites: preMap[crs].append(pre)
visiting = set()
def dfs(crs):
if crs in visiting: return False
if preMap[crs] == []: return True
visiting.add(crs)
for pre in preMap[crs]:
if not dfs(pre): return False
visiting.remove(crs)
preMap[crs] = []
return True
for crs in range(numCourses):
if not dfs(crs): return False
return TrueBrute Force Code (Spoiler Guarded)
Brute Force Code (Spoiler Guarded)
def can_finish_brute(numCourses, prerequisites):
adj = {i: [] for i in range(numCourses)}
for crs, pre in prerequisites: adj[crs].append(pre)
def has_cycle(crs, visited):
if crs in visited: return True
visited.add(crs)
for pre in adj[crs]:
if has_cycle(pre, visited): return True
visited.remove(crs)
return False
for crs in range(numCourses):
if has_cycle(crs, set()): return False
return TrueAlgorithm Pattern Checklist
When dealing with Graphs data patterns.
Core Prerequisites
Revision Key Notes
Common Mistakes & Pitfalls
Related Questions
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Loops
Learn how to use Python loops to iterate over data. Master for loops, while loops, break, continue, and loop best practices with interactive examples.
How to Sort a List in Python
Learn how to sort a list in Python using the sort() method and the sorted() function. Discover custom key sorting and reverse order examples.
Python String Methods
A complete reference guide for Python string manipulation. Master formatting, searching, splitting, replacing, and checking string properties.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.