From 5937cd85a8788325a7e12565515664eebb0cad9c Mon Sep 17 00:00:00 2001 From: Nandita Gilroy Date: Wed, 19 Jan 2022 17:22:46 -0800 Subject: [PATCH 1/2] adds working logic for newman_conway function --- lib/newman_conway.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 70a3353..2b2c68e 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -7,4 +7,19 @@ def newman_conway(num): Time Complexity: ? Space Complexity: ? """ - 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) From e98e029ea6adafe86df1539f0c8fe56728082c51 Mon Sep 17 00:00:00 2001 From: Nandita Gilroy Date: Wed, 19 Jan 2022 18:17:19 -0800 Subject: [PATCH 2/2] adds logic for max_subarray function and adds time and space complexities --- lib/max_subarray.py | 18 +++++++++++++++--- lib/newman_conway.py | 8 ++++---- 2 files changed, 19 insertions(+), 7 deletions(-) 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 2b2c68e..34aff6a 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -1,11 +1,11 @@ -# 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) """ if num <= 0: raise ValueError("num cannot be less than 1")