diff --git a/lib/max_subarray.py b/lib/max_subarray.py index 4e892e0..42324b3 100644 --- a/lib/max_subarray.py +++ b/lib/max_subarray.py @@ -2,11 +2,26 @@ 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 + + max_so_far = 0 + max_ending_here = 0 + + for val in nums: + max_ending_here += val + + if max_ending_here < 0: + max_ending_here = 0 + max_so_far = max(max_so_far, max_ending_here) + + if max_so_far == 0: + return max(nums) + + return max_so_far + diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 70a3353..e663f4e 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -4,7 +4,18 @@ # 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 + if num == 0: + raise ValueError + + if num == 1: + return '1' + + sequence = [0, 1, 1] + for val in range(3, num+1): + nc = sequence[sequence[val - 1]] + sequence[val - sequence[val - 1]] + sequence.append(nc) + + return ' '.join([str(s) for s in sequence[1:]])