diff --git a/lib/max_subarray.py b/lib/max_subarray.py index 4e892e0..ad9aad6 100644 --- a/lib/max_subarray.py +++ b/lib/max_subarray.py @@ -2,11 +2,23 @@ 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 len(nums) == 1: + return nums[0] + + max_so_far = nums[0] + max_ending_here = 0 + + for i in range(len(nums)): + if max_ending_here < 0: + max_ending_here = 0 + max_ending_here += nums[i] + if max_so_far < max_ending_here: + max_so_far = max_ending_here + return max_so_far diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 70a3353..34aff6a 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -1,10 +1,25 @@ -# 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) """ - pass + if num <= 0: + raise ValueError("num cannot be less than 1") + memo = [0] * (num+1) + memo[0] = 0 + memo[1] = 1 + if num > 1: + memo[2] = 1 + + x = 3 + while x <= num: + memo[x] = memo[memo[x - 1]] + memo[x - memo[x - 1]] + x += 1 + + memo.pop(0) + memo = [str(int) for int in memo] + return " ".join(memo)