Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Angela #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
35 changes: 31 additions & 4 deletions lib/max_subarray.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n^2) where n is size of nums
# Space Complexity: O(n)
def max_sub_array(nums)
return 0 if nums == nil

raise NotImplementedError, "Method not implemented yet!"
return nil if nums.length == 0

size = nums.length
max_so_far = find_min(nums)

i = 0
while i < size
max_ending_here = 0
j = i
while j < size
max_ending_here = max_ending_here + nums[j]
if max_so_far < max_ending_here
max_so_far = max_ending_here
end
j += 1
end
i += 1
end
return max_so_far
Comment on lines +12 to +24

Choose a reason for hiding this comment

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

Think about how you could use a dynamic programming approach (saving max subarrays) to avoid having this inner loop.

end

def find_min(nums)
min = 0
nums.each do |num|
if num < min
min = num
end
end
return min
end
25 changes: 22 additions & 3 deletions lib/newman_conway.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@


# Time complexity: ?
# Space Complexity: ?
# Time complexity: O(n)
# Space Complexity: O(n)
def newman_conway(num)
raise NotImplementedError, "newman_conway isn't implemented"
raise ArgumentError, "n must be >= 0" if num == 0

if num == 1
return "1"
end

output_string = "1 1"
i = 3
while i > 2 && i <= num
new_num = newman_conway_helper(i)
output_string += " " + new_num.to_s
i += 1
end

return output_string
end

def newman_conway_helper(num)
return 1 if num == 1 || num == 2
return newman_conway_helper( newman_conway_helper(num - 1)) + newman_conway_helper(num - newman_conway_helper(num - 1))
end
Comment on lines +23 to 26

Choose a reason for hiding this comment

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

This is exactly the same problem as fibonacci. Please re-read the textbook curriculum on this. Your solution is O(3n), which is much worse than the linear time solution.

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