Skip to content
Open

Mona #12

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)
# Space Complexity: o(1)

def max_sub_array(nums)
return 0 if nums == nil

Choose a reason for hiding this comment

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

Suggested change
return 0 if nums == nil
return 0 if nums == nil
return nil if nums.length == 0


raise NotImplementedError, "Method not implemented yet!"
end
largest_sum = nums[0] #global max sub array
current = nums[0] #curren max sub array

(nums[1..-1]).each do |num|
current = [num, current + num].max
largest_sum = [largest_sum, current].max
end
return largest_sum
end
21 changes: 17 additions & 4 deletions lib/newman_conway.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@


# Time complexity: ?
# Space Complexity: ?
# Time complexity: 0(n)
# Space Complexity: 0(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
raise ArgumentError if num <= 0
return "1" if num == 1
return "1 1" if num == 2

result = [0, 1, 1]
i = 3

while i <= num
result << result[result[i - 1]] + result[i - result[i - 1]] #P(n) = P(P(n - 1)) + P(n - P(n - 1))
i += 1
end
p result

result[1..-1].join(' ') # remove 0
end