-
Notifications
You must be signed in to change notification settings - Fork 49
Leaves - Elizabeth #44
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?
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,47 @@ | ||
|
||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
# Time Complexity: O(n) | ||
# Space Complexity: 0(1) | ||
def remove_duplicates(list) | ||
raise NotImplementedError, "Not implemented yet" | ||
i = 0 | ||
j = 1 | ||
until list[j] == nil | ||
if list[i] == list[j] | ||
list.delete_at(list[j]) | ||
else | ||
i+=1 | ||
j+=1 | ||
end | ||
end | ||
return list | ||
end | ||
|
||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
# Time Complexity: O(n^4) | ||
# Space Complexity: 0(n) | ||
def longest_prefix(strings) | ||
raise NotImplementedError, "Not implemented yet" | ||
i = 1 | ||
j = 0 | ||
k = 0 | ||
output = "" | ||
characters = [] | ||
length = strings.length | ||
strings.each do |word| | ||
characters << word.chars | ||
end | ||
|
||
characters.each do |item| | ||
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. This isn't working, take a look at the loops a bit. You can fairly comfortably do this by having one loop go through all the words, and the 2nd looping through the letters in the 1st word. When you find a letter than not all the words have in the right position, you've found your prefix. |
||
until j == length-1 | ||
until k == length-1 | ||
if item[k] != characters[i][k] | ||
return output | ||
else | ||
|
||
i += 1 | ||
end | ||
output += item[k] | ||
k += 1 | ||
j += 1 | ||
end | ||
end | ||
return output | ||
end | ||
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.
delete_at
removes an element by shifting all subsequent elements left by one index. So it's an O(n) method. Since it's nested inside a loop running n times,remove_duplicates
is an O(n2) method.