-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathStaircaseSteps.cs
66 lines (56 loc) · 2.21 KB
/
StaircaseSteps.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Collections.Generic;
using Path = System.Collections.Generic.List<int>;
namespace PracticeQuestionsSharp.Exercises.Dynamic_Programming
{
//A child is running up a staircase with n steps and can hop either 1, 2 or 3 steps at a time.
// Count how many possible ways the child could run up the steps.
//Recursive - No memoization
public static class StaircaseSteps
{
public static int StepPermutations(int n)
{
List<Path> paths = new List<Path>();
ClimbSteps(n, 1, 0, new Path(), paths);
ClimbSteps(n, 2, 0, new Path(), paths);
ClimbSteps(n, 3, 0, new Path(), paths);
return paths.Count;
}
private static void ClimbSteps(int n, int stepSize, int currStepsTaken, Path currPath, List<Path> paths)
{
currStepsTaken += stepSize;
currPath.Add(stepSize);
if (currStepsTaken > n) return;
if (currStepsTaken == n)
{
paths.Add(currPath);
return;
}
if (currStepsTaken < n)
{
ClimbSteps(n, 1, currStepsTaken, new Path(currPath), paths);
ClimbSteps(n, 2, currStepsTaken, new Path(currPath), paths);
ClimbSteps(n, 3, currStepsTaken, new Path(currPath), paths);
}
}
//Recursive memoized
public static int StepPermutationsMemoized(int n)
{
int[] memoized = new int[n];
Array.Fill(memoized, -1);
return ClimbStepsMemoized(n, 1, memoized) +
ClimbStepsMemoized(n, 2, memoized) +
ClimbStepsMemoized(n, 3, memoized);
}
private static int ClimbStepsMemoized(int n, int stepSize, int[] memoized)
{
n -= stepSize;
if (n < 0) return 0; //Path is not valid
if (n == 0) return 1; //Path is valid
if (memoized[n] != -1) return memoized[n];
return memoized[n] = ClimbStepsMemoized(n, 1, memoized) +
ClimbStepsMemoized(n, 2, memoized) +
ClimbStepsMemoized(n, 3, memoized);
}
}
}