-
Notifications
You must be signed in to change notification settings - Fork 41
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
base: master
Are you sure you want to change the base?
Leaves - Mariya #28
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,25 @@ | ||||||
|
||||||
# Time Complexity: ? | ||||||
# Space Complexity: ? | ||||||
# Time Complexity: O(n) | ||||||
# Space Complexity: O(1) | ||||||
def max_sub_array(nums) | ||||||
return 0 if nums == nil | ||||||
return 0 if nums == nil || nums.empty? | ||||||
|
||||||
raise NotImplementedError, "Method not implemented yet!" | ||||||
max = nums[0] | ||||||
current = nums[0] | ||||||
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. Otherwise you will add
Suggested change
|
||||||
|
||||||
nums.each do |index| | ||||||
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. index here won't be an index into the array, it will be a value. Maybe you meant:
Suggested change
|
||||||
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 |
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
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. 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 |
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.
I think you're on the right track, but not quite there yet. 😢