-
Notifications
You must be signed in to change notification settings - Fork 29
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
Elle #6
base: master
Are you sure you want to change the base?
Elle #6
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,39 @@ | ||
|
||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
# Time Complexity: O(n) where n is the length of the array | ||
# Space Complexity: O(1) | ||
|
||
def max_sub_array(nums) | ||
return 0 if nums == nil | ||
return nil if nums == [] | ||
|
||
max_so_far = nums[0] | ||
sum_ending_here = nums[0] | ||
|
||
# ----------------------------------------------------- | ||
# SOLUTION 1 | ||
nums[1..-1].each do |num| | ||
|
||
sum_ending_here = [sum_ending_here + num, num].max | ||
|
||
if max_so_far < sum_ending_here | ||
max_so_far = sum_ending_here | ||
end | ||
end | ||
|
||
# ----------------------------------------------------- | ||
|
||
# SOLUTION 2 | ||
|
||
# nums[1..-1].each do |num| | ||
|
||
# sum_ending_here = sum_ending_here + num | ||
|
||
# if max_so_far < sum_ending_here | ||
# max_so_far = sum_ending_here | ||
# elsif sum_ending_here < 0 | ||
# sum_ending_here = 0 | ||
# end | ||
# end | ||
# ----------------------------------------------------- | ||
|
||
raise NotImplementedError, "Method not implemented yet!" | ||
return max_so_far | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,27 @@ | ||
# Time complexity: O(n) | ||
# Space Complexity: O(n) | ||
|
||
|
||
# Time complexity: ? | ||
# Space Complexity: ? | ||
def newman_conway(num) | ||
raise NotImplementedError, "newman_conway isn't implemented" | ||
solution = [1] | ||
raise ArgumentError, "Number must be 1 or greater" if num <1 | ||
p(num, solution) | ||
|
||
string_solution = "" | ||
solution.each do |char| | ||
string_solution << "#{char.to_s} " | ||
end | ||
|
||
return string_solution[0...-1] | ||
end | ||
|
||
def p(n, s) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just noting the variable names are not meaningful here. |
||
return s[n-1] if s[n-1] | ||
|
||
if (n == 2) && !s[2-1] | ||
s[2-1] = 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just do |
||
else | ||
s[n-1] = p(p(n - 1, s), s) + p(n - p(n - 1, s), s) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice both recursive and efficient solution. |
||
end | ||
|
||
return s[n-1] | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Nicely done!