-
Notifications
You must be signed in to change notification settings - Fork 49
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
Branches - Emily Ball #30
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,46 @@ | ||
|
||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
# Time Complexity: O(n^2) | ||
# Space Complexity: O(1) | ||
def remove_duplicates(list) | ||
raise NotImplementedError, "Not implemented yet" | ||
list.each_with_index do |value, index| | ||
if index > 0 | ||
if value == list[index - 1] | ||
list.slice!(index) | ||
end | ||
end | ||
end | ||
return list | ||
end | ||
|
||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
# Time Complexity: O(n) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you have those two nested loops you actually have a time complexity of O(n2) |
||
# Space Complexity: O(n) | ||
|
||
def longest_prefix(strings) | ||
raise NotImplementedError, "Not implemented yet" | ||
end | ||
|
||
min_string = strings.min { |a,b| a.length <=> b.length} | ||
hi = min_string.length - 1 | ||
low = 0 | ||
mid = (hi + low)/2 | ||
commons = [] | ||
|
||
strings.each do |string| | ||
if min_string == string.slice(low..hi) | ||
commons << string | ||
end | ||
end | ||
return min_string if commons.length == strings.length | ||
|
||
until hi <= low | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you never change low, you may not need it. This made me think of binary search and it confused me for a bit. |
||
commons = [] | ||
hi -= 1 | ||
prefix = min_string.slice(low..hi) | ||
|
||
strings.each do |string| | ||
if prefix == string.slice(low..hi) | ||
commons << string | ||
end | ||
end | ||
return prefix if commons.length == strings.length | ||
end | ||
|
||
return "" | ||
end |
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.
Slice is going to shift subsequent elements to the left one index. Thus it as a time complexity of O(n). Inside this loop your method as time complexity of O(n2)