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

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n!)? You only go throught the each with index n times, but the inner loop completes decrementing number of loops...
# Space Complexity: O(1), no additional data structures created outside of 2 variables each times
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.

⚠️ I think this is O(n^2) due to the tested loop. You can do it without the inner loop, and just with some if-else statements.

return 0 if nums == nil

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

max_so_far = nums[0]
max_ending_here = 0

nums.each_with_index do |num, index|
i = index + 1

if num > max_so_far
max_so_far = num
end

max_ending_here = num

while i < nums.length do
max_ending_here = max_ending_here + nums[i]
if (max_so_far < max_ending_here)
max_so_far = max_ending_here
end
i+=1
end
Comment on lines +21 to +27

Choose a reason for hiding this comment

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

You can do this without the nested loop. Think about how you could use same saved values, like the largest subarray so far and the largest ending at the current index and how that could work.

end


return max_so_far
end
30 changes: 27 additions & 3 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), always creating 1 array num + 1 length long and 1 string proprtionate to n
# P(1) = 1
# P(2) = 1
# for all n > 2
# P(n) = P(P(n - 1)) + P(n - P(n - 1))
# 1 1 2 2 3 4 4 4 5 6 7 7….
def newman_conway(num)
Comment on lines +3 to 10

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"
raise ArgumentError, "num must be > 0" if num < 1

return "1" if num == 1
return "1 1" if num == 2

ncs_numbers = Array.new(num + 1)
ncs_numbers[1] = 1
ncs_numbers[2] = 1

n = 3

ncs_string = "1 1"
while n <= num
ncs_numbers[n] = ncs_numbers[ncs_numbers[n-1]] + ncs_numbers[n - ncs_numbers[n-1]]
ncs_string << " "
ncs_string << ncs_numbers[n].to_s
n += 1
end

return ncs_string
end
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