diff --git a/lib/max_subarray.py b/lib/max_subarray.py index 4e892e0..35df9be 100644 --- a/lib/max_subarray.py +++ b/lib/max_subarray.py @@ -2,11 +2,26 @@ 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) """ if nums == None: return 0 if len(nums) == 0: return 0 - pass + + if max(nums) < 0: + return max(nums) + + left_pointer = 0 + right_pointer = 0 + + for num in nums: + right_pointer += num + + if right_pointer < 0: + right_pointer = 0 + + if left_pointer < right_pointer: + left_pointer = right_pointer + return left_pointer diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 70a3353..48852f9 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -4,7 +4,19 @@ # 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) """ - pass + + if num == 0: + raise ValueError + return None + + if num == 1: + return '1' # appeasing tests with string + + seq = [0, 1, 1] + + for i in range(3, num + 1): + seq.append(seq[seq[i - 1]] + seq[i - seq[i - 1]]) + return (" ".join(map(str, seq[1:])))