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

Sarah #8

Open
wants to merge 2 commits 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
16 changes: 11 additions & 5 deletions lib/max_subarray.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n) where n is the length of nums, we iterate over the array once, and perform constant time operations each loop
# Space Complexity: O(1) we only use constant sized variables
def max_sub_array(nums)
return 0 if nums == nil

raise NotImplementedError, "Method not implemented yet!"
return 0 if nums == nil
best_total = nil
curr_total = 0
nums.each do |num|
curr_total += num
best_total = curr_total if !best_total or curr_total > best_total
curr_total = 0 if curr_total < 0
end
return best_total
end
19 changes: 15 additions & 4 deletions lib/newman_conway.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@


# Time complexity: ?
# Space Complexity: ?
# Time complexity: O(n) where n is the size of num
# Space Complexity: O(n) becuase we are creating a new array of size n

def newman_conway(num)

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
sequence = Array.new(num)
num.times do |i|
if i < 2
sequence[i] = 1
else
previous = sequence[i - 1]
sequence[i] = sequence[previous - 1] + sequence[i - previous]
end
end
return sequence.join(" ")
end