mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-26 21:58:56 +01:00
Update climbing_stairs.rb
This commit is contained in:
parent
4a38db0764
commit
70a1b9353a
1 changed files with 14 additions and 1 deletions
|
@ -41,7 +41,7 @@ def climb_stairs(n)
|
||||||
memo[0] = 1
|
memo[0] = 1
|
||||||
memo[1] = 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|
|
(2..n).each do |n|
|
||||||
recurse(n, memo)
|
recurse(n, memo)
|
||||||
|
@ -56,3 +56,16 @@ def recurse(n, memo)
|
||||||
|
|
||||||
memo[n] = recurse(n - 1, memo) + recurse(n - 2, memo)
|
memo[n] = recurse(n - 1, memo) + recurse(n - 2, memo)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
puts climb_stairs(2)
|
||||||
|
# => 2
|
||||||
|
|
||||||
|
puts climb_stairs(4)
|
||||||
|
# => 5
|
||||||
|
|
||||||
|
puts climb_stairs(10)
|
||||||
|
# => 89
|
||||||
|
|
||||||
|
puts climb_stairs(45)
|
||||||
|
# => 1836311903
|
||||||
|
|
Loading…
Reference in a new issue