mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-17 06:11:43 +01:00
Update dynamic_programming/fast_fibonacci.rb
This commit is contained in:
parent
b1f6170d02
commit
0a5ce13816
1 changed files with 18 additions and 2 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue