From d893a3c78c37f58688714424a50c7122c6afb2cc Mon Sep 17 00:00:00 2001 From: Sharon Cheung Date: Sat, 3 Oct 2020 18:27:05 -0700 Subject: [PATCH 1/3] newman_conway tests passed --- lib/newman_conway.rb | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 4c985cd..3b6a3dc 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -1,7 +1,24 @@ - # Time complexity: ? # Space Complexity: ? def newman_conway(num) - raise NotImplementedError, "newman_conway isn't implemented" + # Recursive Function to find the n-th element + 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 + return array.join(' ') end \ No newline at end of file From 2ca58c4cbe16c5ca3a16580cf930e6a101131b42 Mon Sep 17 00:00:00 2001 From: Sharon Cheung Date: Sat, 3 Oct 2020 19:20:38 -0700 Subject: [PATCH 2/3] max_subarray tests passed --- lib/max_subarray.rb | 13 +++++++++++-- test/max_sub_array_test.rb | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index 5204edb..9e31823 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -3,6 +3,15 @@ # Space Complexity: ? def max_sub_array(nums) return 0 if nums == nil - - raise NotImplementedError, "Method not implemented yet!" + max_current = max_sum = nums[0] + + i = 1 + while i <= (nums.length - 1) + max_current = [nums[i], (max_current + nums[i])].max + if max_current > max_sum + max_sum = max_current + end + i += 1 + end + return max_sum 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] From 2fcefe366eb122b342ee8397f236991224755c31 Mon Sep 17 00:00:00 2001 From: Sharon Cheung Date: Wed, 7 Oct 2020 21:43:06 -0700 Subject: [PATCH 3/3] added 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 9e31823..88ee056 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -1,6 +1,6 @@ -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n) +# Space Complexity: O(1) def max_sub_array(nums) return 0 if nums == nil max_current = max_sum = nums[0] diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 3b6a3dc..14bbeb4 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -1,6 +1,6 @@ -# Time complexity: ? -# Space Complexity: ? +# Time complexity: O(n) +# Space Complexity: O(n) def newman_conway(num) # Recursive Function to find the n-th element raise ArgumentError if num < 1