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:
Aboobacker MK 2021-08-11 09:38:43 +05:30 committed by GitHub
parent f7538d07b4
commit f4d6346106
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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)