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
22 changes: 20 additions & 2 deletions lib/max_subarray.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,25 @@
# Time Complexity: ?
# Space Complexity: ?
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.

This works, but no guesses on time/space complexity?

Copy link
Author

@brikemp brikemp Apr 1, 2020

Choose a reason for hiding this comment

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

Whoops, totally missed those! I think the time should be O(n) and space O(1)

return 0 if nums == nil
return nil if (nums == nil || nums.length === 0)

max = nums[0]
temp = 0

nums.each do |num|
temp += num

raise NotImplementedError, "Method not implemented yet!"
if temp > max
max = temp
end

if temp < 0
temp = 0
end
end

return max
end


# p max_sub_array([-2,1,-3,4,-1,2,1,-5,4])
26 changes: 21 additions & 5 deletions lib/newman_conway.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
# Time complexity: O(n)
# Space Complexity: O(n)
def newman_conway(num)
Comment on lines +1 to +3

Choose a reason for hiding this comment

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

Good combination of recursion and dynamic programming 😄

return nc_helper(num)
end

def nc_helper(num, count = 2, memo = [0, 1, 1], result = "1 1")
raise ArgumentError if num <= 0
return "1" if num == 1
return "1 1" if num == 2

if count == num
return result
end

value = memo[memo[count]] + memo[count + 1 - memo[count]]
memo << value
result << " #{value}"

return nc_helper(num, count + 1, memo, result)
end

# Time complexity: ?
# Space Complexity: ?
def newman_conway(num)
raise NotImplementedError, "newman_conway isn't implemented"
end
# p newman_conway(12)