From 1964dfc92fa3069729760c08a1d9ea9e4b4477cd Mon Sep 17 00:00:00 2001 From: Alice Boone Date: Thu, 6 May 2021 17:00:31 -0700 Subject: [PATCH 1/3] max_subarray --- lib/max_subarray.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index 5204edb..c089e4a 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -4,5 +4,14 @@ def max_sub_array(nums) return 0 if nums == nil - raise NotImplementedError, "Method not implemented yet!" + max = nums[0] + max_sum = nums[0] + i = 1 + + while i <= (nums.length - 1) + max = [nums[i], (max + nums[i])].max + max_sum = max if max > max_sum + i += 1 + end + return max_sum end From 821c31b8bd42384d37a7d6b771ff1155efbe0f67 Mon Sep 17 00:00:00 2001 From: Alice Boone Date: Thu, 6 May 2021 17:01:02 -0700 Subject: [PATCH 2/3] newman_conway --- lib/newman_conway.rb | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 4c985cd..f3cd322 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -3,5 +3,23 @@ # Time complexity: ? # Space Complexity: ? def newman_conway(num) - raise NotImplementedError, "newman_conway isn't implemented" + raise ArgumentError if num < 1 + return "1" if num == 1 + return "1 1" if num == 2 + + array = Array.new(num, 0) + i = 1 + + while i <= num + if i == 1 || i == 2 + array[i] = 1 + else + array[i] = array[array[i - 1]] + array[i - array[i-1]] + end + i += 1 + end + + array.shift # remove first element + + return array.join(' ') end \ No newline at end of file From 4d4691c29335f1c628fbd93275d62dc55d7e2fa4 Mon Sep 17 00:00:00 2001 From: Alice Boone Date: Thu, 6 May 2021 17:04:22 -0700 Subject: [PATCH 3/3] Add time & space complexity --- lib/max_subarray.rb | 6 +++--- lib/newman_conway.rb | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index c089e4a..878fde1 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -1,9 +1,9 @@ -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n) +# Space Complexity: O(1) def max_sub_array(nums) return 0 if nums == nil - + max = nums[0] max_sum = nums[0] i = 1 diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index f3cd322..a1dec3b 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -1,7 +1,7 @@ -# Time complexity: ? -# Space Complexity: ? +# Time complexity: O(n) +# Space Complexity: O(n) def newman_conway(num) raise ArgumentError if num < 1 return "1" if num == 1