Recursion: Davis' Staircase Discussions | | HackerRank

Recursion: Davis' Staircase

  • + 0 comments

    Recursion function to avoid TLE :

    int solve(int n, int index, vector &dp){

    if(index == n){
        return 1;
    }
    if(index > n){
        return 0;
    }
    if(dp[index]!=-1){
        return dp[index];
    }
    return dp[index] = solve(n,index+1, dp)+solve(n,index+2, dp)+solve(n,index+3, dp);    
    

    }

    int stepPerms(int n) { vectordp(n+1,-1); int ans = solve(n, 0, dp) % mod;

    return ans;
    

    }