From fd3fad97568872ce2d5a9df7f3e386c4c5cd1f35 Mon Sep 17 00:00:00 2001 From: Sumitra <58893954+sumitrac@users.noreply.github.com> Date: Wed, 19 Jan 2022 12:50:39 -0800 Subject: [PATCH 1/2] max_subarray --- .vscode/settings.json | 17 +++++++++++++++++ lib/max_subarray.py | 24 +++++++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..dfa5f23 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,17 @@ +{ + "python.testing.unittestArgs": [ + "-v", + "-s", + "./tests", + "-p", + "*test.py" + ], + "python.testing.pytestEnabled": true, + "python.testing.unittestEnabled": false, + "python.testing.pytestArgs": [ + "tests" + ], + "cSpell.words": [ + "curr" + ] +} \ No newline at end of file diff --git a/lib/max_subarray.py b/lib/max_subarray.py index 4e892e0..4070727 100644 --- a/lib/max_subarray.py +++ b/lib/max_subarray.py @@ -4,9 +4,31 @@ def max_sub_array(nums): Returns 0 if nums is None or an empty list. Time Complexity: ? Space Complexity: ? + Does this have to be contiguous? """ if nums == None: return 0 if len(nums) == 0: return 0 - pass +# # Arrange + # input = [50, 3, -50, 50, 3] + # output = 56 + # input1 = [-2, -1] + # output = [-1] + + max_so_far = 0 + max_ending = 0 + + for i in nums: + max_ending += i + + if max_ending > max_so_far: + max_so_far = max_ending + + # when max sum is less than 0/negative num + if max_so_far <= 0: + return max(nums) + return max_so_far + +# Run Time: 0(n) +# Space: O(1) From eac53beab7be9f7690136b9f559b5a71e1b42479 Mon Sep 17 00:00:00 2001 From: Sumitra <58893954+sumitrac@users.noreply.github.com> Date: Wed, 19 Jan 2022 14:28:50 -0800 Subject: [PATCH 2/2] newman_conway --- lib/newman_conway.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 70a3353..df8c15c 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -4,7 +4,21 @@ # 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: 0(n) """ - pass + # count of num and the num itself + # input = 13 + # output = 1113 -> 3113 -> 132113-> 1113122113 -> 311311222113 -> 13211321322113 -> + if num == 0: + raise ValueError + if num == 1: + return "1" + + sequence = [0, 1, 1] + for val in range(3, num+1): + nc = sequence[sequence[val - 1]] + sequence[val - sequence[val - 1]] + sequence.append(nc) + + return ' '.join([str(s) for s in sequence[1:]]) +