Fibonacci solution using memoization


  • 0
    T
    public class Solution {
        Map<Integer,Integer> map = new HashMap<>();
        public int climbStairs(int n) {
            if(n<=0) return 0;
            if(n==1) return 1;
            return helper(n+1);
        }
    
    private int helper(int n){
        if(n==0) return 0;
        if(n==1 || n==2) return 1;
        
        if(map.containsKey(n)) return map.get(n);
        int answer = helper(n-1)+helper(n-2);
        map.put(n,answer);
        return answer;
        }
    }

Log in to reply
 

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