From 3f9ade590918c1bcdf9e19ccf34075a4f30240ad Mon Sep 17 00:00:00 2001 From: Taylor Tompkins Date: Mon, 12 Jul 2021 15:04:53 -0700 Subject: [PATCH 1/2] all tests passing --- lib/max_subarray.rb | 14 +++++++++++--- lib/newman_conway.rb | 13 ++++++++++++- test/max_sub_array_test.rb | 2 +- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index 5204edb..c924743 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -2,7 +2,15 @@ # Time Complexity: ? # Space Complexity: ? def max_sub_array(nums) - return 0 if nums == nil - - raise NotImplementedError, "Method not implemented yet!" + local_max = 0 + max = nil + i = 0 + until i == nums.length + local_max = [nums[i], nums[i] + local_max].max + if max == nil || max < local_max + max = local_max + end + i += 1 + end + return max end diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 4c985cd..c19c342 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -3,5 +3,16 @@ # Time complexity: ? # Space Complexity: ? def newman_conway(num) - raise NotImplementedError, "newman_conway isn't implemented" + raise ArgumentError.new('num must be greater than 0') if num == 0 + return '1' if num == 1 + array = [] + array[0] = 0 + array[1], array[2] = 1, 1 + i = 3 + while i <= num + array[i] = array[array[i-1]] + array[i - array[i - 1]] + i += 1 + end + array.slice!(0) + return array.join(',').gsub(',', ' ') end \ No newline at end of file diff --git a/test/max_sub_array_test.rb b/test/max_sub_array_test.rb index 3253cdf..e27e1ca 100644 --- a/test/max_sub_array_test.rb +++ b/test/max_sub_array_test.rb @@ -1,6 +1,6 @@ require_relative "test_helper" -xdescribe "max subarray" do +describe "max subarray" do it "will work for [-2,1,-3,4,-1,2,1,-5,4]" do # Arrange input = [-2,1,-3,4,-1,2,1,-5,4] From 208a80d3a855418178027e6156bde137eaf0e313 Mon Sep 17 00:00:00 2001 From: Taylor Tompkins Date: Mon, 12 Jul 2021 15:09:27 -0700 Subject: [PATCH 2/2] forgot time/space complexity --- lib/max_subarray.rb | 4 ++-- lib/newman_conway.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index c924743..527fc4e 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -1,6 +1,6 @@ -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(nlogn) +# Space Complexity: O(n) def max_sub_array(nums) local_max = 0 max = nil diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index c19c342..44a0250 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.new('num must be greater than 0') if num == 0 return '1' if num == 1