From f4d6346106b0d5e0620ce71c837d5e49ed2a0854 Mon Sep 17 00:00:00 2001 From: Aboobacker MK Date: Wed, 11 Aug 2021 09:38:43 +0530 Subject: [PATCH] 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 --- dynamic_programming/fibonacci.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/fibonacci.rb b/dynamic_programming/fibonacci.rb index a7aa6e9..617a083 100644 --- a/dynamic_programming/fibonacci.rb +++ b/dynamic_programming/fibonacci.rb @@ -30,7 +30,7 @@ def fibonacci(number, memo_hash = {}) end 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)