2

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.

pgiitu
  • 1,671
  • 1
  • 15
  • 23
cresta
  • 31
  • 3
  • This question has appeared several times on Stack Overflow this week, such as most recently here: http://stackoverflow.com/questions/33292472/algorithm-for-a-stair-climbing-permutation – Tim Biegeleisen Oct 23 '15 at 03:55
  • And here: http://stackoverflow.com/questions/33250876/possible-paths-to-the-top-of-a-staircase/33251224#33251224 – Tim Biegeleisen Oct 23 '15 at 03:55
  • @TimBiegeleisen Yes but at least this person has made some effort, posted some code and some indication of where he is stuck, like it should be on SO. So it has my vote. Your first link is ... well I can't fathom what must have gotten into the high-ranking user who answered that question, or who upvoted the question. – Erwin Bolwidt Oct 23 '15 at 04:02

3 Answers3

0

I don't understanding why you need to set the step size to be 1. It should look something like :

climb(String steps, int remainingSteps) {
    if (remaiingSteps == 0) {
        print steps
    } else if (remainingSteps == 1) {
        print steps + "1"
    } else {
        // either climb one step this time
        climb(steps + "1 ", remainingSteps -1);

        // or client 2 steps this time
        climb(steps + "2 ", remainingSteps -2 );
    }
}

If you understand the above recursion, getting all unique path should not be anything difficult. A more intuitive recursion is (for real life, you may want to pass the result List as parameter instead of returning). Pseudo-code :

List<String> climb(String steps, int remainingSteps) {
    if (remaingSteps == 0) {
        return [steps]
    } else if (remainingSteps == 1) {
        return [steps + "1 "]
    } else {
        // combine result lists of two paths
        return climb(steps + "1 ", remainingSteps -1)
             + climb(steps + "2 ", remainingSteps -2 );
    }
}
Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
  • you are right. I do not have to set the step size to be 1. I just did that because I didn't know what else to do there. Ok, but I don't understand how I am supposed to get all the unique paths? I think step size is a key here.. – cresta Oct 23 '15 at 04:06
  • added some extra for getting the paths. Shouldn't be difficult though if you actually understand the original recursion – Adrian Shum Oct 23 '15 at 04:15
  • hmm, I guess I'm not understanding your logic here. I think what you have here is quite similar to what I have in the problem? just adding numbers to a string. – cresta Oct 23 '15 at 04:24
  • Also, looking at what I have right now, my method can return only one combination at a time right? should I change it so that it can print all the combinations all at once? – cresta Oct 23 '15 at 04:26
  • Read until you understand. Things we are returning is already totally different. How are you suppose to return a bunch of paths by one single String? – Adrian Shum Oct 23 '15 at 04:26
  • Oh yes, I get that part, but we haven't learned List yet. so we can't do that. we have to be able to print all the combinations just using strings. – cresta Oct 23 '15 at 04:28
  • forget about the implementation detail, for return type, ask yourself, what do you want to return if you want to have a function to return all possible paths? – Adrian Shum Oct 23 '15 at 04:28
  • If you just want to *print*, then that's what I was doing in beginning – Adrian Shum Oct 23 '15 at 04:28
  • I guess I should have System.out.println right under if (remaingSteps == 0) so that it keeps printing a unique path – cresta Oct 23 '15 at 04:29
  • @cresta to be honest, I have no idea on what you were asking. – Adrian Shum Oct 23 '15 at 06:20
0

If there are no steps left, then we simply want to print our path. If there is only one step left, then we cannot take two steps, so we take one step and add a one to our path. Otherwise, we take one step, adding a one to our path. Once that is finished and we are back where we were, we do the same thing with two steps.

void climb(int steps, String path) {
    if (steps == 0) {
        System.out.println(path);
    } else if (steps == 1) {
        climb(steps-1, path + "1 ");
    } else {
        climb(steps-1, path + "1 ");
        climb(steps-2, path + "2 ");
    }
}
Robin
  • 1
0

Its fairly simple

public static void main(String[] args) {
        printPath(4,"");
    }

private static void printPath(int n,String path) {
    if (n == 0) {
        System.out.println(path.substring(1));
    }
    if (n-1 >=0) {
        printPath(n-1,path + ",1");
    }

    if (n-2 >=0) {
        printPath(n-2,path + ",2");
    }
}
hard coder
  • 5,449
  • 6
  • 36
  • 61