Skip to content

Conversation

@HouseOfVange
Copy link

I relied heavily on the class recording for this assignment. I think it'd be a good idea for me to do more algorithm problems independentlyl.

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

🟢

elif num == 1:
return '1'

memo = [0,1,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.

memo = [0,1,1]
count = 3

while count <= 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.

Comment on lines +26 to +28
NCnums = []
for num in memo:
NCnums.append(str(num))

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. And avoid using identifiers that start with capital letters for things that aren't classes.

    nc_nums = [str(num) for num in memo]

or

    nc_nums = map(str, memo)

Comment on lines +5 to +6
Time Complexity: O(n), iterates through relative to the size of the list given.
Space Complexity: ? O(1), we will only ever make 2

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.

max_this_subarray = nums[0]

for i in range(1, len(nums)): # O(n)
max_this_subarray = max(max_this_subarray + nums[i], 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.

You could also refer back to the pseudocode presented in the project description for a slightly different (though equivalent) approach. Again, to me it's not obvious why Kadane's algorithm is correct, so do check out that linked article.

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