n steps with 1, 2 or 3 steps taken. How many ways to get to the top?

前端 未结 13 764
时光说笑
时光说笑 2020-12-08 08:12

If we have n steps and we can go up 1 or 2 steps at a time, there is a Fibonacci relation between the number of steps and the ways to climb them. IF and ONLY if we do not co

相关标签:
13条回答
  • 2020-12-08 08:57

    Count total number of ways to cover the distance with 1, 2 and 3 steps.

    Recursion solution time complexity is exponential i.e. O(3n).

    Since same sub problems are solved again, this problem has overlapping sub problems property. So min square sum problem has both properties of a dynamic programming problem.

    public class MaxStepsCount {
    
         /** Dynamic Programming. */
         private static int getMaxWaysDP(int distance) {
    
               int[] count = new int[distance+1];
               count[0] = 1;
               count[1] = 1;
               count[2] = 2;
    
               /** Memorize the Sub-problem in bottom up manner*/
               for (int i=3; i<=distance; i++) {
                    count[i] = count[i-1] + count[i-2] + count[i-3];               
               }
               return count[distance];
         }
    
    
         /** Recursion Approach. */
         private static int getMaxWaysRecur(int distance) {
               if(distance<0) {
                    return 0;
               } else if(distance==0) {
                    return 1;
               }
               return getMaxWaysRecur(distance-1)+getMaxWaysRecur(distance-2)
                         +getMaxWaysRecur(distance-3);
         }
    
         public static void main(String[] args) {
               // Steps pf 1, 2 and 3.
               int distance = 10;
    
               /** Recursion Approach. */
               int ways = getMaxWaysRecur(distance);
               System.out.println(ways);
    
               /** Dynamic Programming. */
               ways = getMaxWaysDP(distance);
               System.out.println(ways);
         }
    }
    

    My blog post on this:

    http://javaexplorer03.blogspot.in/2016/10/count-number-of-ways-to-cover-distance.html

    0 讨论(0)
  • 2020-12-08 08:59

    Java recursive implementation based on Michał's answer:

    public class Tribonacci {
        // k(0) = 1
        // k(1) = 1
        // k(2) = 2
        // k(3) = 4
        // ...
        // k(n) = k(n-3) + k(n-2) + k(n - 1)
    
        static int get(int n) {
            if (n == 0) {
                return 1;
            } if (n == 1) {
                return 1;
            } else if (n == 2) {
                return 2;
            //} else if (n == 3) {
            //    return 4;
            } else {
                return get(n - 3) + get(n - 2) + get(n - 1);
            }
        }
    
        public static void main(String[] args) {
            System.out.println("Tribonacci sequence");
            System.out.println(Tribonacci.get(1));
            System.out.println(Tribonacci.get(2));
            System.out.println(Tribonacci.get(3));
            System.out.println(Tribonacci.get(4));
            System.out.println(Tribonacci.get(5));
            System.out.println(Tribonacci.get(6));
        }
    }
    
    0 讨论(0)
  • 2020-12-08 09:01

    Maybe its just 2^(n-1) with n being the number of steps?

    It makes sence for me because with 4 steps you have 8 possibilities:

    4,
    3+1,
    1+3,
    2+2,
    2+1+1,
    1+2+1,
    1+1+2,
    1+1+1+1,
    

    I guess this is the pattern

    0 讨论(0)
  • 2020-12-08 09:03

    Count ways to reach the nth stair using step 1, 2, 3.

    We can count using simple Recursive Methods.

    // Header File
    #include<stdio.h>
    // Function prototype for recursive Approch
    int findStep(int);
    int main(){
        int n;
        int ways=0;
        ways = findStep(4);
        printf("%d\n", ways);
    return 0;
    }
    // Function Definition
    int findStep(int n){
        int t1, t2, t3;
        if(n==1 || n==0){
            return 1;
        }else if(n==2){
            return 2;
        }
        else{
            t3 = findStep(n-3);
            t2 = findStep(n-2);
            t1 = findStep(n-1);
            return t1+t2+t3;
        }
    }
    
    0 讨论(0)
  • 2020-12-08 09:12

    Easily get the intuition for the problem:

    Think you are climbing stairs and the possible steps you can take are 1 & 2

    The total no. of ways to reach step 4 = Total no. of ways to reach step 3 + Total no of ways to reach step 2

    How?

    Basically, there are only two possible steps from where you can reach step 4.

    1. Either you are in step 3 and take one step
    2. Or you are in step 2 and take two step leap

    These two are the only possibilities by which you can ever reach step 4

    Similarly, there are only two possible ways to reach step 2

    1. Either you are in step 1 and take one step
    2. Or you are in step 0 and take two step leap

    F(n) = F(n-1) + F(n-2)

    F(0) = 0 and F(1) = 1 are the base cases. From here you can start building F(2), F(3) and so on. This is similar to Fibonacci series.

    If the number of possible steps is increased, say [1,2,3], now for every step you have one more option i.e., you can directly leap from three steps prior to it

    Hence the formula would become

    F(n) = F(n-1) + F(n-2) + F(n-3)

    See this video for understanding Staircase Problem Fibonacci Series

    Easy understanding of code: geeksforgeeks staircase problem

    0 讨论(0)
  • 2020-12-08 09:14

    Recursive memoization based C++ solution: You ask a stair how many ways we can go to top? If its not the topmost stair, its going to ask all its neighbors and sum it up and return you the result. If its the topmost stair its going to say 1.

    vector<int> getAllStairsFromHere(vector<int>& numSteps,  int& numStairs, int currentStair)
    {
        vector<int> res;
    
        for(auto it : numSteps)
            if(it + currentStair <= numStairs)
                res.push_back(it + currentStair);
    
        return res;
    }
    
    
    int numWaysToClimbUtil(vector<int>& numSteps, int& numStairs, int currentStair, map<int,int>& memT)
    {
        auto it = memT.find(currentStair);
        if(it != memT.end())
            return it->second;
    
        if(currentStair >= numStairs)
            return 1;
    
        int numWaysToClimb = 0;
        auto choices = getAllStairsFromHere(numSteps,  numStairs, currentStair);
        for(auto it : choices)
            numWaysToClimb += numWaysToClimbUtil(numSteps, numStairs, it, memT);
    
    
        memT.insert(make_pair(currentStair, numWaysToClimb));
        return memT[currentStair];
    }
    
    
    int numWaysToClimb(vector<int>numSteps, int numStairs)
    {
        map<int,int> memT;
        int currentStair = 0;
        return numWaysToClimbUtil(numSteps, numStairs, currentStair, memT);
    }
    
    0 讨论(0)
提交回复
热议问题