Skip to content

Conversation

@IvetteDF
Copy link

@IvetteDF IvetteDF commented Jul 7, 2022

No description provided.

@anselrognlie anselrognlie self-requested a review July 13, 2022 18:19
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, Ivette! I left some comments on your implementation below.

🟢

Comment on lines +18 to +20
outputs = {}

res = []

Choose a reason for hiding this comment

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

We don't really need two separate places to store the results. Since we are building up P(n) from smaller P() values, as long as we calculate the them in a strictly increasing order (as you do), we could store them in order in an array for later lookup. Using a dictionary can be more helpful if the repeated calculations happen in a less predictable order (often when we are using a recursion+memoization approach).

The other thing you're using the two representations for is to store the numerical result in one, and the string representation in the other. We could covert the numerical values to strings as part of joining them together by using map or a list comprehension.

Otherwise, great approach!

outputs[n] = value
res.append(str(value))

return " ".join(res)

Choose a reason for hiding this comment

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

As mentioned above, we could store the numerical values in res for use during the calculation. But since the str join method only knows how to join together strings, we need to convert them from ints. We could accomplish this with methods resembling the following:

    return " ".join(map(str, res))

or

    return " ".join(str(num) for num in res)

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! We do only make one pass over the desired number of terms during the calculation. The the join call also iterates through the string results, giving us O(2n) → O(n) for time. There is a cost we could try to account for in stringifying the numerical values (they get longer as the numbers get larger) but we can ignore that for the purposes of this exercise.

Storage-wise, we do need n items both in the dictionary and list, as well as the final returned string for O(3n) → O(n), again being a little hand-wavey with the overall size of the final string (and ignoring the sizes of the intermediate strings).

Comment on lines +8 to +9
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.

@@ -1,12 +1,25 @@

from json.encoder import INFINITY

Choose a reason for hiding this comment

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

👀 The INFINITY symbol is really just an alias for float('inf'), which you can access without needing to import anything from the json module.

return 0
pass

maximum = - INFINITY

Choose a reason for hiding this comment

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

Another approach would be to initialize maximum to some value actually found in the list, say nums[0], which we know must at least exist from the guard checks.

maximum = - INFINITY
current_max = 0

for num in nums:

Choose a reason for hiding this comment

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

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