From 81a0d62f284acbee777cc3c53db02cd265779fc3 Mon Sep 17 00:00:00 2001 From: Sara Shah Baig Date: Sun, 29 Mar 2020 13:32:54 -0700 Subject: [PATCH 1/3] completed and passing the test --- lib/max_subarray.rb | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index 5204edb..92a2bfa 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -1,8 +1,22 @@ -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: o(n) +# Space Complexity: o(1) def max_sub_array(nums) + # raise NotImplementedError, "Method not implemented yet!" return 0 if nums == nil - - raise NotImplementedError, "Method not implemented yet!" + return nil if nums == [] + array_max = 0 + current_max = 0 + + nums.each do |num| + array_max = array_max + num + array_max = array_max < 0 ? 0 : array_max + if array_max > current_max + current_max = array_max + end + end + if current_max == 0 + current_max = nums.max + end + return current_max end From 7002290dbe1c77c99ac8904a7c086f48ea0b057f Mon Sep 17 00:00:00 2001 From: Sara Shah Baig Date: Sun, 29 Mar 2020 13:33:09 -0700 Subject: [PATCH 2/3] completed --- lib/newman_conway.rb | 17 ++++++++++++++--- test/max_sub_array_test.rb | 2 +- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 4c985cd..801764a 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -1,7 +1,18 @@ -# Time complexity: ? -# Space Complexity: ? +# Time complexity: o(n) +# Space Complexity: o(n) def newman_conway(num) - raise NotImplementedError, "newman_conway isn't implemented" + # raise NotImplementedError, "newman_conway isn't implemented" + return "1" if num == 1 + array = [1, 1] + i = 2 + + until i == num + previous = array[i - 1] + current = array[previous - 1] + array [i - previous] + array << current + i += 1 + end + return array.join("") 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 f85f6e3f335391561c1edc48fa101ce5dcb8ad26 Mon Sep 17 00:00:00 2001 From: Sara Shah Baig Date: Sun, 29 Mar 2020 13:39:29 -0700 Subject: [PATCH 3/3] edited newman to pass all the test --- lib/newman_conway.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 801764a..1fc293d 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -4,6 +4,7 @@ # Space Complexity: o(n) def newman_conway(num) # raise NotImplementedError, "newman_conway isn't implemented" + raise ArgumentError if num < 1 return "1" if num == 1 array = [1, 1] i = 2 @@ -14,5 +15,5 @@ def newman_conway(num) array << current i += 1 end - return array.join("") + return array.join(" ") end \ No newline at end of file