-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from Modernbeast02/main
Course Schedule in C++
- Loading branch information
Showing
4 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
class Solution { | ||
public: | ||
vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) { | ||
// just print the topo sort if all indegree at the end are zero | ||
|
||
vector<int>indegree(numCourses, 0); | ||
vector<int>adj[numCourses]; | ||
vector<int>ans; | ||
for(auto it : prerequisites){ | ||
adj[it[1]].push_back(it[0]); // created graph | ||
indegree[it[0]]++; // created indegree array | ||
} | ||
queue<int>q; | ||
for(int i = 0; i < numCourses; i++){ | ||
if(indegree[i] == 0){ | ||
q.push(i); | ||
} | ||
} | ||
while(!q.empty()){ | ||
int top = q.front(); | ||
ans.push_back(top); | ||
q.pop(); | ||
for(auto it : adj[top]){ | ||
indegree[it]--; | ||
if(indegree[it] == 0){ | ||
q.push(it); | ||
} | ||
} | ||
} | ||
for(int i = 0; i < numCourses; i++){ | ||
if(indegree[i] != 0){ | ||
vector<int> dummy; | ||
return dummy; | ||
} | ||
} | ||
return ans; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<h2><a href="https://leetcode.com/problems/course-schedule-ii/">210. Course Schedule II</a></h2><h3>Medium</h3><hr><div><p>There are a total of <code>numCourses</code> courses you have to take, labeled from <code>0</code> to <code>numCourses - 1</code>. You are given an array <code>prerequisites</code> where <code>prerequisites[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that you <strong>must</strong> take course <code>b<sub>i</sub></code> first if you want to take course <code>a<sub>i</sub></code>.</p> | ||
|
||
<ul> | ||
<li>For example, the pair <code>[0, 1]</code>, indicates that to take course <code>0</code> you have to first take course <code>1</code>.</li> | ||
</ul> | ||
|
||
<p>Return <em>the ordering of courses you should take to finish all courses</em>. If there are many valid answers, return <strong>any</strong> of them. If it is impossible to finish all courses, return <strong>an empty array</strong>.</p> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
|
||
<pre><strong>Input:</strong> numCourses = 2, prerequisites = [[1,0]] | ||
<strong>Output:</strong> [0,1] | ||
<strong>Explanation:</strong> 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]. | ||
</pre> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<pre><strong>Input:</strong> numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]] | ||
<strong>Output:</strong> [0,2,1,3] | ||
<strong>Explanation:</strong> 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]. | ||
</pre> | ||
|
||
<p><strong class="example">Example 3:</strong></p> | ||
|
||
<pre><strong>Input:</strong> numCourses = 1, prerequisites = [] | ||
<strong>Output:</strong> [0] | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= numCourses <= 2000</code></li> | ||
<li><code>0 <= prerequisites.length <= numCourses * (numCourses - 1)</code></li> | ||
<li><code>prerequisites[i].length == 2</code></li> | ||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < numCourses</code></li> | ||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li> | ||
<li>All the pairs <code>[a<sub>i</sub>, b<sub>i</sub>]</code> are <strong>distinct</strong>.</li> | ||
</ul> | ||
</div> |