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

✨ This works and your time and space complexity does match your solution, but I think you're doing a little extra work here and we can make this solution even more efficient! See comments below ⬇️

"""
if nums == None:
return 0
if len(nums) == 0:
return 0
pass
max_overall = nums[0]
max_so_far = nums[0]
for index, num in enumerate(nums):
so_far_sum = 0
for each in nums[:index + 1]:
so_far_sum += each
max_so_far = max(so_far_sum, num)
Comment on lines +15 to +18

Choose a reason for hiding this comment

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

With your current solution, you calculate what the sum would be for values from index 0 to the current index, and set 'max_so_far' to be either that calculated value or the current value you are iterating over.

We can eliminate the need for that inner loop/calculation by instead just checking if max_so_far + the next value in the list is greater than the next value in the list. If max_so_far + num is greater than num, max_so_far in the next iteration will already hold the so_far_sum value you're currently calculating with a loop.

If max_so_far + num is less than num, that means the so_far_sum shouldn't be a part of the subarray anyways, so there's no need to calculate it in this case either.

How would this change to your code affect your time and space complexity? 🤔

Suggested change
so_far_sum = 0
for each in nums[:index + 1]:
so_far_sum += each
max_so_far = max(so_far_sum, num)
max_so_far = max(max_so_far + num, num)

max_overall = max (max_overall, max_so_far)

return max_overall

26 changes: 20 additions & 6 deletions lib/newman_conway.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@


# Time complexity: ?
# Space Complexity: ?
# Time complexity: O(n)
# Space Complexity: O(n)
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.

"""
pass
if num == 0:
raise ValueError
if num == 1:
return "1"
if num == 2:
return "1 1"
initial_array = [0, 1, 1]
for i in range(3, num + 1):
r = initial_array[initial_array[i-1]]+initial_array[i-initial_array[i-1]]
initial_array.append(r)
# remove the first 0 using list slices and change elements to str
stringified_array = []
for elem in initial_array[1:]:
stringified_array.append(str(elem))
return " ".join(stringified_array)