So, I built this program to create different stairwells. Essentially the problem is this: given the integer N, how many different ways you can build a staircase. N is guaranteed to be more than 3 and less than 200. Any previous step cannot be more than the next step, otherwise it will defeat the goal of the ladder.
Thus, with N = 3, you can build one staircase: 2 steps, and then 1 step following
Given N = 4, you can build one staircase: 3 steps, and then 1 step following
With N = 5, you can build two stairs: 3 steps, and then 2 steps OR 4 steps, and then 1 step.
My function is lower and it works, except that its execution time is too slow. So I was thinking of trying to make a memorandum of function, but to be honest, I donโt quite understand how to implement this. If I could get some help on how to do this, that would be great.
public static void main(String [] args)
{
System.out.println(answer(200));
}
public static int answer(int n) {
return bricks(1,n) -1;
}
public static int bricks(int height, int bricksLeft)
{
if(bricksLeft == 0)
{
return 1;
}
else if(bricksLeft < height)
{
return 0;
}
else
{
return bricks(height +1, bricksLeft - height) + bricks(height +1, bricksLeft);
}
}
source
share