diff --git a/README.md b/README.md index 3240bcc..41156be 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Please complete this by **Monday October 11th** ## Wave 1 Newman-Conway Sequence -[Newman-Conway sequence](https://archive.lib.msu.edu/crcmath/math/math/n/n078.htm) is the one which generates the following integer sequence. 1 1 2 2 3 4 4 4 5 6 7 7….. and follows below recursive formula. +[Newman-Conway sequence](https://archive.lib.msu.edu/crcmath/math/math/n/n078.htm) is the one which generates the following integer sequence. 1 1 2 2 3 4 4 4 5 6 7 7….. and follows the below recursive formula. ``` P(0) = 1 diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index 5204edb..d5b0cc6 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -1,8 +1,23 @@ -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n) where n is the length of the input array +# Space Complexity: O(1) def max_sub_array(nums) - return 0 if nums == nil + return 0 if nums == nil - raise NotImplementedError, "Method not implemented yet!" + max_so_far = 0 + max = nums[0] + i = 0 + + while i < nums.length + max_so_far += nums[i] + + if max_so_far > max + max = max_so_far + end + if max_so_far < 0 + max_so_far = 0 + end + i += 1 + end + return max end diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 4c985cd..52537bb 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 the input +# Space Complexity: O(n) where n is the size of the input (space needed for storing each char) def newman_conway(num) - raise NotImplementedError, "newman_conway isn't implemented" + raise ArgumentError, "Number must be larger than 0" if num < 1 + + result = [] + num.times do |i| + if i < 2 + result << 1 + else + last_char = result[i - 1] + result[i] = result[last_char - 1] + result[i - last_char] + end + end + return result.join(" ") end \ No newline at end of file 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]