Conversation
anselrognlie
left a comment
There was a problem hiding this comment.
✨ 💫 Looks good, Lilly! I left some comments on your implementation below.
🟢
| Time Complexity: O(n) | ||
| Space Complexity: O(n^2) |
There was a problem hiding this comment.
👀 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 is related to n, 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).
| return "1 1" | ||
|
|
||
| seq = "" | ||
| start_list = [0, 1, 1] |
There was a problem hiding this comment.
✨ Nice use of a buffer slot to account for the 1-based calculation.
| for num in start_list[1:]: | ||
| seq += str(num) + " " |
There was a problem hiding this comment.
👀 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, start_list[1:]))| if nums == 0: | ||
| raise ValueError |
There was a problem hiding this comment.
We should raise this error for any value below the valid starting point of the sequence:
if nums <= 0:
raise ValueError| Time Complexity: O(n) | ||
| Space Complexity: O(1) |
There was a problem hiding this comment.
✨ 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.
| return 0 | ||
| pass | ||
| n = len(nums) | ||
| max_sum = -10000000000 |
There was a problem hiding this comment.
👀 What if the list were made of negative numbers all below -10000000000?
We could use an impossibly small value (like float('-inf')) or grab a value from the array (we know that nums[0] at least must exist).
| max_sum = -10000000000 | ||
| curr_sum = 0 | ||
|
|
||
| for i in range(n): |
No description provided.