diff --git a/lib/max_subarray.py b/lib/max_subarray.py index 4e892e0..75d01fe 100644 --- a/lib/max_subarray.py +++ b/lib/max_subarray.py @@ -5,8 +5,20 @@ def max_sub_array(nums): Time Complexity: ? Space Complexity: ? """ - if nums == None: + if nums == None or len(nums) == 0: return 0 - if len(nums) == 0: - return 0 - pass + + if len(nums) == 1: + return nums[0] + + max_sum = nums[0] + nums[1] + max_sum_here = 0 + + for i in range(len(nums)): + max_sum_here = max_sum_here + nums[i] + if max_sum < max_sum_here: + max_sum = max_sum_here + if max_sum_here < 0: + max_sum_here = 0 + + return max_sum \ No newline at end of file diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 70a3353..a9a0f64 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -4,7 +4,25 @@ # 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 < 1: + raise ValueError("Input must be an integer larger than 0") + + if num == 1: + return "1" + + seq = [1, 1] + i = 3 + + while i <= num: + n = seq[seq[i - 2] - 1] + seq[i - seq[i - 2] - 1] + seq.append(n) + i += 1 + + printed_seq = "" + for n in seq: + printed_seq += str(n) + " " + + return printed_seq[0:-1] \ No newline at end of file