Skip to content

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

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
88 changes: 82 additions & 6 deletions lib/practice_exercises.rb
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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct!

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

Choose a reason for hiding this comment

The 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)

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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