O(n) easy Ruby


  • 0
    T
    def climb_stairs(n)
        ways = []
        ways[1] = {step: 1, nostep: 1}
        (2..n).each do |i|
            ways[i] = {step: (ways[i - 1][:nostep] + ways[i - 1][:step]), nostep: ways[i - 1][:step]}
        end
        return ways[n][:step]
    end
    

Log in to reply
 

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