From 5007540e5cc89e0869360c5014d2daf2a7081e68 Mon Sep 17 00:00:00 2001 From: Brianna Kemp Date: Mon, 30 Mar 2020 18:59:58 -0700 Subject: [PATCH 1/2] Finished waves 1 and 2 --- lib/max_subarray.rb | 22 ++++++++++++++++++++-- lib/newman_conway.rb | 26 +++++++++++++++++++++----- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index 5204edb..129e9de 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -2,7 +2,25 @@ # Time Complexity: ? # Space Complexity: ? def max_sub_array(nums) - return 0 if nums == nil + return nil if (nums == nil || nums.length === 0) + + max = nums[0] + + nums.each do |num| + temp = 0 + temp += num - raise NotImplementedError, "Method not implemented yet!" + if temp > max + max = temp + end + + if temp < 0 + temp = 0 + end + end + + return max end + + +p max_sub_array([-2, -3, 4, -1, -2, 1, 5, -3]) \ No newline at end of file diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 4c985cd..db79e8a 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -1,7 +1,23 @@ +# Time complexity: O(n) +# Space Complexity: O(n) +def newman_conway(num) + return nc_helper(num) +end +def nc_helper(num, count = 2, memo = [0, 1, 1], result = "1 1") + raise ArgumentError if num <= 0 + return "1" if num == 1 + return "1 1" if num == 2 + + if count == num + return result + end + + value = memo[memo[count]] + memo[count + 1 - memo[count]] + memo << value + result << " #{value}" + + return nc_helper(num, count + 1, memo, result) +end -# Time complexity: ? -# Space Complexity: ? -def newman_conway(num) - raise NotImplementedError, "newman_conway isn't implemented" -end \ No newline at end of file +# p newman_conway(12) \ No newline at end of file From 0cab1bbca1cbc4064433a0785a02f062070b7528 Mon Sep 17 00:00:00 2001 From: Brianna Kemp Date: Mon, 30 Mar 2020 19:09:02 -0700 Subject: [PATCH 2/2] Fixed typo --- lib/max_subarray.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index 129e9de..bbb9566 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -5,9 +5,9 @@ def max_sub_array(nums) return nil if (nums == nil || nums.length === 0) max = nums[0] + temp = 0 nums.each do |num| - temp = 0 temp += num if temp > max @@ -23,4 +23,4 @@ def max_sub_array(nums) end -p max_sub_array([-2, -3, 4, -1, -2, 1, 5, -3]) \ No newline at end of file +# p max_sub_array([-2,1,-3,4,-1,2,1,-5,4]) \ No newline at end of file