From e32887b8118c5b3a95867d7a8bdf1cf6dfc18a88 Mon Sep 17 00:00:00 2001 From: wangjiajia95 Date: Sat, 12 Mar 2022 16:52:21 -0800 Subject: [PATCH] hw --- .vscode/settings.json | 7 +++++++ lib/max_subarray.py | 16 ++++++++++++++-- lib/newman_conway.py | 26 +++++++++++++++++++++----- 3 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..9b38853 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "python.testing.pytestArgs": [ + "tests" + ], + "python.testing.unittestEnabled": false, + "python.testing.pytestEnabled": true +} \ No newline at end of file diff --git a/lib/max_subarray.py b/lib/max_subarray.py index 4e892e0..77bcd67 100644 --- a/lib/max_subarray.py +++ b/lib/max_subarray.py @@ -1,4 +1,4 @@ - +import math 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. @@ -9,4 +9,16 @@ def max_sub_array(nums): return 0 if len(nums) == 0: return 0 - pass + largest_sum = [0] * len(nums) + largest_sum[0] = nums[0] + + for i in range(1, len(nums)): + if largest_sum[i - 1] <= 0: + largest_sum[i] = nums[i] + + else: + largest_sum[i] = nums[i] + largest_sum[i-1] + + return max(largest_sum) + + diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 70a3353..b1983e5 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 + nc_list = [0] * (num + 1) + if num == 0: + raise ValueError("n must be > 0") + if num == 1: + return "1" + if num == 2: + return "1 1" + if num > 2: + nc_list[1] = 1 + nc_list[2] = 1 + for i in range(3, num + 1): + + nc_list[i] = nc_list[nc_list[i-1]] + nc_list[i - nc_list[i-1]] + + return " ".join(str(v) for v in nc_list[1:]) +print(newman_conway(1)) +