Ruby Solution using Dynamic Programming


  • 0
    M
    def num_trees(n)
        g = Array.new(n + 1){ 0 }
        g[0] = g[1] = 1
        2.upto(n) do |i|
            1.upto(i) do |j|
                g[i] += g[j - 1] * g[i - j]
            end
        end
        g[n]
    end
    

Log in to reply
 

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