207. Course Schedule
Determine if it is possible to finish all courses given their prerequisites.
Approach: Topological Sort
- in-degree: In a directed graph, the in-degree of a node is the number of edges pointing into that node.
- out-degree: In a directed graph, the out-degree of a node is the number of edges going out from that node — in other words, how many other nodes it points to.
- DAG, Directed Acyclic Graph (有向无环图)
We build a graph and perform topological sort to process all courses. If there is no cycle detected, return true. Otherwise, return false.
Example:
[ai, bi] // to take course ai, you must first take course bi
// ai depends on bi, so the graph looks like
bi --> ai // inDegree[ai] = 1
// inDegree[x] = the number of prerequisites for course x.
// in graph terms, it’s the number of incoming edges to node x.
// means: from course b, you can go to course a
graph[bi].push_back(ai);- Build an adjacency list graph, and an inDegree array to track the number of prerequisites for each course.
- Start with courses that have no prerequisites (inDegree == 0)
- Use BFS to process those courses, reducing the inDegree of dependent courses.
- If we can process all courses → no cycle → return true.
from collections import defaultdict, deque
class Solution:
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
graph = defaultdict(list)
indegree = [0] * numCourses
for course, prereq in prerequisites:
graph[prereq].append(course)
indegree[course] += 1
qu = deque([c for c in range(numCourses) if indegree[c] == 0])
visited = 0
while qu:
node = qu.popleft()
visited += 1
for neighbor in graph[node]:
indegree[neighbor] -= 1
if indegree[neighbor] == 0:
qu.append(neighbor)
return visited == numCourses- Time: — where is the number of courses and is the number of prerequisites.
- Space: — for the graph and in-degree array.
Last updated on
695. Max Area of Island
Given a 2D grid of 0s and 1s, find the maximum area of an island.
994. Rotting Oranges
You are given an m x n grid where each cell can have one of three values: 0 representing an empty cell, 1 representing a fresh orange, or 2 representing a rotten orange. Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten. Return the minimum number of minutes that must pass until all oranges are rotten. If it's impossible, return -1.