Algorithm for a stair climbing permutation

谁说胖子不能爱 提交于 2019-12-23 06:13:06

问题


I've been asked to build an algorithm that involves a permutation and I'm a little stumped and looking for a starting place. The details are this...

You are climbing a staircase of n stairs. Each time you can either climb 1 or 2 steps at a time. How many distinct ways can you climb to the top?

Any suggestions how I can tackle this challenge?


回答1:


You're wrong about permutations. Permutations involve orderings of a set. This problem is something else.

Problems involving simple decisions that produce another instance of the same problem are often solvable by dynamic programming. This is such a problem.

When you have n steps to climb, you can choose a hop of either 1 or 2 steps, then solve the smaller problems for n-1 and n-2 steps respectively. In this case you want to add the numbers of possibilities. (Many DPs are to find minimums or maximums instead, so this is a bit unusual.)

The "base cases" are when you have either 0 or 1 step. There's exactly 1 way to traverse each of these.

With all that in mind, we can write this dynamic program for the number of ways to climb n steps as a recursive expression:

W(n) = W(n - 1) + W(n - 2)  if n > 1
       1                    n == 0, 1

Now you don't want to implement this as a simple recursive function. It will take time exponential in n to compute because each call to W calls itself twice. Yet most of those calls are unnecessary repeats. What to do?

One way to get the job done is find a way to compute the W(i) values in sequence. For a 1-valued DP it's usually quite simple, and so it is here:

W(0) = 1
W(1) = 1  (from the base case)
W(2) = W(1) + W(0) = 1 + 1 = 2
W(3) = W(2) + W(1) = 2 + 1 = 3

You get the idea. This is a very simple DP indeed. To compute W(i), we need only two previous values, W(i-1) and W(i-2). A simple O(n) loop will do the trick.

As a sanity check, look at W(3)=3. Indeed, to go up 3 steps, we can take hops of 1 then 2, 2 then 1, or three hops of 1. That's 3 ways!

Can't resist one more? W(4)=2+3=5. The hop sequences are (2,2), (2,1,1), (1,2,1), (1,1,2), and (1,1,1,1): 5 ways.

In fact, the chart above will look familiar to many. The number of ways to climb n steps is the (n+1)th Fibonacci number. You should code the loop yourself. If stuck, you can look up any of the hundreds of posted examples.




回答2:


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");
    }
}

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



回答3:


Since it looks like a programming assignment, I will give you the steps to get you started instead of giving the actual code:

  • You can write a recursive function which keeps track of the step you are on.
  • If you have reached step n then you found one permutation or if you are past n then you should discard it.
  • At every step you can call the function by incrementing the steps by 1 and 2
  • The function will return the number of permutations possible once finished


来源:https://stackoverflow.com/questions/33292472/algorithm-for-a-stair-climbing-permutation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!