Java solution with caching results


  • 0
    A
    public class Solution {
        public int climbStairs(int n) {
            int[] map = new int[n + 1];
            Arrays.fill(map, -1);
            return climbStairs(n, map);
        }
        
        private int climbStairs(int n, int[] map) {
            if (n < 0) {
                return 0;
            }
            else if (n == 0) {
                return 1;
            }
            else if (map[n] > -1) {
                return map[n];
            }
            else {
                map[n] = climbStairs(n-1, map) + climbStairs(n-2, map);
                return map[n];
            }
        }
        
    }

Log in to reply
 

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