Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leaves - Mariya #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
25 changes: 21 additions & 4 deletions lib/max_subarray.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(1)
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 you're on the right track, but not quite there yet. 😢

return 0 if nums == nil
return 0 if nums == nil || nums.empty?

raise NotImplementedError, "Method not implemented yet!"
max = nums[0]
current = nums[0]

Choose a reason for hiding this comment

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

Otherwise you will add nums[0] twice.

Suggested change
current = nums[0]
current = 0


nums.each do |index|

Choose a reason for hiding this comment

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

index here won't be an index into the array, it will be a value.

Maybe you meant:

Suggested change
nums.each do |index|
nums.length.time do |index|

temp = current + max[index]

if temp > nums[index]
current = max
else
current = nums[index]
end

if max < current
max = current
end
end

return max
end
32 changes: 29 additions & 3 deletions lib/newman_conway.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@


# Time complexity: ?
# Space Complexity: ?
# Time complexity: O(1)
# Space Complexity: O(n)

def newman_helper(num, newman_hash)
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.

This works, although you could use an array just as easily as a hash here.

if newman_hash[num]
return newman_hash[num]
end

if num == 1 || num == 2
return 1
else
newman_hash[num] = newman_helper(newman_helper(num - 1, newman_hash), newman_hash) +
newman_helper(num - newman_helper(num - 1, newman_hash), newman_hash)
return newman_hash[num]
end
end

def newman_conway(num)
raise NotImplementedError, "newman_conway isn't implemented"
newman_hash = {}

if num < 1
raise ArgumentError
end
newman_array = []

num.times do |i|
newman_array << newman_helper(i + 1, newman_hash)
end

return newman_array.join(" ")
end