Short C++ DP code


  • 0
    G
        int climbStairs(int n) {
            vector<int> dp(n, -1);
            return _climbStairs(0, n, dp);
        }
        int _climbStairs(int i, int n, vector<int>& dp) {
            if (n == 0 || i > n) return 0;
            else if (i == n) return 1;
            else if (dp[i] == -1) dp[i] = _climbStairs(i + 1, n, dp) + _climbStairs(i + 2, n, dp);
            return dp[i];
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.