Skip to content

Conversation

@alexandria7
Copy link

No description provided.

Copy link

@CheezItMan CheezItMan left a comment

Choose a reason for hiding this comment

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

Not bad, the methods you wrote are good, but your time and space are a little off. There are some better ways to do things more efficiently. Take a look at my answer for reverse_in_place for a hint.

# Space complexity: ?
# Time complexity: O(n) where n is the size of the number input
# Space complexity: O(n) where n is the size of the number input
def factorial(n)

Choose a reason for hiding this comment

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

👍


# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) where n is the length of the string

Choose a reason for hiding this comment

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

This is actually O(n^2) for time and space due to the fact that s[1..-1] creates a new string with each recursive call.

# Space complexity: ?
# Time complexity: O(n) where n is the number of bunnies
# Space complexity: O(n) where n is the number of bunnies
def bunny(n)

Choose a reason for hiding this comment

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

👍

# Time complexity: ?
# Space complexity: ?
def reverse_inplace(s)
raise NotImplementedError, "Method not implemented"

Choose a reason for hiding this comment

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

Think about making a helper which takes the string, a left index and right index. The base case is when left_index >= right_index Each recursive call you swap the characters at left_index or right_index and then make a recursive call with the string, and left_index + 1 and right_index -1.

return false
elsif s.length == 0
return true
# chris!! I realize this would fail if given an initial string like ")(" but i can't figure it out!

Choose a reason for hiding this comment

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

Take a look at my notes for the reverse in place method and a similar solution can work here.


# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) where n is the length of the array

Choose a reason for hiding this comment

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

This is O(n^2) due to the fact that you create a new array with each recursive call.


# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) where n is the size of the number

Choose a reason for hiding this comment

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

O(n^2) for time and space.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants