Skip to content

Commit acdc98e

Browse files
authored
Update parallel-courses-ii.cpp
1 parent 800d210 commit acdc98e

File tree

1 file changed

+63
-59
lines changed

1 file changed

+63
-59
lines changed

C++/parallel-courses-ii.cpp

Lines changed: 63 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,8 @@
1-
// Time: O(nlogn + e), e is the number of edges in graph
2-
// Space: O(n + e)
3-
4-
class Solution {
5-
public:
6-
int minNumberOfSemesters(int n, vector<vector<int>>& dependencies, int k) {
7-
unordered_map<int, vector<int>> graph;
8-
vector<int> degrees(n);
9-
for (const auto &d: dependencies) {
10-
graph[d[0] - 1].emplace_back(d[1] - 1);
11-
++degrees[d[1] - 1];
12-
}
13-
vector<int> depths(n, -1);
14-
for (int i = 0; i < n; ++i) {
15-
dfs(graph, i, &depths);
16-
}
17-
priority_queue<pair<int, int>> max_heap;
18-
for (int i = 0; i < n; ++i) {
19-
if (!degrees[i]) {
20-
max_heap.emplace(depths[i], i);
21-
}
22-
}
23-
int result = 0;
24-
while (!max_heap.empty()) {
25-
vector<int> new_q;
26-
for (int i = 0; !max_heap.empty() && i < k; ++i) {
27-
const auto [depth, node] = max_heap.top(); max_heap.pop();
28-
for (const auto& child : graph[node]) {
29-
if (!--degrees[child]) {
30-
new_q.emplace_back(child);
31-
}
32-
}
33-
}
34-
++result;
35-
for (const auto& node : new_q) {
36-
max_heap.emplace(depths[node], node);
37-
}
38-
}
39-
return result;
40-
}
41-
42-
private:
43-
int dfs(const unordered_map<int, vector<int>> &graph,
44-
int i, vector<int> *depths) {
45-
if ((*depths)[i] == -1) {
46-
int depth = 0;
47-
if (graph.count(i)) {
48-
for (const auto& child : graph.at(i)) {
49-
depth = max(depth, dfs(graph, child, depths));
50-
}
51-
}
52-
(*depths)[i] = depth + 1;
53-
}
54-
return (*depths)[i];
55-
}
56-
};
57-
581
// Time: O((n * C(c, min(c, k))) * 2^n)
592
// Space: O(2^n)
3+
604
// concise dp solution
61-
class Solution2 {
5+
class Solution {
626
public:
637
int minNumberOfSemesters(int n, vector<vector<int>>& dependencies, int k) {
648
vector<int> reqs(n);
@@ -117,7 +61,7 @@ class Solution2 {
11761
// Time: O((n * C(c, min(c, k))) * 2^n)
11862
// Space: O(2^n)
11963
// embedded combination dp solution
120-
class Solution3 {
64+
class Solution2 {
12165
public:
12266
int minNumberOfSemesters(int n, vector<vector<int>>& dependencies, int k) {
12367
static const auto& choice_mask =
@@ -168,3 +112,63 @@ class Solution3 {
168112
return dp.back();
169113
}
170114
};
115+
116+
// Time: O(nlogn + e), e is the number of edges in graph
117+
// Space: O(n + e)
118+
// wrong greedy solution, ex
119+
// 9
120+
// [[1,4],[1,5],[3,5],[3,6],[2,6],[2,7],[8,4],[8,5],[9,6],[9,7]]
121+
// 3
122+
class Solution_WA {
123+
public:
124+
int minNumberOfSemesters(int n, vector<vector<int>>& dependencies, int k) {
125+
unordered_map<int, vector<int>> graph;
126+
vector<int> degrees(n);
127+
for (const auto &d: dependencies) {
128+
graph[d[0] - 1].emplace_back(d[1] - 1);
129+
++degrees[d[1] - 1];
130+
}
131+
vector<int> depths(n, -1);
132+
for (int i = 0; i < n; ++i) {
133+
dfs(graph, i, &depths);
134+
}
135+
priority_queue<pair<int, int>> max_heap;
136+
for (int i = 0; i < n; ++i) {
137+
if (!degrees[i]) {
138+
max_heap.emplace(depths[i], i);
139+
}
140+
}
141+
int result = 0;
142+
while (!max_heap.empty()) {
143+
vector<int> new_q;
144+
for (int i = 0; !max_heap.empty() && i < k; ++i) {
145+
const auto [depth, node] = max_heap.top(); max_heap.pop();
146+
for (const auto& child : graph[node]) {
147+
if (!--degrees[child]) {
148+
new_q.emplace_back(child);
149+
}
150+
}
151+
}
152+
++result;
153+
for (const auto& node : new_q) {
154+
max_heap.emplace(depths[node], node);
155+
}
156+
}
157+
return result;
158+
}
159+
160+
private:
161+
int dfs(const unordered_map<int, vector<int>> &graph,
162+
int i, vector<int> *depths) {
163+
if ((*depths)[i] == -1) {
164+
int depth = 0;
165+
if (graph.count(i)) {
166+
for (const auto& child : graph.at(i)) {
167+
depth = max(depth, dfs(graph, child, depths));
168+
}
169+
}
170+
(*depths)[i] = depth + 1;
171+
}
172+
return (*depths)[i];
173+
}
174+
};

0 commit comments

Comments
 (0)