Skip to content

Commit d06bfb6

Browse files
committed
add cache
1 parent a241327 commit d06bfb6

File tree

3 files changed

+62
-32
lines changed

3 files changed

+62
-32
lines changed

Advanced.Algorithms/DynamicProgramming/Count/CountBinaryTree.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace Advanced.Algorithms.DynamicProgramming.Count
1+
using System.Collections.Generic;
2+
3+
namespace Advanced.Algorithms.DynamicProgramming.Count
24
{
35
/// <summary>
46
/// Problem statement below
@@ -8,7 +10,7 @@ public class CountBinaryTree
810
{
911
public static int Count(int n)
1012
{
11-
return Count(0, n);
13+
return Count(0, n, new Dictionary<string, int>());
1214
}
1315

1416
/// <summary>
@@ -21,7 +23,7 @@ public static int Count(int n)
2123
/// <param name="start"></param>
2224
/// <param name="end"></param>
2325
/// <returns></returns>
24-
private static int Count(int start, int end)
26+
private static int Count(int start, int end, Dictionary<string, int> cache)
2527
{
2628
if (start > end)
2729
{
@@ -34,15 +36,25 @@ private static int Count(int start, int end)
3436
return 1;
3537
}
3638

39+
var cacheKey = $"{start}-{end}";
40+
41+
if(cache.ContainsKey(cacheKey))
42+
{
43+
return cache[cacheKey];
44+
}
45+
46+
3747
var count = 0;
3848

3949
//break in to left & right
4050
for (int i = start; i < end; i++)
4151
{
42-
count += Count(start, i)
43-
* Count(i + 1, end);
52+
count += Count(start, i, cache)
53+
* Count(i + 1, end, cache);
4454
}
4555

56+
cache.Add(cacheKey, count);
57+
4658
return count;
4759
}
4860
}

Advanced.Algorithms/DynamicProgramming/Matrix/MinCostMatrixPath.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ public class MinCostMatrixPath
1414
{
1515
public static int FindPath(int[,] matrix)
1616
{
17-
return FindPath(matrix, 0, 0);
17+
return FindPath(matrix, 0, 0, new Dictionary<string, int>());
1818
}
1919

20-
private static int FindPath(int[,] matrix, int i, int j)
20+
private static int FindPath(int[,] matrix, int i, int j, Dictionary<string, int> cache)
2121
{
2222
if (i >= matrix.GetLength(0)
2323
|| j >= matrix.GetLength(1))
@@ -31,13 +31,24 @@ private static int FindPath(int[,] matrix, int i, int j)
3131
return matrix[i, j];
3232
}
3333

34+
var cacheKey = $"{i}-{j}";
35+
36+
if(cache.ContainsKey(cacheKey))
37+
{
38+
return cache[cacheKey];
39+
}
40+
3441
var pathResults = new List<int>();
3542

36-
pathResults.Add(FindPath(matrix, i + 1, j + 1));
37-
pathResults.Add(FindPath(matrix, i, j + 1));
38-
pathResults.Add(FindPath(matrix, i + 1, j));
43+
pathResults.Add(FindPath(matrix, i + 1, j + 1, cache));
44+
pathResults.Add(FindPath(matrix, i, j + 1, cache));
45+
pathResults.Add(FindPath(matrix, i + 1, j, cache));
46+
47+
var result = pathResults.Min() + matrix[i, j];
48+
49+
cache.Add(cacheKey, result);
3950

40-
return pathResults.Min() + matrix[i, j];
51+
return result;
4152
}
4253
}
4354
}

Advanced.Algorithms/DynamicProgramming/TowerOfHanoi.cs

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Diagnostics;
1+
using System.Collections.Generic;
2+
using System.Diagnostics;
23

34
namespace Advanced.Algorithms.DynamicProgramming
45
{
@@ -10,11 +11,7 @@ public class TowerOfHanoi
1011
{
1112
public static int Tower(int numOfDisks)
1213
{
13-
var minMoves = 0;
14-
15-
Tower(numOfDisks, "a", "b", "c", ref minMoves);
16-
17-
return minMoves;
14+
return Tower(numOfDisks, "a", "b", "c", new Dictionary<int, int>());
1815
}
1916

2017
/// <summary>
@@ -25,29 +22,39 @@ public static int Tower(int numOfDisks)
2522
/// <param name="aux"></param>
2623
/// <param name="dest"></param>
2724
/// <param name="moveCount"></param>
28-
private static void Tower(int n, string source, string aux, string dest, ref int moveCount)
25+
private static int Tower(int n, string source, string aux, string dest, Dictionary<int, int> cache)
2926
{
3027
Debug.WriteLine("Moving disc {0} from pole {1} to {2}", n, source, dest);
3128

32-
moveCount++;
29+
if (n == 1)
30+
{
31+
return 1;
32+
}
3333

34-
if (n > 1)
34+
if (cache.ContainsKey(n))
3535
{
36-
////assume without last disc on top we would be moving a disc from source to dest
37-
//Tower(n - 1, source, aux, dest, ref moveCount);
36+
return cache[n];
37+
}
3838

39-
////The last disc we just moved above to destination was in source
40-
////that disc was definitely moved from aux to source (if it was in destination we would be not here)
41-
//Tower(n - 1, aux, dest, source, ref moveCount);
39+
////assume without last disc on top we would be moving a disc from source to dest
40+
//Tower(n - 1, source, aux, dest, ref moveCount);
4241

43-
//or alternatively
44-
//assume without last disc on top we would be moving a disc from aux to dest
45-
Tower(n - 1, aux, source, dest, ref moveCount);
42+
////The last disc we just moved above to destination was in source
43+
////that disc was definitely moved from aux to source (if it was in destination we would be not here)
44+
//Tower(n - 1, aux, dest, source, ref moveCount);
45+
46+
//or alternatively
47+
//assume without last disc on top we would be moving a disc from aux to dest
48+
var result = Tower(n - 1, aux, source, dest, cache)
49+
50+
//The last disc we just moved above to destination was in aux
51+
//that disc was definitely moved from source to aux (if it was in destination we would be done by now)
52+
+ Tower(n - 1, source, dest, aux, cache) + 1;
53+
54+
cache.Add(n, result);
55+
56+
return result;
4657

47-
//The last disc we just moved above to destination was in aux
48-
//that disc was definitely moved from source to aux (if it was in destination we would be done by now)
49-
Tower(n - 1, source, dest, aux, ref moveCount);
50-
}
5158
}
5259

5360
}

0 commit comments

Comments
 (0)