-
Notifications
You must be signed in to change notification settings - Fork 49
Branches - Caroline #32
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,89 @@ | ||
|
||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def remove_duplicates(list) | ||
raise NotImplementedError, "Not implemented yet" | ||
# list is a sorted array of numbers | ||
unique_list = [] | ||
|
||
list.each do |element| | ||
unless unique_list[-1] == element | ||
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. very clever! |
||
unique_list << element | ||
end | ||
end | ||
|
||
return unique_list | ||
end | ||
|
||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
|
||
|
||
|
||
# Time Complexity: O(n^2) | ||
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 is actually O(m * n) where m is the length of the smallest string. |
||
# Space Complexity: O(n) | ||
def longest_prefix(strings) | ||
raise NotImplementedError, "Not implemented yet" | ||
shortest_string = strings.min_by { |string| string.length} # O(n) | ||
|
||
match = "" | ||
shortest_string.length.times do |index| #O(n) | ||
curr_char = strings[0][index] | ||
|
||
strings.each do |string| #O(n) | ||
if string[index] != curr_char | ||
return match | ||
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. Nice! |
||
end | ||
end | ||
|
||
# all strings have matching letter on this index position | ||
match << curr_char | ||
end | ||
|
||
return match | ||
end | ||
|
||
|
||
|
||
|
||
|
||
#### ALTERNATE METHOD, THOUGHT I COULD DO BETTER THAN O(n^2) BUT COULDN'T #### | ||
|
||
# def longest_prefix_ALT(strings) | ||
# # find the longest common prefix string amongst an array of strings | ||
# # Time O(n^2) and Space O(n) | ||
|
||
# # get the match results from the first 2 strings | ||
# prev_match = get_head_matches(strings[0], strings[1]) | ||
|
||
# # if the first 2 strings don't have any chars in common, answer is automatically "" | ||
# if prev_match == "" | ||
# return "" | ||
# end | ||
|
||
# # compare the prev_match with each subsequent string, modifying prev_match with each iteration | ||
# ((strings.length)-2).times do |index| | ||
# prev_match = get_head_matches(prev_match, strings[index+2]) | ||
# puts | ||
# end | ||
|
||
# return prev_match | ||
# end | ||
|
||
# def get_head_matches(string1, string2) | ||
# # O(n) time & O(1) space | ||
# shorter_string = [string1, string2].min_by { |str| str.length } | ||
|
||
# index = 0 | ||
# match = "" | ||
|
||
# while index < shorter_string.length | ||
# if string1[index] == string2[index] | ||
# match << string1[index] | ||
# else | ||
# break | ||
# end | ||
|
||
# index += 1 | ||
# end | ||
|
||
# return match | ||
# 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.
Correct!