From 2ccfa319dbb301383008aa8f29cc2960c128d2c7 Mon Sep 17 00:00:00 2001 From: dynamicdev3000 Date: Mon, 6 Jun 2022 12:31:28 -0700 Subject: [PATCH] passing tests --- lib/max_subarray.py | 14 +++++++++++--- lib/newman_conway.py | 35 ++++++++++++++++++++++++++++++----- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/lib/max_subarray.py b/lib/max_subarray.py index 4e892e0..0d11b2a 100644 --- a/lib/max_subarray.py +++ b/lib/max_subarray.py @@ -2,11 +2,19 @@ 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(n) """ if nums == None: return 0 if len(nums) == 0: return 0 - pass + + ans = nums[0] + subarr_sum = nums[0] + + for i in range(1, len(nums)): + subarr_sum = max(nums[i], nums[i] + subarr_sum) + ans = max(ans, subarr_sum) + + return ans diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 70a3353..23f3304 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -1,10 +1,35 @@ - - # Time complexity: ? # 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: O(n) +Newman-Conway Sequence is the one which generates the following integer sequence. +1 1 2 2 3 4 4 4 5 6 7 7… +In mathematical terms, the sequence P(n) of Newman-Conway numbers is defined by recurrence relation + +P(n) = P(P(n - 1)) + P(n - P(n - 1)) +with seed values P(1) = 1 and P(2) = 1 +Given a number n, print n-th number in Newman-Conway Sequence. """ - pass + if num == 0: + raise ValueError + elif num == 1: + return "1" + elif num == 2: + return "1 1" + + memo_list = [0,1,1] + + for n in range(3, num +1): + new_num = memo_list[memo_list[n-1]] + memo_list[n - memo_list[n-1]] + memo_list.append(new_num) + memo_list.pop(0) + memo_list = " ".join([str(x) for x in memo_list]) + return memo_list + + + + + +