From a296cbe568fb3a4ab5343f00a22cb9301976f6f9 Mon Sep 17 00:00:00 2001 From: Asli Athman Date: Sun, 17 Jul 2022 16:24:09 -0400 Subject: [PATCH 1/3] Adds max_sub_array logic, passes all tests --- lib/max_subarray.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/max_subarray.py b/lib/max_subarray.py index 4e892e0..b0f88ea 100644 --- a/lib/max_subarray.py +++ b/lib/max_subarray.py @@ -9,4 +9,16 @@ def max_sub_array(nums): return 0 if len(nums) == 0: return 0 - pass + + max = - 10000000000000000000000000000000000000000000 + curr_max = 0 + + for num in nums: + curr_max += num + if curr_max > max: + max = curr_max + if curr_max < 0: + curr_max = 0 + return max + + From 6de9cfc494e28f1cf7c01e1427ed43155dc1d3f0 Mon Sep 17 00:00:00 2001 From: Asli Athman Date: Sun, 17 Jul 2022 16:25:24 -0400 Subject: [PATCH 2/3] Adds time/space complexity info for max_sub_array --- lib/max_subarray.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/max_subarray.py b/lib/max_subarray.py index b0f88ea..af78fce 100644 --- a/lib/max_subarray.py +++ b/lib/max_subarray.py @@ -2,8 +2,8 @@ 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 From 28548fe9461a14d94b5e102a2d82c9f3a92b1ef5 Mon Sep 17 00:00:00 2001 From: Asli Athman Date: Thu, 21 Jul 2022 18:11:54 -0400 Subject: [PATCH 3/3] Answers newman_conway problem, passes all tests --- lib/newman_conway.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 70a3353..b6b6700 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -1,10 +1,26 @@ -# 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 + outputs = {} + + res = [] + if num <= 0: + raise ValueError + + for n in range(1, num + 1): + if n == 1 or n ==2: + outputs[n] = 1 + res.append("1") + continue + + val = outputs[outputs[n-1]] + outputs[n - outputs[n - 1]] + outputs[n] = val + res.append(str(val)) + + return " ".join(res)