mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-26 21:58:56 +01:00
Fix the complexity memoisation check
keys.include? number is not cheap as it is search operation on array and it increases the time complexity. replaced it with `key?` method to fix memoisation check part on O(1) complexity
This commit is contained in:
parent
f7538d07b4
commit
f4d6346106
1 changed files with 1 additions and 1 deletions
|
@ -30,7 +30,7 @@ def fibonacci(number, memo_hash = {})
|
||||||
end
|
end
|
||||||
|
|
||||||
def memoize(number, memo_hash)
|
def memoize(number, memo_hash)
|
||||||
return memo_hash[number] if memo_hash.keys.include? number
|
return memo_hash[number] if memo_hash.key? number
|
||||||
|
|
||||||
memo_hash[number] = memoize(number - 1, memo_hash) + memoize(number - 2, memo_hash)
|
memo_hash[number] = memoize(number - 1, memo_hash) + memoize(number - 2, memo_hash)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue