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
14 changes: 11 additions & 3 deletions lib/max_subarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@
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(n)

Choose a reason for hiding this comment

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

👀 Your implementation looks good, however since ans and subarr_sum only hold integer values, space complexity would be O(1) here

"""
if nums == None:
return 0
if len(nums) == 0:
return 0
pass

ans = nums[0]
subarr_sum = nums[0]

for i in range(1, len(nums)):
subarr_sum = max(nums[i], nums[i] + subarr_sum)
ans = max(ans, subarr_sum)

return ans
35 changes: 30 additions & 5 deletions lib/newman_conway.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@


# Time complexity: ?
# Space Complexity: ?
def newman_conway(num):
""" Returns a list of the Newman Conway numbers for the given value.
Time Complexity: ?
Space Complexity: ?
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.

✨ Very nice!

Newman-Conway Sequence is the one which generates the following integer sequence.
1 1 2 2 3 4 4 4 5 6 7 7…
In mathematical terms, the sequence P(n) of Newman-Conway numbers is defined by recurrence relation

P(n) = P(P(n - 1)) + P(n - P(n - 1))
with seed values P(1) = 1 and P(2) = 1
Given a number n, print n-th number in Newman-Conway Sequence.
"""
pass
if num == 0:
raise ValueError
elif num == 1:
return "1"
elif num == 2:
return "1 1"

memo_list = [0,1,1]

for n in range(3, num +1):
new_num = memo_list[memo_list[n-1]] + memo_list[n - memo_list[n-1]]
memo_list.append(new_num)
memo_list.pop(0)
memo_list = " ".join([str(x) for x in memo_list])
return memo_list