# Course Schedule LeetCode Solution

Here, We see Course Schedule LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

# List of all LeetCode Solution

## 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`.

• For example, the pair `[0, 1]`, indicates that to take course `0` you have to first take course `1`.

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.

## Course Schedule LeetCode SolutionC++

``````class Solution {
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
vector<int> indegree(numCourses, 0);
vector<int> ans;
for(auto x: prerequisites){
indegree[x[1]]++;
}
queue<int> q;
for(int i = 0; i < numCourses; i++){
if(indegree[i] == 0){
q.push(i);
}
}
while(!q.empty()){
auto t = q.front();
ans.push_back(t);
q.pop();
indegree[x]--;
if(indegree[x] == 0){
q.push(x);
}
}
}
return ans.size() == numCourses;
}
};```Code language: PHP (php)```

## Course Schedule LeetCode SolutionJava

``````public class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
ArrayList[] graph = new ArrayList[numCourses];
int[] degree = new int[numCourses];
int count=0;
for(int i=0;i<numCourses;i++)
graph[i] = new ArrayList();
for(int i=0; i<prerequisites.length;i++){
degree[prerequisites[i][1]]++;
}
for(int i=0; i<degree.length;i++){
if(degree[i] == 0){
count++;
}
}
while(queue.size() != 0){
int course = (int)queue.poll();
for(int i=0; i<graph[course].size();i++){
int pointer = (int)graph[course].get(i);
degree[pointer]--;
if(degree[pointer] == 0){
count++;
}
}
}
if(count == numCourses)
return true;
else
return false;
}
}```Code language: PHP (php)```

## Course Schedule SolutionJavaScript

``````var canFinish = function(numCourses, prerequisites) {
const order = [];
const queue = [];
const graph = new Map();
const indegree = Array(numCourses).fill(0);
for (const [e, v] of prerequisites) {
if (graph.has(v)) {
graph.get(v).push(e);
} else {
graph.set(v, [e]);
}
indegree[e]++;
}
for (let i = 0; i < indegree.length; i++) {
if (indegree[i] === 0) queue.push(i);
}
while (queue.length) {
const v = queue.shift();
if (graph.has(v)) {
for (const e of graph.get(v)) {
indegree[e]--;
if (indegree[e] === 0) queue.push(e);
}
}
order.push(v);
}
return numCourses === order.length;
};```Code language: JavaScript (javascript)```

## Course Schedule SolutionPython

``````class Solution(object):
def canFinish(self, numCourses, prerequisites):
adj = [[] for _ in range(numCourses)]
indegree = [0] * numCourses
ans = []
for pair in prerequisites:
course = pair[0]
prerequisite = pair[1]
indegree[course] += 1
queue = deque()
for i in range(numCourses):
if indegree[i] == 0:
queue.append(i)
while queue:
current = queue.popleft()
ans.append(current)