From 816ca0ff860e3d0b61c58b3eb6ce34838e4b8751 Mon Sep 17 00:00:00 2001 From: mulhoo Date: Fri, 16 Oct 2020 17:14:33 -0700 Subject: [PATCH 1/3] newman_conway complete --- lib/newman_conway.rb | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 4c985cd..21e0e48 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -1,7 +1,20 @@ - - # Time complexity: ? # Space Complexity: ? def newman_conway(num) - raise NotImplementedError, "newman_conway isn't implemented" + raise ArgumentError if num <= 0 + starting_array = [0, 1, 1] + + result = "1" + count = 3 + + while count <= num + starting_array[count] = starting_array[starting_array[count - 1]] + starting_array[count - starting_array[count - 1]] + count += 1 + end + + (2..num).each do |i| + result += " #{starting_array[i]}" + end + + return result end \ No newline at end of file From e849f6d374c0bc180b06c9d75fe2a2d96ff8b9ec Mon Sep 17 00:00:00 2001 From: mulhoo Date: Fri, 16 Oct 2020 17:18:33 -0700 Subject: [PATCH 2/3] added time/space complexity --- lib/newman_conway.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 21e0e48..bf17854 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -1,5 +1,5 @@ -# Time complexity: ? -# Space Complexity: ? +# Time complexity: on +# Space Complexity: on def newman_conway(num) raise ArgumentError if num <= 0 starting_array = [0, 1, 1] From 6fbe83b2b89f449d6376d17799f2a81ee59ad31a Mon Sep 17 00:00:00 2001 From: mulhoo Date: Sat, 17 Oct 2020 16:48:25 -0700 Subject: [PATCH 3/3] all tests pass --- lib/max_subarray.rb | 19 +++++++++++++++---- test/max_sub_array_test.rb | 2 +- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index 5204edb..bf70993 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -1,8 +1,19 @@ - -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: on +# Space Complexity: o1 def max_sub_array(nums) return 0 if nums == nil + return nil if nums == [] - raise NotImplementedError, "Method not implemented yet!" + max_so_far = nums[0] + sum_ending_here = nums[0] + + nums[1..-1].each do |num| + sum_ending_here = [sum_ending_here + num, num].max + + if max_so_far < sum_ending_here + max_so_far = sum_ending_here + end + end + + return max_so_far 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]