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..e1a9f24 100644 --- a/lib/max_subarray.py +++ b/lib/max_subarray.py @@ -1,12 +1,25 @@ +from json.encoder import INFINITY + + 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 + + maximum = - INFINITY + current_max = 0 + + for num in nums: + current_max += num + if current_max > maximum: + maximum = current_max + if current_max < 0: + current_max = 0 + return maximum diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 70a3353..20e92be 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -4,7 +4,31 @@ # 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) + + P(1) = 1 + P(2) = 1 + for all n > 2 + P(n) = P(P(n - 1)) + P(n - P(n - 1)) """ - pass + + # make a dict with key as n, and the value as P(n) (aka the output) + + outputs = {} + + res = [] + if num <= 0: + raise ValueError + + for n in range(1, num + 1): + if n == 1 or n == 2: + outputs[n] = 1 + res.append("1") + continue + # P(n -1) -> outputs[ n - 1] + value = outputs[outputs[n - 1]] + outputs[n - outputs[n - 1]] + outputs[n] = value + res.append(str(value)) + + return " ".join(res)