Skip to content

Ports - Sopheary#10

Open
sophearychiv wants to merge 1 commit intoAda-C11:masterfrom
sophearychiv:master
Open

Ports - Sopheary#10
sophearychiv wants to merge 1 commit intoAda-C11:masterfrom
sophearychiv:master

Conversation

@sophearychiv
Copy link
Copy Markdown

No description provided.

Copy link
Copy Markdown

@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 have working algorithms, but some inefficiencies, take a look at my comments and let me know if you have questions.

Comment thread lib/recursive-methods.rb
raise NotImplementedError, "Method not implemented"
return s if s.length == 0
last = s[-1]
s = s[0..-2]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This creates a new string which increases the time and space time complexity.

So the time and space complexity is O(n^2).

Comment thread lib/recursive-methods.rb
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) where n is the length of the string
# Space complexity: O(1)
Copy link
Copy Markdown

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) since you end up using the system stack.

Comment thread lib/recursive-methods.rb

# Time complexity: ?
# Space complexity: ?
def reverse_helper(s, first_index, last_index)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍

Comment thread lib/recursive-methods.rb
def nested(s)
raise NotImplementedError, "Method not implemented"
return true if s == ""
return false if !s.include?("(")
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This isn't a good idea since it goes through the entire length of the string. That means your algorithm is O(n^2).

Instead just check the 1st and last character.

Comment thread lib/recursive-methods.rb

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

Choose a reason for hiding this comment

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

👍

Comment thread lib/recursive-methods.rb
raise NotImplementedError, "Method not implemented"
return true if s.empty?
if s[0] == s[-1]
s.sub!(s[0], "")
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

sub is an O(n) algorithm, so this is an O(n) operation you are doing n times so the full algorithm is O(n^2) for both time and space complexity.

Instead think about tracking the left and right indexes adjusting them each recursive call.

Comment thread lib/recursive-methods.rb
# Space complexity: ?
# Time complexity: O(n) where n is the lenght of the smaller number
# Space complexity: O(1) because there's no variable storing the values of the numbers
def digit_match(n, m)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍

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