Skip to content

Commit

Permalink
CourseSchedule (210) answer
Browse files Browse the repository at this point in the history
  • Loading branch information
NSC508 committed Dec 4, 2020
1 parent e9d30cc commit 1942eec
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
42 changes: 42 additions & 0 deletions CourseSchedules2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <vector>
#include <queue>
#include <unordered_set>
using namespace std;


vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
//loop through the pre reqs,
vector<unordered_set<int>> parents(numCourses);
vector<unordered_set<int>> children(numCourses);
queue<int> orderToLoop;
vector<int> answer;
for (auto &i : prerequisites) {
//vector [child, parent]
//add all the parents to a parent vector,
parents[i[0]].insert(i[1]);
//add children to child vector
children[i[1]].insert(i[0]);
}
//loop through the parents
for (int i = 0; i < numCourses; i++) {
//add all that have 0 size into the queue
if (parents[i].empty()) {
orderToLoop.push(i);
}
}
while (!orderToLoop.empty()) {
for (auto &c: children[orderToLoop.front()]) {
//remove the front from the set at parent[c]
parents[c].erase(orderToLoop.front());
if (parents[c].empty()) {
orderToLoop.push(c);
}
}
answer.push_back(orderToLoop.front());
orderToLoop.pop();
}
if (answer.size() != numCourses) {
answer.clear();
}
return answer;
};
2 changes: 1 addition & 1 deletion SumOfEvenNumbers.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static int[] sumEvenAfterQueries(int[] A, int[][] queries) {
}
//Loop over queries
for (int i = 0; i < queries.length; i++) {
int internalSum = sum
int internalSum = sum;
//If adding even number to already even, add that much to the sum
if (queries[i][0] % 2 == 0 && evenIndices.contains(queries[i][1])) {
internalSum += queries[i][0];
Expand Down

0 comments on commit 1942eec

Please sign in to comment.