C# - recursion with memoization


  • 0
    R
    public int ClimbStairs (int n) {
        Dictionary<int, int> mem = new Dictionary<int, int> ();
        mem.Add (0, 0);
        mem.Add (1, 1);
        mem.Add (2, 2);
        return ClimbStairs (n, mem);
    }
    private int ClimbStairs (int n, Dictionary<int, int> mem) {
        if (mem.ContainsKey (n)) return mem[n];
        var result = ClimbStairs (n - 1, mem) + ClimbStairs (n - 2, mem);
        mem.Add (n, result);
        return result;
    }

Log in to reply
 

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