diff --git a/lib/max_subarray.py b/lib/max_subarray.py index 4e892e0..af78fce 100644 --- a/lib/max_subarray.py +++ b/lib/max_subarray.py @@ -2,11 +2,23 @@ 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 + + max = - 10000000000000000000000000000000000000000000 + curr_max = 0 + + for num in nums: + curr_max += num + if curr_max > max: + max = curr_max + if curr_max < 0: + curr_max = 0 + return max + + diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 70a3353..b6b6700 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -1,10 +1,26 @@ -# Time complexity: ? -# Space Complexity: ? +# Time complexity: O(n) +# Space Complexity: O(n) 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 + 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 + + val = outputs[outputs[n-1]] + outputs[n - outputs[n - 1]] + outputs[n] = val + res.append(str(val)) + + return " ".join(res)