-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathall-paths-from-source-to-target.h
47 lines (37 loc) · 1.11 KB
/
all-paths-from-source-to-target.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#ifndef ALL_PATHS_FROM_SOURCE_TO_TARGET_H_
#define ALL_PATHS_FROM_SOURCE_TO_TARGET_H_
#include <queue>
#include <vector>
namespace solution {
std::vector<std::vector<int>> allPathsSourceTarget(
std::vector<std::vector<int>>& graph) {
// got 2 rejection because i did not read through the question
// Runtime: 16 ms, faster than 63.00% of C++ online submissions for All Paths From Source to Target.
// Memory Usage: 17.7 MB, less than 17.97% of C++ online submissions for All Paths From Source to Target.
//
std::vector<std::vector<int>> result;
std::queue<std::vector<int>> temp;
for (auto& n : graph[0]) {
temp.push({0, n});
}
while (!temp.empty()) {
auto& v = temp.front();
if (graph[v.back()].empty()) {
result.push_back(v);
} else {
for (auto& next : graph[v.back()]) {
std::vector<int> n = v;
n.push_back(next);
if (graph[next].empty()) {
result.push_back(v);
} else {
temp.push(n);
}
}
}
temp.pop();
}
return result;
}
} // namespace solution
#endif // ALL_PATHS_FROM_SOURCE_TO_TARGET_H_