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..24cec20 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), iterates through relative to the size of the list given. + Space Complexity: ? O(1), we will only ever make 2 """ + #guard clauses if nums == None: return 0 if len(nums) == 0: return 0 - pass + + # initialize /memoize: + max_subarray_so_far = nums[0] + max_this_subarray = nums[0] + + for i in range(1, len(nums)): # O(n) + max_this_subarray = max(max_this_subarray + nums[i], nums[i]) + max_subarray_so_far = max(max_this_subarray, max_subarray_so_far) + + return max_subarray_so_far \ No newline at end of file diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 70a3353..91e77b1 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -1,10 +1,30 @@ - - -# Time complexity: ? -# Space Complexity: ? +''' +P(1) = 1 +P(2) = 1 +for all n > 2 +P(n) = P(P(n - 1)) + P(n - P(n - 1)) +''' 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 + # base case guard clauses + if num < 1: + raise ValueError('newman conway number cant be zero nums long') + elif num == 1: + return '1' + + memo = [0,1,1] + count = 3 + + while count <= num: + # P(P(n - 1)) + P (n - P(n - 1)) + memo.append(memo[memo[count-1]] + memo[count - memo[count-1]]) + count += 1 + + NCnums = [] + for num in memo: + NCnums.append(str(num)) + # print(NCnums[1:]) + return " ".join(NCnums[1:]) \ No newline at end of file