From 88c3fc441fc2d7e9ba45fcae3ba4d91f54455acf Mon Sep 17 00:00:00 2001 From: Laurel Date: Mon, 24 Jan 2022 11:38:09 -0700 Subject: [PATCH 1/2] chchchchanges --- lib/max_subarray.py | 22 ++++++++++++++++------ lib/newman_conway.py | 23 +++++++++++++++++++---- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/lib/max_subarray.py b/lib/max_subarray.py index 4e892e0..b172340 100644 --- a/lib/max_subarray.py +++ b/lib/max_subarray.py @@ -2,11 +2,21 @@ 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: + if not nums: return 0 - if len(nums) == 0: - return 0 - pass + + _max = 0 + end = nums[0] + + for num in nums: + _max = max(num, _max + num) + end = max(end, _max) + return end + + + + + diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 70a3353..407c34e 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -2,9 +2,24 @@ # Time complexity: ? # Space Complexity: ? -def newman_conway(num): +def newman_conway(num, memo=None): """ 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 + + if num == 1: + return '1' + + if memo is None: + memo = [0, 1, 1] + + while num >= len(memo): + for i in range(3, num +1): + memo.append(memo[memo[i -1]] + memo[i - memo[i - 1]]) + return ' '.join([str(s) for s in memo[1:]]) + + + From c164bb594d20c51f7991b24f8be9ac9cc1b7011d Mon Sep 17 00:00:00 2001 From: Laurel Date: Mon, 31 Jan 2022 21:10:18 -0700 Subject: [PATCH 2/2] submitted --- lib/max_subarray.py | 8 ++++---- lib/newman_conway.py | 7 ++----- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/lib/max_subarray.py b/lib/max_subarray.py index b172340..bb44818 100644 --- a/lib/max_subarray.py +++ b/lib/max_subarray.py @@ -2,18 +2,18 @@ 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: O(n)? + Time Complexity: O(n) Space Complexity: O(1) """ if not nums: return 0 - _max = 0 + max_ = 0 end = nums[0] for num in nums: - _max = max(num, _max + num) - end = max(end, _max) + max_ = max(num, max_ + num) + end = max(end, max_) return end diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 407c34e..8208998 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -1,11 +1,8 @@ - -# Time complexity: ? -# Space Complexity: ? def newman_conway(num, memo=None): """ Returns a list of the Newman Conway numbers for the given value. - Time Complexity: O(n)? - Space Complexity: O(n)? + Time Complexity: O(n) + Space Complexity: O(n) """ if num == 0: raise ValueError