Skip to content

Conversation

@asliathman
Copy link

No description provided.

@anselrognlie anselrognlie self-requested a review July 23, 2022 21:12
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, Alf! I left some comments on your implementation below.

🟢

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

Space Complexity: O(n)
"""
pass
outputs = {}

Choose a reason for hiding this comment

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

The structure we use to store the memo data can be a list, since we are carefully adding up from the bottom. A dict can be useful if we are storing results for non-integer keys, or if the order that we calculate values is less-predictable.


val = outputs[outputs[n-1]] + outputs[n - outputs[n - 1]]
outputs[n] = val
res.append(str(val))

Choose a reason for hiding this comment

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

Another approach to could be to calculate the set of values using a list as the backing memo structure, then convert the whole structure into strings all at once at the end. It's not really any more efficient, but by focusing on doing one thing in the main calculation (finding the needed numbers) and then doing the conversion separately, we separate the two phases a little which can help with understandability by somewhat separating the concerns.

Comment on lines +5 to +6
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.

return 0
pass

max = - 10000000000000000000000000000000000000000000

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 the maximum to some value actually found in the list, say nums[0], which we know must at least exist from the guard checks.

max = - 10000000000000000000000000000000000000000000
curr_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