207. Course Schedule
- Total Accepted: 73890
- Total Submissions: 237873
- Difficulty: Medium
- Contributors: Admin
There are a total of n courses you have to take, labeled from 0
to n - 1
.
Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]
Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?
For example:
2, [[1,0]]
There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible.
2, [[1,0],[0,1]]
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.
Note:
- The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
- You may assume that there are no duplicate edges in the input prerequisites.
class Solution {
public:
bool canFinish(int numCourses, vector
>& prerequisites) {
int size = prerequisites.size();
vector
> matrix(numCourses);
vector
visited(numCourses, false); vector
tags(numCourses, false); for(int i = 0; i < size; i ++){ matrix[prerequisites[i].first].push_back(prerequisites[i].second); } if(has_cycle(numCourses, visited, matrix, tags)) return false; return true; } bool DFS(int n, vector
&visited, vector
> &matrix, vector
&tags){ tags[n] = true; visited[n] = true; for(int i = 0; i < matrix[n].size(); i ++){ if(visited[matrix[n][i]] == true || DFS(matrix[n][i], visited, matrix, tags)) return true; } visited[n] = false; return false; } bool has_cycle(int numCourses, vector
&visited, vector
> &matrix, vector
&tags){ for(int i = 0; i < numCourses; i ++){ if(tags[i] == false){ if(DFS(i, visited, matrix, tags)) return true; } } return false; } };
210. Course Schedule II
- Total Accepted: 55696
- Total Submissions: 209487
- Difficulty: Medium
- Contributor: LeetCode
There are a total of n courses you have to take, labeled from 0
to n - 1
.
Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]
Given the total number of courses and a list of prerequisite pairs, return the ordering of courses you should take to finish all courses.
There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.
For example:
2, [[1,0]]
There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is [0,1]
4, [[1,0],[2,0],[3,1],[3,2]]
There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is [0,1,2,3]
. Another correct ordering is[0,2,1,3]
.
Note:
- The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
- You may assume that there are no duplicate edges in the input prerequisites.
class Solution {
public:
vector
findOrder(int numCourses, vector
>& prerequisites) {
int size = prerequisites.size();
vector
order;
order.clear();
vector
> matrix(numCourses); vector
visited(numCourses, false); vector
tags(numCourses, false); vector
get(numCourses, false); for(int i = 0; i < size; i ++){ matrix[prerequisites[i].first].push_back(prerequisites[i].second); } if(has_order(numCourses, visited, matrix, tags, get, order)) return order; return order; } bool DFS(int n, vector
&visited, vector
> &matrix, vector
&tags, vector
&get, vector
&order){ tags[n] = true; visited[n] = true; for(int i = 0; i < matrix[n].size(); i ++){ if(visited[matrix[n][i]] == true || DFS(matrix[n][i], visited, matrix, tags, get, order)){ order.clear(); get.clear(); return true; } } visited[n] = false; if(get[n] == false) order.push_back(n); get[n] = true; return false; } bool has_order(int numCourses, vector
&visited, vector
> &matrix, vector
&tags, vector
&get, vector
&order){ for(int i = 0; i < numCourses; i ++){ if(tags[i] == false){ if(DFS(i, visited, matrix, tags, get, order)){ order.clear(); get.clear(); return false; } } } return true; } };