Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 41 additions & 8 deletions lib/practice_exercises.rb
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)

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)

end
end
end
return list
end

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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