diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index 5204edb..e50cee1 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -1,8 +1,25 @@ -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: ? o(n) +# Space Complexity: ? o(n)? def max_sub_array(nums) return 0 if nums == nil - raise NotImplementedError, "Method not implemented yet!" + current_sum = nums.first + max_number = nums.first + + #use the spread here to loop through + (1...nums.length).each do |i| + current_sum += nums[i] + #check if current sum is larger than max, then max is the current num + if current_sum > max_number + max_number = current_sum + end + #if current sum is less than zero, then current zum equals 0 + if current_sum < 0 + current_sum = 0 + end +end + +return max_number + end diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 4c985cd..4235d92 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -1,7 +1,24 @@ -# Time complexity: ? -# Space Complexity: ? +# Time complexity: ? o(n) +# Space Complexity: ? o(n) def newman_conway(num) - raise NotImplementedError, "newman_conway isn't implemented" -end \ No newline at end of file + raise ArgumentError if num <= 0 + return 1 if num == 1 + + newman = [0, 1, 1] + goal = "1 1" + i = 3 + + #P(n) = P(P(n - 1)) + P(n - P(n - 1)) + #https://www.geeksforgeeks.org/newman-conway-sequence/ + until i >= num + 1 + current_newman = newman[newman[i - 1]] + newman[i - (newman[i - 1])] + + newman << current_newman + goal += " #{current_newman}" + i += 1 + end + + return goal +end