From 883430c7ae5f64261de525129a504b70c54e8d8f Mon Sep 17 00:00:00 2001 From: sjscotton Date: Sat, 5 Oct 2019 18:33:56 -0700 Subject: [PATCH 1/2] Finished Max sub array --- lib/max_subarray.rb | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index 5204edb..eac4b4e 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -1,8 +1,14 @@ -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n) where n is the length of nums, we iterate over the array once, and perform constant time operations each loop +# Space Complexity: O(1) we only use constant sized variables def max_sub_array(nums) - return 0 if nums == nil - - raise NotImplementedError, "Method not implemented yet!" + return 0 if nums == nil + best_total = nil + curr_total = 0 + nums.each do |num| + curr_total += num + best_total = curr_total if !best_total or curr_total > best_total + curr_total = 0 if curr_total < 0 + end + return best_total end From 49d700151969177eee1b19e2768d6fd524d092d3 Mon Sep 17 00:00:00 2001 From: sjscotton Date: Sat, 5 Oct 2019 19:09:16 -0700 Subject: [PATCH 2/2] Added newman conway --- lib/newman_conway.rb | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 4c985cd..345f10d 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -1,7 +1,18 @@ -# Time complexity: ? -# Space Complexity: ? +# Time complexity: O(n) where n is the size of num +# Space Complexity: O(n) becuase we are creating a new array of size n + def newman_conway(num) - raise NotImplementedError, "newman_conway isn't implemented" -end \ No newline at end of file + raise ArgumentError if num <= 0 + sequence = Array.new(num) + num.times do |i| + if i < 2 + sequence[i] = 1 + else + previous = sequence[i - 1] + sequence[i] = sequence[previous - 1] + sequence[i - previous] + end + end + return sequence.join(" ") +end