Skip to content
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
46 changes: 38 additions & 8 deletions lib/practice_exercises.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
require "pry"
# Given nums = [0,0,1,1,1,2,2,3,3,4]
# Your function should return length = 5,
# with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n) - depends of the length of the list
# Space Complexity: O(1) - doens't create another array
def remove_duplicates(list)
raise NotImplementedError, "Not implemented yet"
end
i = 0
while i < list.length
if list[i] == list[i+1]
list.delete_at(i)

Choose a reason for hiding this comment

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

delete_at shifts all subsequent elements left one index each. This is an O(n) method. Since it's nested inside a loop running n times, this means the method is an O(n2) algorithm.

end
i += 1
end
return list
end

# Find the longest common prefix string amongst an array of strings.
# Input: ["flower","flow","flight"]
# Output: "fl"
# Input: ["dog","racecar","car"]
# Output: ""

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(nm) - depends on the length of strings = n and length of string[0] = m
# Space Complexity: O(m) - is reacreting a result total
def longest_prefix(strings)

Choose a reason for hiding this comment

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

Well done.

raise NotImplementedError, "Not implemented yet"
result = ""
i = 0

strings[0].each_char do |letter|
strings.each do |string|
if letter != string[i]
return result
end
end

result += letter
i += 1
end

return result
end