class Solution {
public:
vector<int> findOrder(int numCourses, vector<vector<int>> prerequisites) {
vector<vector<int>> graph(numCourses);
vector<int> indegree(numCourses, 0);
for (auto &e : prerequisites) {
graph[e[1]].push_back(e[0]);
++indegree[e[0]];
}
queue<int> q;
for (int i = 0; i < indegree.size(); ++i) {
if (indegree[i] == 0) {
q.push(i);
}
}
vector<int> answer;
while (!q.empty()) {
int curr = q.front();
q.pop();
for (auto &next : graph[curr]) {
--indegree[next];
if (indegree[next] == 0) {
q.push(next);
}
}
--numCourses;
answer.push_back(curr);
}
if (numCourses != 0) {
return {};
}
return answer;
}
};
My Solution Solution 1: DFS (recursion) The Key Idea for Solving This Coding Question C++ Code /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right;
Jun 6, 2025MyCircularQueueO(k)
Apr 20, 2025O(m)
Mar 4, 2025O(n)n is the length of nums.
Feb 19, 2025or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up