Skip to content

Commit 4c1ad6b

Browse files
add solution to course schedule problem (#84)
1 parent 7ab026c commit 4c1ad6b

File tree

2 files changed

+66
-10
lines changed

2 files changed

+66
-10
lines changed

C++/Course Schedule.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//Problem Statement: Course Schedule
2+
3+
// There are a total of numCourses courses you have to take, labeled from 0 to numCourses-1.
4+
5+
// 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]
6+
7+
// Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?
8+
9+
// Solution:
10+
// approach used to solve the problem: detect cycle in directed graph
11+
// if a back edge exists in the graph, it implies that there is a cycle, thus it is not possible to finish all the courses
12+
13+
class Solution {
14+
public:
15+
16+
// helper function
17+
bool dfs(vector<vector<int>>& adjlist, vector<int>& visited, int i) {
18+
19+
// base condition
20+
if(visited[i]==1) return false;
21+
visited[i]=1; // mark as being visited
22+
23+
for(int a:adjlist[i]) {
24+
if(!dfs(adjlist, visited, a)) // dfs(adjlist, visited, a) == false
25+
return false;
26+
}
27+
28+
visited[i] = 2; // mark as visited
29+
return true;
30+
}
31+
32+
33+
bool canFinish(int numc, vector<vector<int>>& prereq) {
34+
35+
// numc: numCourses
36+
// prereq: prerequisites
37+
38+
// create adjacency list
39+
vector<vector<int>> adjlist(numc, vector<int>());
40+
for(vector<int>& p:prereq)
41+
adjlist[p[0]].push_back(p[1]);
42+
43+
vector<int> visited(numc, 0);
44+
// unvisited: 0
45+
// being visited: 1
46+
// completely visited: 2
47+
for(int i=0; i<numc; i++) {
48+
if(visited[i]==0 && !dfs(adjlist, visited, i))
49+
return false;
50+
}
51+
52+
return true;
53+
}
54+
55+
};

README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
6868

6969
# Bit Manipulation
7070

71-
| # | Title | Solution | Time | Space | Difficulty | Tag | Tutorial |
72-
| ---- | --------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- | ------ | ------ | ---------- | --- | --------- |
73-
| 0136 | [Single Number](https://leetcode.com/problems/single-number/) | [Java](./Java/single-number.java) <br> [Python](./Python/single-number.py) <br> [C++](./C++/Single-Number.cpp) | _O(n)_ | _O(1)_ | Easy | | Using XOR |
74-
| 260 | [Single Number III](https://leetcode.com/problems/single-number-iii/) | [Python](./Python/single-number-iii.py) | _O(n)_ | _O(1)_ | Medium | | |
75-
| 476 | [Number Complement](https://leetcode.com/problems/number-complement/) | [Java](./Java/number-complement.java) | _O(1)_ | _O(1)_ | Easy | | [Tutorial](https://youtu.be/6bp5V-O3zts) |
76-
| 520 | [Detect Capital Use](https://leetcode.com/problems/detect-capital/) | [Python](./Python/detect-capital.py) | _O(n)_ | _O(1)_ | Easy | | |
71+
| # | Title | Solution | Time | Space | Difficulty | Tag | Tutorial |
72+
| ---- | --------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- | ------ | ------ | ---------- | --- | ---------------------------------------- |
73+
| 0136 | [Single Number](https://leetcode.com/problems/single-number/) | [Java](./Java/single-number.java) <br> [Python](./Python/single-number.py) <br> [C++](./C++/Single-Number.cpp) | _O(n)_ | _O(1)_ | Easy | | Using XOR |
74+
| 260 | [Single Number III](https://leetcode.com/problems/single-number-iii/) | [Python](./Python/single-number-iii.py) | _O(n)_ | _O(1)_ | Medium | | |
75+
| 476 | [Number Complement](https://leetcode.com/problems/number-complement/) | [Java](./Java/number-complement.java) | _O(1)_ | _O(1)_ | Easy | | [Tutorial](https://youtu.be/6bp5V-O3zts) |
76+
| 520 | [Detect Capital Use](https://leetcode.com/problems/detect-capital/) | [Python](./Python/detect-capital.py) | _O(n)_ | _O(1)_ | Easy | | |
7777

7878
<br/>
7979
<div align="right">
@@ -254,7 +254,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
254254
| ---- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ------------------------- | ----------------- | ---------- | --- | ---- |
255255
| 1284 | [Minimum Number of Flips to Convert Binary Matrix to Zero Matrix](https://leetcode.com/problems/minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix/) | [C++](./C++/Minimum-Number-of-Flips-to-Convert-Binary-Matrix-to-Zero-Matrix.cpp) | _O(m * n * 2 ^ (m \* n))_ | _O(2 ^ (m \* n))_ | Hard | BFS | |
256256
| 200 | [Number of Islands](https://leetcode.com/problems/number-of-islands/) | [Java](./Java/NumberOfIslands.java) | O(R \* C) | O(R \* C) | Medium | BFS |
257-
| 200 | [Word Ladder](https://leetcode.com/problems/word-ladder/) | [Java](./Java/word-ladder.java) | O(N^2 \* M) | O(N \* M) | Medium | BFS |
257+
| 200 | [Word Ladder](https://leetcode.com/problems/word-ladder/) | [Java](./Java/word-ladder.java) | O(N^2 \* M) | O(N \* M) | Medium | BFS |
258258

259259
<br/>
260260
<div align="right">
@@ -327,9 +327,10 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
327327

328328
# Graph
329329

330-
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
331-
| ---- | --------------------------------------------------------------------------------------------------- | ------------------------------------------------------- | -------- | --------- | ---------- | ----- | -------------- |
332-
| 1042 | [Flower Planting with No Adjacent](https://leetcode.com/problems/flower-planting-with-no-adjacent/) | [Python](./Python/1042_FlowerPlantingwithNoAdjacent.py) | _O(V+E)_ | _O(2V+E)_ | Medium | Graph | Graph Coloring |
330+
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
331+
| ---- | --------------------------------------------------------------------------------------------------- | ------------------------------------------------------- | -------- | --------- | ---------- | ----- | --------------------------------- |
332+
| 207 | [Course Schedule](https://leetcode.com/problems/course-schedule/) | [C++](./C++/Course Schedule.cpp) | _O(V+E)_ | _O(V+E)_ | Medium | Graph | Cycle Detection in Directed Graph |
333+
| 1042 | [Flower Planting with No Adjacent](https://leetcode.com/problems/flower-planting-with-no-adjacent/) | [Python](./Python/1042_FlowerPlantingwithNoAdjacent.py) | _O(V+E)_ | _O(2V+E)_ | Medium | Graph | Graph Coloring |
333334

334335
<br/>
335336
<div align="right">

0 commit comments

Comments
 (0)