210. Course Schedule II
Medium NeetCode 150 ↗ Open on LeetCode Source: NeetCode 150
Quick review
- Pattern
- Graphs
- Time
- —
- Space
- —
Standard Solution (C++)
SummaryGiven a list of prerequisite pairs for N courses, find a valid order in which all courses can be completed, or return an empty array if no such order exists (i.e., a cycle is present).
Approach
- Model courses and prerequisites as a directed graph, then apply Kahn's algorithm (BFS-based topological sort).
- Compute in-degrees for every node; seed the queue with all zero-in-degree nodes (no prerequisites).
- Repeatedly dequeue a course, append it to the result, and decrement the in-degree of its dependents — re-enqueue any that reach zero.
- If the result contains all N courses the graph is acyclic and the ordering is valid; otherwise a cycle exists and we return
{}.
#include <vector>
#include <queue>
using namespace std;
class Solution {
public:
vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
// Build adjacency list (prerequisite -> course) and track in-degrees.
vector<vector<int>> adj(numCourses);
vector<int> inDegree(numCourses, 0);
for (const auto& p : prerequisites) {
int course = p[0], pre = p[1];
adj[pre].push_back(course);
++inDegree[course];
}
// Seed queue with all courses that have no prerequisites.
queue<int> q;
for (int i = 0; i < numCourses; ++i)
if (inDegree[i] == 0) q.push(i);
vector<int> order;
order.reserve(numCourses);
while (!q.empty()) {
int cur = q.front(); q.pop();
order.push_back(cur);
for (int next : adj[cur]) {
if (--inDegree[next] == 0)
q.push(next);
}
}
// A complete topological ordering exists only when every course is visited.
return (static_cast<int>(order.size()) == numCourses) ? order : vector<int>{};
}
};
Time O(V + E) where V = numCourses and E = number of prerequisite pairs Space O(V + E) for the adjacency list and in-degree array
A canonical reference solution. Problem statement © LeetCode — see the official link above.
My Solution (C++)
#include <vector>
#include <queue>
using namespace std;
class Solution {
public:
vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
// topological sort
// 1. create adj list
// 2. start from those inDegree=0, do bfs
vector<vector<int>> adj(numCourses);
vector<int> inDegree(numCourses, 0);
vector<int> res;
for(const auto& p: prerequisites){
adj[p[1]].push_back(p[0]); // pre: [nexts]
++inDegree[p[0]];
}
queue<int> q; // record all inDegree=0
for(int i=0; i<numCourses; ++i){
if(inDegree[i]==0){
q.push(i);
}
}
while(!q.empty()){
int qlen=q.size();
for(int i=0; i<qlen; ++i){
int cur=q.front();
q.pop();
res.push_back(cur);
for(const int v: adj[cur]){
--inDegree[v];
if(inDegree[v]==0){
q.push(v);
}
}
}
}
return res.size()==numCourses?res:vector<int>{};
}
};
// Time: O(V+E) where V is the number of courses and E is the number of prerequisites
// Space: O(V+E) for the adjacency list and inDegree array My own solution and notes, as I wrote them.
▶ Run it — editable in-browser playground with a sample driver C++
Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.
C++ ▶ Runnable Auto-generated driver with sample input — unverified.
Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.