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

Elle #6

Open
wants to merge 4 commits 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
39 changes: 35 additions & 4 deletions lib/max_subarray.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,39 @@

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n) where n is the length of the array
# Space Complexity: O(1)

def max_sub_array(nums)

Choose a reason for hiding this comment

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

👍 Nicely done!

return 0 if nums == nil
return nil if nums == []

max_so_far = nums[0]
sum_ending_here = nums[0]

# -----------------------------------------------------
# SOLUTION 1
nums[1..-1].each do |num|

sum_ending_here = [sum_ending_here + num, num].max

if max_so_far < sum_ending_here
max_so_far = sum_ending_here
end
end

# -----------------------------------------------------

# SOLUTION 2

# nums[1..-1].each do |num|

# sum_ending_here = sum_ending_here + num

# if max_so_far < sum_ending_here
# max_so_far = sum_ending_here
# elsif sum_ending_here < 0
# sum_ending_here = 0
# end
# end
# -----------------------------------------------------

raise NotImplementedError, "Method not implemented yet!"
return max_so_far
end
28 changes: 24 additions & 4 deletions lib/newman_conway.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
# Time complexity: O(n)
# Space Complexity: O(n)


# Time complexity: ?
# Space Complexity: ?
def newman_conway(num)
raise NotImplementedError, "newman_conway isn't implemented"
solution = [1]
raise ArgumentError, "Number must be 1 or greater" if num <1
p(num, solution)

string_solution = ""
solution.each do |char|
string_solution << "#{char.to_s} "
end

return string_solution[0...-1]
end

def p(n, s)

Choose a reason for hiding this comment

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

Just noting the variable names are not meaningful here.

return s[n-1] if s[n-1]

if (n == 2) && !s[2-1]
s[2-1] = 1

Choose a reason for hiding this comment

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

Why not just do s[1] = 1

else
s[n-1] = p(p(n - 1, s), s) + p(n - p(n - 1, s), s)

Choose a reason for hiding this comment

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

Nice both recursive and efficient solution.

end

return s[n-1]
end
26 changes: 25 additions & 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 Expand Up @@ -67,4 +67,28 @@
expect(answer).must_equal 50
end

it "will work for [50, -51, 50]" do
# This test (which I added) is failing

# Arrange
input = [50, -51, 50]

# Act
answer = max_sub_array(input)

# Assert
expect(answer).must_equal 50
end

it "will work for [-2,1,-3,4,-1,2,1,-5,4,12]" do
# Arrange
input = [4,-2,1,-3,4,-1,2,1,-5,4,12]

# Act
answer = max_sub_array(input)

# Assert
expect(answer).must_equal 17
end

end