-
Notifications
You must be signed in to change notification settings - Fork 48
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
Janice H. #32
base: master
Are you sure you want to change the base?
Janice H. #32
Changes from all commits
d961033
0ca11d9
e68add4
46c6da2
3b1c548
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,19 @@ | ||
# Improved Fibonacci | ||
# # Improved Fibonacci | ||
|
||
# # Time Complexity - O(n) | ||
# # Space Complexity - O(n) (should be O(n)) | ||
# # Hint, you may want a recursive helper method | ||
|
||
# Time Complexity - ? | ||
# Space Complexity - ? (should be O(n)) | ||
# Hint, you may want a recursive helper method | ||
def fibonacci(n) | ||
|
||
raise ArgumentError if n < 0 | ||
return fib_helper([0, 1], 2, n) | ||
end | ||
|
||
def fib_helper(fib_saver, current, n) | ||
return n if n == 0 || n == 1 | ||
return fib_saver.sum if current == n | ||
new_sum = fib_saver.sum | ||
fib_saver[0] = fib_saver[1] | ||
fib_saver[1] = new_sum | ||
Comment on lines
+15
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes! |
||
return fib_helper(fib_saver, current + 1, n) | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,23 @@ | ||
# Superdigit | ||
|
||
# Time Complexity - ? | ||
# Space Complexity - ? | ||
# Time Complexity - O(nlog10n)? | ||
# Space Complexity - O(log10n)? | ||
def super_digit(n) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
return n if n < 10 | ||
super_digit(digitize(n)) | ||
end | ||
|
||
|
||
# Time Complexity - ? | ||
# Space Complexity - ? | ||
def digitize(n) | ||
return n if n < 10 | ||
sum = 0 | ||
single_digit = n % 10 | ||
sum += single_digit | ||
return sum + digitize(n / 10) | ||
end | ||
|
||
|
||
# Time Complexity - I'm pretty sure this solution doesn't reduce the time complexity, unfortunately. So it's O(nlog10n) still? | ||
# Space Complexity - O(log10n)? | ||
def refined_super_digit(n, k) | ||
super_digit(n * k) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The trick here is to find the superdigit of n and then multiply the result by k. |
||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍