-
Notifications
You must be signed in to change notification settings - Fork 112
/
0797-AllPathsFromSourceToTarget.cs
47 lines (41 loc) · 1.51 KB
/
0797-AllPathsFromSourceToTarget.cs
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
//-----------------------------------------------------------------------------
// Runtime: 268ms
// Memory Usage: 36.2 MB
// Link: https://leetcode.com/submissions/detail/360484268/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _0797_AllPathsFromSourceToTarget
{
public IList<IList<int>> AllPathsSourceTarget(int[][] graph)
{
var cache = new Dictionary<int, IList<IList<int>>>();
return AllPathsSourceTarget(graph, 0, cache);
}
private IList<IList<int>> AllPathsSourceTarget(int[][] graph, int node, Dictionary<int, IList<IList<int>>> cache)
{
if (cache.ContainsKey(node))
return cache[node];
var results = new List<IList<int>>();
foreach (var next in graph[node])
{
if (next == graph.Length - 1)
results.Add(new List<int>() { node, next });
var pathes = AllPathsSourceTarget(graph, next, cache);
if (pathes.Count > 0)
{
foreach (var path in pathes)
{
var newPath = new List<int>();
newPath.Add(node);
newPath.AddRange(path);
results.Add(newPath);
}
}
}
cache[node] = results;
return results;
}
}
}