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
39 changes: 36 additions & 3 deletions lib/max_subarray.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,41 @@

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(1)
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.

👍 Interesting use of negative infinity.

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

raise NotImplementedError, "Method not implemented yet!"
max_so_far = 0
max_ending_here = 0
positive_flag = false

nums.each do |num|
if num >= 0
positive_flag = true
end

max_so_far = max_so_far + num

if max_so_far < 0
max_so_far = 0
end

if max_so_far > max_ending_here
max_ending_here = max_so_far
end
end

if !positive_flag
largest_num = -Float::INFINITY

nums.each do |num|
if num > largest_num
largest_num = num
end
end

return largest_num
end

return max_ending_here
end
32 changes: 28 additions & 4 deletions lib/newman_conway.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@


# Time complexity: ?
# Space Complexity: ?
# Time complexity: O(n)
# Space Complexity: O(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.

👍

raise NotImplementedError, "newman_conway isn't implemented"
end
if num == 0
raise ArgumentError
end

if num == 1
return num.to_s
end

answer = [0, 1, 1]

(3..num).each do |i|
add = answer[answer[i-1]] + answer[i-answer[i-1]]
answer << add
end

answer_string = ""

answer.each do |number|
if number != 0
answer_string = answer_string + " " + number.to_s
end
end

return answer_string.strip

end
50 changes: 25 additions & 25 deletions test/max_sub_array_test.rb
Original file line number Diff line number Diff line change
@@ -1,70 +1,70 @@
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]

# Act
answer = max_sub_array(input)

# Assert
expect(answer).must_equal 6
end

it "will work with a totally negative array" do
# Arrange
input = [-3, -4, -5, -6, -7]

# Act
answer = max_sub_array(input)

# Assert
expect(answer).must_equal(-3)
end

it "will work with a totally negative array with the largest element at the rear" do
# Arrange
input = [ -4, -5, -6, -7, -3]

# Act
answer = max_sub_array(input)

# Assert
expect(answer).must_equal(-3)
end

it "will work with a 1-element array" do
# Arrange
input = [3]

# Act
answer = max_sub_array(input)

# Assert
expect(answer).must_equal 3
end

it "will return nil for an empty array" do
# Arrange
input = []

# Act
answer = max_sub_array(input)

# Assert
expect(answer).must_be_nil
# Arrange
input = []
# Act
answer = max_sub_array(input)
# Assert
expect(answer).must_be_nil
end

it "will work for [50, -50, 50]" do
# Arrange
input = [50, -50, 50]

# Act
answer = max_sub_array(input)

# Assert
expect(answer).must_equal 50
end

end