Skip to content

Conversation

@amyesh
Copy link

@amyesh amyesh commented May 25, 2019

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, you wrote some effective recursive methods, but missed a bit on the time & space complexities. See my inline notes and let me know if you have questions.

def reverse(s)
raise NotImplementedError, "Method not implemented"
return s if s.length <= 1
reversed_str = reverse(s[1..-1])

Choose a reason for hiding this comment

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

s[1..-1] creates a new string of length n-2, which means your time and space complexities are going to be O(n^2)

def reverse_inplace(s)
raise NotImplementedError, "Method not implemented"
return s if s.length < 1
return (s[-1] + reverse_inplace(s[0...-1]))

Choose a reason for hiding this comment

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

This is not in place since you create a new string.

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.


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

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)

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

Choose a reason for hiding this comment

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

Space complexity will be O(n^2) due to the system stack and a new string on each level of the stack.


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

Choose a reason for hiding this comment

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

This is also O(n^2) for both space and time complexity.


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

Choose a reason for hiding this comment

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

O(n^2) for both time and space complexity. You could do it with O(n) using a helper method and current left_index and right_index parameters.

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

Choose a reason for hiding this comment

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

Due to the system stack, the space complexity will be O(n)

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