-
Notifications
You must be signed in to change notification settings - Fork 51
all tests passing #36
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
base: master
Are you sure you want to change the base?
Conversation
anselrognlie
left a comment
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.
✨ 💫 Looks good, Faith! I left some comments on your implementation below.
🟢
| Time Complexity: O(n) | ||
| Space Complexity: O(n) |
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.
👀 By carefully building up the calculations and storing them for later use, we only need to perform O(n) calculations. The storage to keep those calculations would be related to n (as is the converted string) giving space complexity of O(n) as well (ignoring a little bit of fiddliness related to the length of larger numbers being longer strings).
However, when building up the string representation, performing repeated string concatenation ends up recopying the intermediate strings with each new append. Now there's only ever the previous string and the new string in memory simultaneously, so the space complexity remains O(n), however, the time complexity actually becomes O(n^2). So this implementation has time complexity O(n^2) and space complexity O(n).
We could also avoid an additional O(n) growth of the stack by re-writing the recursive helper function as a while loop, but as written, it doesn't affect either the time or space complexity (though in practical terms, using recursion will add a bit to the time and space used).
| newman_nums = newman_nums[1:] | ||
| final_string = f'{newman_nums.pop(0)}' | ||
| for num in newman_nums: | ||
| final_string += ' ' + str(num) |
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.
👀 This repeated concatenation is what causes the O(n^2) time complexity.
Rather than repeated string concatenations, it's preferred to build up a list of values, and then join them all at the end. You already have a list of the numerical values, so we need only transform it into a list of strings, then join them. One way to accomplish this would be:
return " ".join(map(str, newman_nums[1:]))which converts each of the numbers in newman_nums (skipping the buffer value at position 0) to a string value in a new list, then joins those values separated by a space.
|
|
||
| newman_nums = [None] * (num + 1) | ||
|
|
||
| newman_helper(1) |
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.
👀 Consider using an iterative approach rather than a recursive helper to populate the sequence values
prev_seq = 1
cur_num = 1
for cur_num in range(1, num + 1):
if cur_num == 1 or cur_num == 2:
newman_nums[cur_num] = 1
else:
cur_seq = newman_nums[prev_seq] + newman_nums[cur_num - prev_seq]
newman_nums[cur_num] = cur_seq
prev_seq = cur_seqFurther simplifications could be applied, but this is a fairly direct translation from the structure of the helper method. The helper was really expressing a loop, so this makes things a little more obvious to the reader. It also sidesteps the scope mixing going on with the helper, where it's a little confusing to keep track of which of the variables being used are parameters being passed in to the current invocation, and which are bound from the enclosing scope (a closure in the current execution context).
| Time Complexity: O(n) | ||
| Space Complexity: O(1) |
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.
✨ Notice how better time complexity this approach achieves over a "naïve" approach of checking for the maximum achievable sum starting from every position and every length. The correctness of this approach might not be apparent, so I definitely encourage reading a bit more about it. This has a fairly good explanation, as well as a description of why this is considered a dynamic programming approach (on the face it might not "feel" like one).
Since like the fibonacci sequence, we are able to maintain a sliding window of recent values to complete our calculation, we can do it with a constant O(1) amount of storage.
|
|
||
| max_sum = nums[0] | ||
| current_sum = nums[0] | ||
| for i in range(1, len(nums)): |
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.
Another way we could skip over the first value is to slice the array, or use islice from itertools to avoid making a copy (be sure to add from itertools import islice):
for num in islice(nums, 1, None):Then in the loop, replace nums[i] with num.
No description provided.