diff --git a/lib/fibonacci.rb b/lib/fibonacci.rb index 7465c25..73fad8e 100644 --- a/lib/fibonacci.rb +++ b/lib/fibonacci.rb @@ -1,8 +1,18 @@ # Improved Fibonacci -# Time Complexity - ? -# Space Complexity - ? (should be O(n)) +# Time Complexity - O(n) +# Space Complexity - O(n) # Hint, you may want a recursive helper method def fibonacci(n) - + if n in results + return results(n) + else + if n == 0 + return 0 + if n == 1 + return 1 + end + end + end + results[n] = fib(n-1) + fib(n-2) end diff --git a/lib/super_digit.rb b/lib/super_digit.rb index 33e367f..b500b48 100644 --- a/lib/super_digit.rb +++ b/lib/super_digit.rb @@ -1,15 +1,16 @@ # Superdigit -# Time Complexity - ? -# Space Complexity - ? +# Time Complexity - O(n) +# Space Complexity - O(n) def super_digit(n) - + return n if n < 10 + ans = n % 10 + super_digit(n/10) # over 10 + ans >= 10 ? super_digit(ans) : ans end - + # Time Complexity - ? # Space Complexity - ? def refined_super_digit(n, k) - + #will come back too. end - \ No newline at end of file