Skip to content

Conversation

@mac-madison
Copy link

No description provided.

@anselrognlie anselrognlie self-requested a review July 23, 2022 21:13
Copy link

@anselrognlie anselrognlie left a 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!

🟡

Comment on lines -7 to -8
Time Complexity: ?
Space Complexity: ?

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

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:

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.

Comment on lines +21 to +22
for i in range(2, len(memo)):
result += " " + str(memo[i])

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.

Comment on lines +4 to +5
if num == 0:
raise ValueError

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

Comment on lines -5 to -6
Time Complexity: ?
Space Complexity: ?

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])

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants