-
Notifications
You must be signed in to change notification settings - Fork 51
cedar mac #43
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?
cedar mac #43
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, 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: ? |
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.
👀 Time and space complexity?
|
|
||
| memo = [None] * (num + 1) | ||
|
|
||
| memo[0] = 0 |
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.
✨ 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👀 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👀 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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: ? |
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.
👀 Time and 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✨ 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.