Skip to content

Conversation

@shubha-rajan
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.

You did well and they all work, but you made some mistakes on the big-O due to the fact that you misunderstood what was happening with some of the built-in functions.

You are also doing a very odd search where you split the array, but you aren't doing a binary search, you search both halves. Take a look at my comment on that one.


# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) where n/2 recursive calls are made

Choose a reason for hiding this comment

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

Not quite, as s[1..s.length-1] makes a new string with n-2 characters, and since you do this every recursive call... you end up with O(n * n) = O(n^2).

There is another way by using a helper method and keeping track of the current indexes which is O(n).

Copy link
Author

@shubha-rajan shubha-rajan May 29, 2019

Choose a reason for hiding this comment

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

Gotcha. For some reason I thought accessing a slice of an array/string would access part of what was already stored in memory.

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) where n/2 recursive calls are made
# Space complexity: O(n) where the max depth of the function is n/2

Choose a reason for hiding this comment

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

Similarly each recursive call makes a new array so O(n^2).


# Time complexity: ?
# Space complexity: ?
# Time complexity: If n < m, time complexity will be O(log10 n) since the number of calls is dependent on how many times the smaller number can be divided by 10

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 recursive calls are made

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 recursive calls are made

Choose a reason for hiding this comment

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

Not quite since s[0..-1] makes a new string, it's O(n^2)

It's similar for Space complexity.

if s.length == 1 || s.length == 0
return s
else
s[-1] + reverse_inplace(s[1...-1]) + s[0]

Choose a reason for hiding this comment

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

Unfortunately this isn't in place. It makes a new string.

if n <= 0
return 0
else
return 2 + bunny(n - 1)

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/2 recursive calls are made

Choose a reason for hiding this comment

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

Similar to the above.

if array.length == 1
return true if array[0] == value
else
first_half = array[0...(array.length / 2)]

Choose a reason for hiding this comment

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

This looks like an attempt at MergeSort or something. Because you're making new arrays, this will be O(n^2), but I don't see what advantage you're getting by splitting it.

This could be easier as:

return false if array.length == 0
return true if value == array[0]
return search(array[1...-1])

Copy link
Author

Choose a reason for hiding this comment

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

Wouldn't this save stack memory (space complexity) though? At most, the method will only go log2(n) levels deep at a time, as opposed to n levels if you just knocked off one number with each call.

Choose a reason for hiding this comment

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

It will save on some stack space, but won't save you in time complexity.

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.

Much better reverse_in_place method.


# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)^2 where n/2 recursive calls are made and each calls slice, which is O(n)

Choose a reason for hiding this comment

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

now that you're doing this with swap, this method is O(n), well done!

if s.length == 1 || s.length == 0
return s
else
s[-1] + reverse_inplace(s[1...-1]) + s[0]

Choose a reason for hiding this comment

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

reverse uses reverse_in_place?

Copy link
Author

Choose a reason for hiding this comment

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

Whoops. fixed!

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