2

I am confused how to write this in JAVA: There exists a staircase with N steps, and you can climb up either 1 or 2 steps at a time. Given N, write a function that returns the number of unique ways you can climb the staircase. The order of the steps matters.

For example, if N is 4, then there are 5 unique ways:

1, 1, 1, 1
2, 1, 1
1, 2, 1
1, 1, 2
2, 2

What if, instead of being able to climb 1 or 2 steps at a time, you could climb any number from a set of positive integers X? For example, if X = {1, 3, 5}, you could climb 1, 3, or 5 steps at a time.

Basically, I could do the first part and understand the logics with harder part with the set that the answer is: f(n) = f(n-1) + f(n-3) + f(n-5). Can anyone help me? This is mine approach:

public static void main(String[] args) {
 int n = 4;
 Set < Integer > list = new HashSet < Integer > ();
 list.add(1);
 list.add(3);
 list.add(5);
 int ways = reachNFloorSet(list, n, 0);
 //     int ways = reachNFloor(n);
 System.out.println(n + " Floor is reached in " + ways + " different way/s.");
}


public static int reachNFloor(int n) { // easy part
 if (n <= 1) {
  return 1;
 }
 return reachNFloor(n - 1) + reachNFloor(n - 2);
}

public static int reachNFloorSet(Set < Integer > numbers, int n, int sum) {
 if (n < 0) {
  return 0;
 } else if (n == 0) {
  return 1;
 }

 for (Integer x: numbers) {
  if (x <= n) {
   sum += reachNFloorSet(numbers, n - x, sum);
  }
 }
 return sum;
}

I think that the problem is with the for loop, but I cannot figure how to make it correct.

10
  • 1
    I up-voted it because I think its not about the stacktrace or debugging it. He just needs to rephrase his question. He has a problem that he needs help with because he is unsure of how to design the algorithm to so as such. Commented Aug 8, 2018 at 16:40
  • Recursion is not a reasonable approach to solving this problem. Your algorithm will be too slow and/or unnecessarily complicated. Commented Aug 8, 2018 at 16:43
  • Since reachNFloor works fine without a sum parameter, why did you make reachNFloorSet take a sum parameter? Get rid of it to fix you problem.
    – Andreas
    Commented Aug 8, 2018 at 16:47
  • @Mr00Anderson It's entirely about people doing their own diligence (research) before asking questions here. Debugging code is part of that, so the down-vote is appropriate, since tooltip of down-vote button says "This question does not show any research effort".
    – Andreas
    Commented Aug 8, 2018 at 16:50
  • @MattTimmermans - It seems to me that recursion is a completely reasonable approach. Of course you can get rid of recursive calls by using your own stack to record decisions, but that would hardly result in less complicated code. What else would you propose?
    – Ted Hopp
    Commented Aug 8, 2018 at 16:51

1 Answer 1

2

When n is negative or 0 in reachNFloorSet(), you're returning 0 or 1, but you should be returning sum or sum + 1. Otherwise you're throwing away all the accumulated info.

It would be better, I think, to rewrite your method so it doesn't have to worry about how many steps have already been taken:

public static int reachNFloorSet (Set<Integer> numbers, int n) {
    if (n == 0) {
        return 1;
    }

    int sum = 0;
    for(Integer x: numbers) {

        if (x <= n) {

            sum += reachNFloorSet(numbers, n-x);

        }
    }
    return sum;
}

You don't have to worry about n being negative, because you don't make recursive calls where that could happen. (Of course, you should guard against negative n in the original call as well.)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.