diff --git a/lib/max_subarray.py b/lib/max_subarray.py index 4e892e0..0b43d84 100644 --- a/lib/max_subarray.py +++ b/lib/max_subarray.py @@ -1,12 +1,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: constant? """ if nums == None: return 0 if len(nums) == 0: return 0 - pass + + current_max_array = nums[0] + max_sub_array = nums[0] + + for i in range(1, len(nums)): + max_sub_array = max(max_sub_array + nums[i], nums[i]) + current_max_array = max(current_max_array, max_sub_array) + + return current_max_array diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 70a3353..2dd3d55 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -1,10 +1,18 @@ - - -# 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)? """ - pass + if num < 1: + raise ValueError("Invalid newman conway number") + if num == 1: + return "1" + + n = [0, 1, 1] + + count = 3 + while count <= num: + n.append(n[n[count - 1]] + n[count - n[count - 1]]) + count += 1 + + return ' '.join([str(item) for item in n[1:]])