Skip to content

Conversation

@k-nox
Copy link

@k-nox k-nox commented Jul 8, 2022

No description provided.

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

🟢

Comment on lines +5 to +6
Time Complexity: O(n) where n is num
Space Complexity: O(n) where n is num

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 sliced copy, and 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).

if num == 1:
return "1"

nums = [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.

nums = [0, 1, 1]

i = 3
while i <= 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.

nums.append(nums[nums[i - 1]] + nums[i - nums[i - 1]])
i += 1

return " ".join([str(n) for n in nums[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 list comprehension to convert the int values to strings for use in the join.

We could also use map

    return " ".join(map(str, nums[1:]))

We could consider using islice (from itertools) to lop of that first character without making an actual copy:

    return " ".join(map(str, islice(nums, 1, None))

Comment on lines +5 to +6
Time Complexity: O(n) where n is len(nums)
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.

max_so_far = nums[0]
curr_max = nums[0]
for i in range(1, len(nums)):
curr_max = max(curr_max + 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.

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