-
Notifications
You must be signed in to change notification settings - Fork 39
Ports - Faiza #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Ports - Faiza #11
Conversation
CheezItMan
left a comment
There was a problem hiding this 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) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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]) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
No description provided.