Leetcode 210. Course Schedule II
You are given a total of numCourses
courses labeled from 0
to numCourses-1
. You are also given an array prerequisites
where prerequisites[i] = [ai, bi]
indicates that you must take course bi
before course ai
.
[0, 1]
indicates that to take course 0
you have to first take course 1
.Return the ordering of courses you should take to finish all courses. If there are many valid answers, return any of them. If it is impossible to finish all courses, return an empty array.
Q: Can there be duplicate pairs in the prerequisites?
A: No, each pair [ai, bi]
is unique and indicates one specific prerequisite relationship.
Q: Can the courses have no prerequisites? A: Yes, some courses can have no prerequisites, and you can take them anytime.
Q: What should be returned if it’s impossible to complete all courses? A: An empty array should be returned.
Here’s the C++ solution using Kahn’s Algorithm (Breadth-First Search) for Topological Sorting of a Directed Acyclic Graph (DAG):
#include <vector>
#include <queue>
#include <unordered_map>
using namespace std;
class Solution {
public:
vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
// Initialize graph and in-degree count
vector<vector<int>> graph(numCourses);
vector<int> inDegree(numCourses, 0);
// Build the graph
for (const auto& pair : prerequisites) {
int course = pair[0];
int prerequisite = pair[1];
graph[prerequisite].push_back(course);
inDegree[course]++;
}
// Find all starting points (nodes with in-degree 0)
queue<int> q;
for (int i = 0; i < numCourses; ++i) {
if (inDegree[i] == 0) {
q.push(i);
}
}
// Perform the topological sort
vector<int> order;
while (!q.empty()) {
int current = q.front();
q.pop();
order.push_back(current);
for (int neighbor : graph[current]) {
if (--inDegree[neighbor] == 0) {
q.push(neighbor);
}
}
}
// Check if topological sort is possible
if (order.size() != numCourses) {
return {};
}
return order;
}
};
numCourses
, a cycle exists so it’s impossible to complete all courses; return an empty array in that case.Thus, the overall time complexity is O(V + E).
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?