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

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n) => because I iterate depending on the input (nums)
# Space Complexity: O(1) => because I use the constant numbers of the space (2 variables)
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
return nil if nums.nil? || nums.empty?

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

nums[1..-1].each do |current_num|
current_max = [current_num, current_num + current_max].max
max_so_far = [current_max, max_so_far].max
end

return max_so_far
end
22 changes: 18 additions & 4 deletions lib/newman_conway.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@


# Time complexity: ?
# Space Complexity: ?
def newman_conway(num)
raise NotImplementedError, "newman_conway isn't implemented"
# Time complexity: O(n) => because I iterate depending on the input (num)
# Space Complexity: O(n) => because I create a new array depending on the input (num)

def newman_conway(num) # 9
Comment on lines +3 to +6

Choose a reason for hiding this comment

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

👍

raise ArgumentError if num == 0
solution = []

(1..num).each do |n|
if n == 1 || n == 2
solution[n] = 1
else
# P(P(n - 1)) + P(n - P(n - 1))
solution[n] = solution[solution[n - 1]] + solution[n - solution[n - 1]]
end

end

return solution[1..-1].join(" ")
end
14 changes: 7 additions & 7 deletions 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 @@ -46,14 +46,14 @@
end

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

# Act
answer = max_sub_array(input)
# Act
answer = max_sub_array(input)

# Assert
expect(answer).must_be_nil
# Assert
expect(answer).must_be_nil
end

it "will work for [50, -50, 50]" do
Expand Down