Conversation
anselrognlie
left a comment
There was a problem hiding this comment.
✨ Your implementations look good, Jiajia! I left some comments on your implementation below.
Because of the importance of thinking about complexity for this project, I've evaluated this as a yellow due to the missing complexities for the Largest Sum Contiguous Subarray problem (wave 02). A yellow is a passing score so resubmission is not required, but you are free to resubmit with that time and space complexity filled out for a green score.
🟡
| Time Complexity: o(n) | ||
| Space Complexity: o(n) |
There was a problem hiding this comment.
✨ Great! 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 (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).
| if num == 0: | ||
| raise ValueError("n must be > 0") |
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("n must be > 0")| Space Complexity: o(n) | ||
| """ | ||
| pass | ||
| nc_list = [0] * (num + 1) |
There was a problem hiding this comment.
✨ Nice use of a buffer slot to account for the 1-based calculation.
|
|
||
| nc_list[i] = nc_list[nc_list[i-1]] + nc_list[i - nc_list[i-1]] | ||
|
|
||
| return " ".join(str(v) for v in nc_list[1:]) |
There was a problem hiding this comment.
✨ Nice use of a generator to convert the numeric results to strings. This is a generator rather than a list comprehension because it lacks the [] around the comprehension expression. A generator produces a sequence of values (here, the stringified sequence values) and can be used anywhere an iterable value is needed.
Another approach would be to make uses of the map function
return " ".join(map(str, nc_list[1:]))| largest_sum = [0] * len(nums) | ||
| largest_sum[0] = nums[0] | ||
|
|
||
| for i in range(1, len(nums)): |
There was a problem hiding this comment.
✨ Nice approach that really shows why Kadane's algorithm is really a form of dynamic programming.
👀 What's the time and space complexity of this approach? Could we make an adjustment to who we're carrying along the previous results to improve the space complexity a bit more?
| @@ -1,4 +1,4 @@ | |||
|
|
|||
| import math | |||
No description provided.