Skip to content

Conversation

@Faiza1987
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.

Nice work, you got the methods you wrote working well. However you need to see my notes on time complexity. Remember each recursive call puts things on the system stack.


# Time complexity: ?
# Space complexity: ?
def fib(n)

Choose a reason for hiding this comment

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

This works, but see my video on time and space complexity.

end
end

# Time complexity: O(n), where n isnthe length of the input

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) because s[0...-1] creates a new string and you do it n times.

if s.nil? || s.length == 0 || s.length == 1
return s
else
return s[-1] + reverse(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)
# Space complexity: O(1) because additional space in memory is not being consumed

Choose a reason for hiding this comment

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

O(n) since you use the system stack for the recursion.

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) where n is the size of the input
# Space complexity: O(1) because additional space in memory is not being consumed

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 since you use the system stack and create a new string with each iteration.

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