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

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(nums)
# Space Complexity: O(1)
def max_sub_array(nums)
return 0 if nums == nil

raise NotImplementedError, "Method not implemented yet!"
max_so_far = nums[0]
max_ending_here = nums[0]

counter = 1
until counter > nums.length - 1 # T: O(nums)
max_ending_here = max_ending_here + nums[counter]
if max_ending_here < nums[counter]
max_ending_here = nums[counter]
end

if max_ending_here > max_so_far
max_so_far = max_ending_here
end
counter += 1
end
return max_so_far
end
28 changes: 22 additions & 6 deletions lib/newman_conway.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@


# Time complexity: ?
# Space Complexity: ?
# Time complexity: O(n * num) ?
# Space Complexity: O(num) ?
def newman_conway(num)
raise NotImplementedError, "newman_conway isn't implemented"
end
newman_conway_helper([0, 1, 1], 3, num)
end

def newman_conway_helper(p, current, n)
if n <= 0
raise ArgumentError, "Input must be greater than 0."
end

return "1" if n == 1
return "1 1" if n == 2

if current > n
p.shift # S: O(n)? T: O(n)?
return p.join(" ")
end

p << p[p[current - 1]] + p[current - p[current - 1]]

return newman_conway_helper(p, current + 1, n) # T: O(num) calls this recursively about num number of times
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
58 changes: 29 additions & 29 deletions test/newman_conway_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,51 @@
it "works with 13" do
# Arrange
input = 13

# Act
answer = newman_conway(input)

# Assert
expect(answer).must_equal "1 1 2 2 3 4 4 4 5 6 7 7 8"
end

it "works with 20" do
# Arrange
input = 20

# Act
answer = newman_conway(input)

# Assert
expect(answer).must_equal "1 1 2 2 3 4 4 4 5 6 7 7 8 8 8 8 9 10 11 12"
end

it "works with base cases" do
# Arrange
input = 0

# Act-Assert
expect {
newman_conway(input)
}.must_raise ArgumentError
# Arrange
input = 0


# Arrange
input = 1

# Act
answer = newman_conway(input)
# Act-Assert
expect {
newman_conway(input)
}.must_raise ArgumentError

# Assert
expect(answer).must_equal "1"

# Arrange
input = 2

# Act
answer = newman_conway(input)

# Assert
expect(answer).must_equal "1 1"
# Arrange
input = 1

# Act
answer = newman_conway(input)

# Assert
expect(answer).must_equal "1"

# Arrange
input = 2

# Act
answer = newman_conway(input)

# Assert
expect(answer).must_equal "1 1"
end
end