Add another approach

This commit is contained in:
Vitor Oliveira 2021-03-15 21:57:25 -07:00
parent d5877acb14
commit 9576b18a61

View file

@ -57,6 +57,31 @@ puts remove_elements([0,1,2,2,3,0,4,2], 2)
# Approach 3: Two-pointers
#
# Complexity analysis
#
# Time complexity: O(n).
# Assume the array has a total of n elements,
# both i and j traverse at most 2n steps.
#
# Space complexity: O(1).
def remove_element(nums, val)
pointer1 = 0
nums.each_with_index do |num, pointer2|
if val != num
nums[pointer1] = nums[pointer2]
pointer1 += 1
end
end
pointer1
end
#
# Approach 4: Two-pointers (Optimized)
#
# Complexity analysis
#
# Time complexity: O(n). Both i and n traverse at most n steps.