Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions lib/max_subarray.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n) because we have do our if/else operations on every element in the array
# Space Complexity: O(1) because we are re-using the same variable
def max_sub_array(nums)
Comment on lines +2 to 4

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

return 0 if nums == nil

raise NotImplementedError, "Method not implemented yet!"
return nil if nums.empty?
return nums[0] if nums.length == 1

max_so_far = max_ending_here = nums[0]

nums[1..-1].each do |number|

if max_ending_here + number < number
max_ending_here = number
else
max_ending_here += number
end

if max_so_far < max_ending_here
max_so_far = max_ending_here
end
end

return max_so_far

end
32 changes: 29 additions & 3 deletions lib/newman_conway.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@


# Time complexity: ?
# Space Complexity: ?
# Time complexity: O(n) because we need to perform our "what is the p?" steps n times
# Space Complexity: also O(n) because we are adding an element to the sequence array for every n
def newman_conway(num)
Comment on lines +3 to 5

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really nice example of recursion and dynamic programming together. WEll done.

raise NotImplementedError, "newman_conway isn't implemented"
raise ArgumentError, "Argument must be an integer greater than zero." if num <= 0

if num == 1
return "1"
elsif num == 2
return "1 1"
else
p_helper(num, [1, 1])
end
end

def p_helper(num, sequence)

# BASE CASE: stop when count of numbers in sequence array is the same as `num`
return sequence.join(" ") if num == sequence.length

# REGRESSIVE CASE: add p for next integer in sequence
# forumula: p(num) = p(p(num - 1)) + p(num - p(num - 1))
next_integer = sequence.length + 1
# if num is 4 previous_p is at index 2 (value: 2)
previous_p = sequence[next_integer - 2]
# now we need to look up p of the previous_p value
p_previous_p = sequence[previous_p - 1]
# find p of number minus previous number's p, then add it to p_previous_p
p = p_previous_p + sequence[(next_integer - previous_p) - 1]
sequence << p
return p_helper(num, sequence)
end
2 changes: 1 addition & 1 deletion test/max_sub_array_test.rb
Original file line number Diff line number Diff line change
@@ -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]
Expand Down