diff --git a/lib/max_subarray.py b/lib/max_subarray.py index 4e892e0..d4ecf99 100644 --- a/lib/max_subarray.py +++ b/lib/max_subarray.py @@ -2,11 +2,20 @@ 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 + + max_sum = nums[0] + curr_sum = nums[0] + + for i in range(1, len(nums)): + curr_sum = max(curr_sum + nums[i], nums[i]) + max_sum = max(curr_sum, max_sum) + + return max_sum + diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 70a3353..74b4a3c 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -4,7 +4,17 @@ # 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 < 1: + raise ValueError("Invalid number") + if num == 1: + return "1" + arr = [ 0, 1, 1] + for i in range(3,num+1): + arr.append(arr[arr[i - 1]] + arr[i - arr[i - 1]]) + + arr = [str(item) for item in arr ] + return " ".join(arr[1:]) +