Skip to content

Conversation

@wangjiajia95
Copy link

No description provided.

@anselrognlie anselrognlie self-requested a review July 18, 2022 22:28
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.

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

🟡

Comment on lines +7 to +8
Time Complexity: o(n)
Space Complexity: o(n)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment on lines +11 to +12
if num == 0:
raise ValueError("n must be > 0")

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("n must be > 0")

Space Complexity: o(n)
"""
pass
nc_list = [0] * (num + 1)

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.


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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨ 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)):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨ 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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀 Not used

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