-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCourseScheduleII210.cpp
More file actions
96 lines (86 loc) · 3.54 KB
/
CourseScheduleII210.cpp
File metadata and controls
96 lines (86 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* Problem: LeetCode 210 - Course Schedule II
*
* 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.
* 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 (due to a cycle), return an empty array.
*
* Intuition:
* This problem is a direct application of Topological Sorting on a Directed Graph.
* We can use Kahn's Algorithm (BFS) to achieve this. By keeping track of the in-degree
* (number of prerequisites) for each course, we can process courses that currently
* have no prerequisites (in-degree == 0). As we process a course, we reduce the in-degree
* of all its dependent courses, simulating "taking" the prerequisite.
*
* Approach:
* 1. Build an adjacency list (graph) from the prerequisites array, where an edge u -> v
* means course u is a prerequisite for course v.
* 2. Compute the in-degree (incoming edges) for each course.
* 3. Initialize a queue and push all courses with an in-degree of 0 (ready to be taken).
* 4. While the queue is not empty:
* a. Pop a course and add it to the result array.
* b. Decrement the in-degree of all its neighbors (courses that depend on it).
* c. If a neighbor's in-degree drops to 0, push it into the queue.
* 5. Cycle Detection: If the result array's size equals numCourses, return the result.
* Otherwise, a cycle exists (some in-degrees never reached 0), so return an empty array.
*
* Time Complexity: O(V + E) where V is numCourses and E is the number of prerequisites.
* We process each node and its outgoing edges exactly once.
* Space Complexity: O(V + E) to store the adjacency list graph, queue, and indegree array.
*/
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<int> findOrder(int numCourses, vector<vector<int>> &prerequisites)
{
int V = numCourses;
// Track the number of prerequisites for each course
vector<int> indegree(V, 0);
// Adjacency list to represent the graph (prerequisite -> dependent courses)
vector<vector<int>> adj(V);
// Build the graph and calculate in-degrees
for (int i = 0; i < prerequisites.size(); i++)
{
int u = prerequisites[i][1]; // Prerequisite course
int v = prerequisites[i][0]; // Dependent course
adj[u].push_back(v);
indegree[v]++;
}
queue<int> q;
// Start with courses that have no prerequisites
for (int i = 0; i < V; i++)
if (indegree[i] == 0)
q.push(i);
vector<int> res;
// Process courses using BFS (Kahn's Algorithm for Topological Sort)
while (!q.empty())
{
int curr = q.front();
q.pop();
res.push_back(curr);
// Unlock dependent courses by reducing their in-degree
for (int v : adj[curr])
{
indegree[v]--;
// If a dependent course has no more prerequisites, it's ready to be taken
if (indegree[v] == 0)
q.push(v);
}
}
// If we couldn't take all courses, there must be a cycle
if (res.size() != V)
return {}; // cycle detected
return res;
}
};
int main()
{
return 0;
}