I'm stuck on this problem for a long time.
Problem
You are going up a step ladder that has x steps. Find all the combinations to go up this step ladder if you can only take 1 or 2 steps at a time.
This problem has few restrictions. No loop, no array, must use recursion.
Here is what I did so far.
public static String climb(int remainingSteps, String combo) {
// terminator
if (remainingSteps == 0) {
combo = combo + "";
}
// recursion
else {
if (remainingSteps == 1) {
combo = combo + "1, ";
} else { // remainingSteps is greater than or equal to 2
int step = 1; // this part isn't right
switch (step) {
case 1:
combo = waysToClimbHelper(remainingSteps - step, combo) + "1, ";
break;
case 2:
combo = waysToClimbHelper(remainingSteps - step, combo) + "2, ";
break;
}
}
}
return combo;
}
I need it to show all the unique ways to climb the ladder, so say the ladder has 3 steps, it should show
1, 1, 1
2, 1
1,2
However, as you can see from my code, I set the step size to be 1, so it's always taking 1 step at a time. I can't think of any way to cleverly change the step size so that the program can show all the unique paths.