Skip to content

Emily - Leaves #47

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
48 changes: 40 additions & 8 deletions lib/practice_exercises.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,45 @@

# 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.

Correct!

# Space Complexity: O(1)
def remove_duplicates(list)
raise NotImplementedError, "Not implemented yet"
length = list.length
if length == 0
return []
end
(1...length).each do |k|
if list[k] == list[k-1]
i = k
until i > length || list[i-1] == nil do

Choose a reason for hiding this comment

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

I would say until i >= length || list[i-1] == nil do

list[i - 1] = list[i]
i += 1
end
end
k += 1
end
return list
end

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n), where n is total length of all strings combined

Choose a reason for hiding this comment

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

I would say O(n * m) where n is the number of strings and m is the number of letters in the average string. Unless the strings are small in which case you could say O(n)

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

strings.each do |string|
if string.length == 0
return ""
end
end
prefix = strings[0]
(1...strings.length).each do |n|
(0...prefix.length).each do |i|
if strings[n][i] != prefix[i]
if i == 0
prefix = ""
break
else
prefix = prefix[0...i]
break
end
end
end
end
return prefix
end