Conversation
anselrognlie
left a comment
There was a problem hiding this comment.
✨ 💫 Looks good, mac! But since you're missing the time and space complexity, I'm evaluating this as a yellow. Feel free to update your submission with the complexity for a green!
🟡
| Time Complexity: ? | ||
| Space Complexity: ? |
|
|
||
| memo = [None] * (num + 1) | ||
|
|
||
| memo[0] = 0 |
There was a problem hiding this comment.
✨ Nice use of a buffer slot to account for the 1-based calculation.
| memo[2] = 1 | ||
|
|
||
| i = 3 | ||
| while i <= num: |
There was a problem hiding this comment.
👀 Prefer a for loop, since we know exactly how many times this will loop, especially since you went to the trouble to pre-allocate your memo list.
| for i in range(2, len(memo)): | ||
| result += " " + str(memo[i]) |
There was a problem hiding this comment.
👀 Repeated concatenation causes the O(n^2) time complexity (due to each concatenation needing to copy every previous concatenation).
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, memo[1:]))which converts each of the numbers in memo (skipping the buffer value at position 0) to a string value in a new list, then joins those values separated by a space.
| if num == 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 num <= 0:
raise ValueError| Time Complexity: ? | ||
| Space Complexity: ? |
| current_max = nums[0] | ||
|
|
||
| for i in range(1, len(nums)): | ||
| current_max = max(nums[i], current_max + nums[i]) |
There was a problem hiding this comment.
✨ This is a really nice way to represent this calculation, which captures the underlying invariant that makes Kadane's algorithm work. This article has a fairly good explanation of why this is considered a dynamic programming approach (on the face it might not "feel" like one).
No description provided.