Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leaves - Emily V #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions lib/fibonacci.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
# Improved Fibonacci
# Time Complexity - O(n)
# Space Complexity - O(n)

# Time Complexity - ?
# Space Complexity - ? (should be O(n))
# Hint, you may want a recursive helper method
def fibonacci(n)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


return fib_helper(0, 1, 2, n)
end

def fib_helper(fib_minus_two, fib_minus_one, current, n)
return n if n == 0 || n == 1
raise ArgumentError if n < 0

if current == n
return fib_minus_one + fib_minus_two
end

return fib_helper(fib_minus_one, fib_minus_one + fib_minus_two, current + 1, n)
end
35 changes: 29 additions & 6 deletions lib/super_digit.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
# Superdigit

# Time Complexity - ?
# Space Complexity - ?
# Time Complexity - O(1) <- this doesn't feel quite right but it's definitely not O(n)
# Space Complexity - O(1) <- same here
Comment on lines +3 to +4

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct it's O(log10n)

def super_digit(n)
return super_digit_helper(n.to_s)
end

def super_digit_helper(n)
return n.to_i if n.length == 1

sum = 0
n.each_char do |digit|
sum += digit.to_i
end

return super_digit_helper(sum.to_s)
end

# Time Complexity - O(1) <- same qualms as above
# Space Complexity - O(1)
# I think I missed the point here, this doesn't seem like it would necessarily involve more time complexity than regular super_digit so I'm not sure how to reduce it

# Time Complexity - ?
# Space Complexity - ?
def refined_super_digit(n, k)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the bute-force solution, can you think of a better way to do this with dynamic programming?


return refined_super_digit_helper(n.to_s * k)
end

def refined_super_digit_helper(n)
return n.to_i if n.length == 1

sum = 0
n.each_char do |digit|
sum += digit.to_i
end

return super_digit_helper(sum.to_s)
end

2 changes: 1 addition & 1 deletion test/super_digit_test.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down