From 5ed62cd9d3d3daae20611a9f4be5aba6f20db37f Mon Sep 17 00:00:00 2001 From: Jetmire Bajrami Date: Sun, 4 Oct 2020 22:22:32 -0700 Subject: [PATCH 1/2] added newman_conway method --- lib/newman_conway.rb | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 4c985cd..a17be70 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -1,7 +1,21 @@ -# Time complexity: ? -# Space Complexity: ? +# Time complexity: 0(n)? +# Space Complexity: 0(n)? def newman_conway(num) - raise NotImplementedError, "newman_conway isn't implemented" + raise ArgumentError if num == 0 + array = [] + + array.push(1) + return array.join(" ") if num == 1 + + array.push(1) + return array.join(" ") if num == 2 + + i = 2 + while i < num + array[i] = array[array[i-1]-1] + array[i - array[i-1]] + i += 1 + end + return array.join(" ") end \ No newline at end of file From 2676021d8ffa07506dbccaf9e57232e674835bff Mon Sep 17 00:00:00 2001 From: Jetmire Bajrami Date: Sun, 4 Oct 2020 22:45:16 -0700 Subject: [PATCH 2/2] added max_sub_array and apassed the tests --- lib/max_subarray.rb | 24 +++++++++++++++++++++--- test/max_sub_array_test.rb | 2 +- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index 5204edb..812c15d 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -1,8 +1,26 @@ -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: 0(n)? +# Space Complexity: 0(1)? def max_sub_array(nums) return 0 if nums == nil + return nil if nums == [] + + local_max = 0 + global_max = 0 + + i = 0 + while i < nums.length + local_max = local_max + nums[i] + local_max = local_max < 0 ? 0 : local_max + if local_max > global_max + global_max = local_max + end + i += 1 + end + + if global_max == 0 + global_max = nums.max + end - raise NotImplementedError, "Method not implemented yet!" + return global_max end 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]