Update dynamic_programming/fast_fibonacci.rb

This commit is contained in:
Vitor Oliveira 2021-03-07 12:32:55 -08:00 committed by GitHub
parent b1f6170d02
commit 0a5ce13816
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,6 +1,22 @@
# Given a number, calculates the nth fibonacci number.
# The Fibonacci numbers, commonly denoted F(n) form a sequence,
# called the Fibonacci sequence, such that # each number is the sum
# of the two preceding ones, starting from 0 and 1. That is,
def fast_fibonacci(number, memo_hash = {})
# F(0) = 0, F(1) = 1
# F(n) = F(n - 1) + F(n - 2), for n > 1.
# Given n, calculate F(n).
#
# Approach: Bottom-Up Approach using Memoization
#
# Complexity Analysis:
#
# Time complexity: O(n)
# Space complexity: O(n)
#
def fibonacci(number, memo_hash = {})
if number == 0 || number == 1
return number
end