diff --git a/lib/fibonacci.rb b/lib/fibonacci.rb index 7465c25..d568134 100644 --- a/lib/fibonacci.rb +++ b/lib/fibonacci.rb @@ -1,8 +1,25 @@ # 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 < 0 + raise ArgumentError + end + return fib_helper(n, {}) +end + +def fib_helper(n, hash) + if hash.key? n + return hash[n] + end + + # basecase + if n == 0 || n == 1 + return n + end + + hash[n] = fib_helper(n - 1, hash) + fib_helper(n - 2, hash) + return hash[n] end diff --git a/lib/super_digit.rb b/lib/super_digit.rb index 33e367f..cf85791 100644 --- a/lib/super_digit.rb +++ b/lib/super_digit.rb @@ -1,15 +1,24 @@ # Superdigit -# Time Complexity - ? -# Space Complexity - ? +# Time Complexity - O(n) +# Space Complexity - O(1) def super_digit(n) - -end + s = n.to_s + if s.length <= 1 # basecase here + return n + end - -# Time Complexity - ? -# Space Complexity - ? -def refined_super_digit(n, k) - + sum = 0 + s.split("").each do |d| + sum += d.to_i + end + + return super_digit(sum) end - \ No newline at end of file + + +# # Time Complexity - ? +# # Space Complexity - ? +# def refined_super_digit(n, k) + +# end diff --git a/test/super_digit_test.rb b/test/super_digit_test.rb index 60da3a1..20973f1 100644 --- a/test/super_digit_test.rb +++ b/test/super_digit_test.rb @@ -1,6 +1,6 @@ require_relative "test_helper" -xdescribe "super_digit" do +describe "super_digit" do it "will return 2 for super_digit(9875)" do # Act answer = super_digit(9875) @@ -33,7 +33,7 @@ expect(answer).must_equal 6 end - describe "refined superdigit" do + xdescribe "refined superdigit" do it "will return 1 for n = 1 and k = 1" do # Act answer = refined_super_digit(1, 1)