From 70a1b9353a01f5e9dbab3e2d030fb906060f9bab Mon Sep 17 00:00:00 2001 From: sidaksohi <31377210+sidaksohi@users.noreply.github.com> Date: Tue, 24 Aug 2021 18:33:53 -0700 Subject: [PATCH] Update climbing_stairs.rb --- dynamic_programming/climbing_stairs.rb | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/dynamic_programming/climbing_stairs.rb b/dynamic_programming/climbing_stairs.rb index 8b7e26e..8f058b7 100644 --- a/dynamic_programming/climbing_stairs.rb +++ b/dynamic_programming/climbing_stairs.rb @@ -41,7 +41,7 @@ def climb_stairs(n) memo[0] = 1 memo[1] = 1 - return memo[n] if n <= 1 && n >= 0 + return memo[n] if n == 0 || n == 1 (2..n).each do |n| recurse(n, memo) @@ -56,3 +56,16 @@ def recurse(n, memo) memo[n] = recurse(n - 1, memo) + recurse(n - 2, memo) end + + +puts climb_stairs(2) +# => 2 + +puts climb_stairs(4) +# => 5 + +puts climb_stairs(10) +# => 89 + +puts climb_stairs(45) +# => 1836311903