Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"python.testing.pytestArgs": [
"."
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
16 changes: 13 additions & 3 deletions lib/max_subarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@
def max_sub_array(nums):
""" Returns the max subarray of the given list of numbers.
Returns 0 if nums is None or an empty list.
Time Complexity: ?
Space Complexity: ?
Time Complexity: O(n)
Space Complexity: O(1)
Comment on lines +5 to +6

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.

"""
if nums == None:
return 0
if len(nums) == 0:
return 0
pass
n = len(nums)
max_sum = -10000000000

Choose a reason for hiding this comment

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

👀 What if the list were made of negative numbers all below -10000000000?

We could use an impossibly small value (like float('-inf')) or grab a value from the array (we know that nums[0] at least must exist).

curr_sum = 0

for i in range(n):

Choose a reason for hiding this comment

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

curr_sum += nums[i]
if curr_sum > max_sum:
max_sum = curr_sum
if curr_sum < 0:
curr_sum = 0
return max_sum
27 changes: 23 additions & 4 deletions lib/newman_conway.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,28 @@

# Time complexity: ?
# Space Complexity: ?
def newman_conway(num):
def newman_conway(nums):
""" Returns a list of the Newman Conway numbers for the given value.
Time Complexity: ?
Space Complexity: ?
Time Complexity: O(n)
Space Complexity: O(n^2)
Comment on lines +7 to +8

Choose a reason for hiding this comment

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

👀 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, giving space complexity of O(n) as well (ignoring a little bit of fiddliness related to the length of larger numbers being longer strings).

However, when building up the string representation, performing repeated string concatenation ends up recopying the intermediate strings with each new append. Now there's only ever the previous string and the new string in memory simultaneously, so the space complexity remains O(n), however, the time complexity actually becomes O(n^2). So this implementation has time complexity O(n^2) and space complexity O(n).

"""
pass
if nums == 0:
raise ValueError
Comment on lines +10 to +11

Choose a reason for hiding this comment

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

We should raise this error for any value below the valid starting point of the sequence:

    if nums <= 0:
        raise ValueError


if nums == 1:
return "1"

if nums == 2:
return "1 1"

seq = ""
start_list = [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.

for i in range(3,nums+1):
start_list.append( start_list[start_list[i - 1]] + start_list[i - start_list[i - 1]])

for num in start_list[1:]:
seq += str(num) + " "
Comment on lines +24 to +25

Choose a reason for hiding this comment

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

👀 This repeated concatenation is what causes the O(n^2) time complexity.

Rather than repeated string concatenations, it's preferred to build up a list of values, and then join them all at the end. You already have a list of the numerical values, so we need only transform it into a list of strings, then join them. One way to accomplish this would be:

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

return seq.strip()