Skip to content

Conversation

@DLozanoC
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, Diana! I left some comments on your implementation below.

🟢

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?

nc_nums = [1, 1]
if num < 1:
raise ValueError("Num must be greater than 0.")
for i in range(2, num + 1):

Choose a reason for hiding this comment

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

Since you're always truncating the returned list using num as the end, we only need to calculate up to the same limit (num) here.

Comment on lines +15 to +17
string_list = []
for n in nc_nums:
string_list.append(str(n))

Choose a reason for hiding this comment

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

Consider using a comprehension or map to apply the str transformation to each numeric value.

    string_list = [str(n) for n in nc_nums]

or

    string_list = map(str, nc_nums)

string_list = []
for n in nc_nums:
string_list.append(str(n))
return " ".join(string_list[0:num])

Choose a reason for hiding this comment

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

The need to slice the array here comes from needing to handle the base case for num being 1 (you initialize your calculation array to start with [1, 1], or num = 2). If you added an earlier check for the case of num being 1, you could omit the slicing here to be

    return " ".join(string_list)

Comment on lines +6 to +7
Time Complexity: O(n)
Space Complexity: O(1)

Choose a reason for hiding this comment

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

✨ Notice how better time complexity this approach achieves over a "naïve" approach of checking for the maximum achievable sum starting from every position and every length. The correctness of this approach might not be apparent, so I definitely encourage reading a bit more about it. This has a fairly good explanation, as well as a description of why this is considered a dynamic programming approach (on the face it might not "feel" like one).

Since like the fibonacci sequence, we are able to maintain a sliding window of recent values to complete our calculation, we can do it with a constant O(1) amount of storage.

Comment on lines +18 to +21
if current_sum + nums[i] > nums[i]:
current_sum += nums[i]
else:
current_sum = 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 nice way to represent this calculation, which captures the underlying invariant that makes Kadane's algorithm work.

Consider using max to write it as

        current_sum = max(current_sum + nums[i], nums[i])

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