Wednesday, August 27, 2014

Climb Stairs

Simple question, that's why its AC rate is very high.It is almost same as the 1st question in DP chapter of CC 150. Actually, I solved it once. But, as a practice on DP, I still worked on it. Code is shown as below:
 public class Solution {  
   public int climbStairs(int n) {  
     int[] arr = new int[n+1];  
     Arrays.fill(arr,-1);  
     return climbStairs(n, arr);  
   }  
   public int climbStairs(int n, int[] arr) {  
     if(n < 0){  
       return 0;  
     }  
     if(n == 0){  
       return 1;  
     }  
     if(arr[n] != -1) return arr[n];  
     arr[n] = climbStairs(n-1,arr) + climbStairs(n-2,arr);  
     return arr[n];  
   }  
 }  

No comments:

Post a Comment