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, Kristy #35

Open
wants to merge 1 commit 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
39 changes: 32 additions & 7 deletions lib/practice_exercises.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(n)
def remove_duplicates(list)

Choose a reason for hiding this comment

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

That works! The time complexity is correct, but you are not building a new data structure so your space complexity is not correct. Instead that is O(1).

raise NotImplementedError, "Not implemented yet"
# go through each element in the array and remove duplicates

if list.length == 1 || list[0] == nil
return list
end

list.each_with_index do |item, i|
if item == list[i + 1]
list[i + 1] = nil
end
end


return list

end

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

Choose a reason for hiding this comment

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

Well done!

# Space Complexity: O(1)
def longest_prefix(strings)
raise NotImplementedError, "Not implemented yet"

longest_prefix = ""
strings[0].length.times do |i|
char = strings[0][i]
strings.each_with_index do |string|
if string[i] != char || string[i] == nil
break
elsif char == string[i] && string == strings.last
longest_prefix += char
end
end
end
return longest_prefix
end