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..aa44177 100644 --- a/lib/max_subarray.py +++ b/lib/max_subarray.py @@ -2,11 +2,17 @@ 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: return 0 if len(nums) == 0: return 0 - pass + maxSub = nums[0] + currentSum = nums[0] + for index in range(1, len(nums)): + currentSum = max(nums[index], currentSum + nums[index]) + maxSub = max(maxSub, currentSum) + return maxSub diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 70a3353..0001017 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -1,10 +1,39 @@ - - -# 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) """ - pass + # P(1) = 1 + # P(2) = 1 + # for all n > 2 + # P(n) = P(P(n - 1)) + P(n - P(n - 1)) + # use memoization + if num == 0: + raise ValueError() + if num == 1: + return "1" + if num == 2: + return "1 1" + new_seq = [0] * (num + 1) + new_seq[0] = 0 + new_seq[1] = 1 + result = [] + if num > 2: + new_seq[2] = 1 + + i = 3 + # first 3 items + while i<=num: + new_seq[i] + # P(n) = P(P(n - 1)) + P(n - P(n - 1)) + new_seq[i] = new_seq[new_seq[i-1]] + new_seq[i- new_seq[i-1]] + i +=1 + + i = 1 + result = [] + while (i <= num) : + # Display the sequence element + result.append(new_seq[i]) + i += 1 + resultOutput = [str(item) for item in result] + return " ".join(resultOutput) \ No newline at end of file