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
.
- For example, the pair
[0, 1]
, indicates that to take course0
you have to first take course1
.
Return true
if you can finish all courses. Otherwise, return false
.
Example 1:
Input: numCourses = 2, prerequisites = [[1,0]] Output: true Explanation: There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible.
Example 2:
Input: numCourses = 2, prerequisites = [[1,0],[0,1]] Output: false Explanation: There are a total of 2 courses to take. To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.
Constraints:
1 <= numCourses <= 105
0 <= prerequisites.length <= 5000
prerequisites[i].length == 2
0 <= ai, bi < numCourses
- All the pairs prerequisites[i] are unique.
class Solution:
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
edges = defaultdict(list)
indegree = [0] * numCourses
for a, b in prerequisites:
edges[b].append(a)
indegree[a] += 1
q = deque()
for i in range(numCourses):
if indegree[i] == 0:
q.append(i)
n = 0
while q:
b = q.popleft()
n += 1
for a in edges[b]:
indegree[a] -= 1
if indegree[a] == 0:
q.append(a)
return n == numCourses
class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
List<Integer>[] edges = new List[numCourses];
for (int i = 0; i < numCourses; ++i) {
edges[i] = new ArrayList<>();
}
int[] indegree = new int[numCourses];
for (int[] p : prerequisites) {
int a = p[0], b = p[1];
edges[b].add(a);
++indegree[a];
}
Queue<Integer> q = new LinkedList<>();
for (int i = 0; i < numCourses; ++i) {
if (indegree[i] == 0) {
q.offer(i);
}
}
int n = 0;
while (!q.isEmpty()) {
int b = q.poll();
++n;
for (int a : edges[b]) {
if (--indegree[a] == 0) {
q.offer(a);
}
}
}
return n == numCourses;
}
}
function canFinish(numCourses: number, prerequisites: number[][]): boolean {
let edges: number[][] = Array.from({ length: numCourses }, () => ([]));
let indeg = new Array(numCourses).fill(0);
for (let [b, a] of prerequisites) {
edges[a].push(b);
indeg[b] += 1;
}
let queue = [];
for (let i = 0; i < numCourses; i++) {
if (!indeg[i]) {
queue.push(i);
}
}
let visited: number = 0;
while (queue.length) {
visited += 1;
const u = queue.shift();
for (let v of edges[u]) {
indeg[v] -= 1;
if (!indeg[v]) {
queue.push(v);
}
}
}
return visited == numCourses;
};
class Solution {
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
vector<vector<int>> edges(numCourses);
vector<int> indegree(numCourses);
for (auto& p : prerequisites)
{
int a = p[0], b = p[1];
edges[b].push_back(a);
++indegree[a];
}
queue<int> q;
for (int i = 0; i < numCourses; ++i)
if (indegree[i] == 0)
q.push(i);
int n = 0;
while (!q.empty())
{
int b = q.front();
q.pop();
++n;
for (int a : edges[b])
if (--indegree[a] == 0)
q.push(a);
}
return n == numCourses;
}
};
func canFinish(numCourses int, prerequisites [][]int) bool {
edges := make([][]int, numCourses)
indegree := make([]int, numCourses)
for _, p := range prerequisites {
a, b := p[0], p[1]
edges[b] = append(edges[b], a)
indegree[a]++
}
var q []int
for i := 0; i < numCourses; i++ {
if indegree[i] == 0 {
q = append(q, i)
}
}
n := 0
for len(q) > 0 {
b := q[0]
q = q[1:]
n++
for _, a := range edges[b] {
indegree[a]--
if indegree[a] == 0 {
q = append(q, a)
}
}
}
return n == numCourses
}
public class Solution {
public bool CanFinish(int numCourses, int[][] prerequisites) {
var edges = new List<int>[numCourses];
for (int i = 0; i < numCourses; ++i)
{
edges[i] = new List<int>();
}
var indegree = new int[numCourses];
for (int i = 0; i < prerequisites.Length; ++i)
{
int a = prerequisites[i][0];
int b = prerequisites[i][1];
edges[b].Add(a);
++indegree[a];
}
var q = new Queue<int>();
for (int i = 0; i < numCourses; ++i)
{
if (indegree[i] == 0) q.Enqueue(i);
}
var n = 0;
while (q.Count > 0)
{
int b = q.Dequeue();
++n;
foreach (int a in edges[b])
{
if (--indegree[a] == 0) q.Enqueue(a);
}
}
return n == numCourses;
}
}