Dynamic Programming - Memoization Solution!


  • 0
    B

     public class Solution {
      public int climbStairs(int n) {
        if (n > 0) {
            int steps[] = new int [n+1];
            return countWays(n, steps);
        } else {
            return 1;
        }
    }
    
    public int countWays(int n , int steps[]) {
        if (n < 0) return 0;
        if (n == 0) return 1;
        if (steps[n] <= 0) {
            steps [n] = countWays(n - 1, steps) + countWays(n - 2, steps);
        }
        return steps[n];
     }
    }
    

Log in to reply
 

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