From 9429ae92c1769f47c0d8465de18a576e55a68ff8 Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Fri, 12 Mar 2021 18:51:41 -0800 Subject: [PATCH 1/3] Add remove elements challenge instructions --- DIRECTORY.md | 3 ++- data_structures/arrays/remove_elements.rb | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 data_structures/arrays/remove_elements.rb diff --git a/DIRECTORY.md b/DIRECTORY.md index 6045aa6..fd7a1dd 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -9,7 +9,8 @@ * [Get Products Of All Other Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/get_products_of_all_other_elements.rb) * [Sort Squares Of An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/sort_squares_of_an_array.rb) * [Two Sum](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/two_sum.rb) - * [Two Sum Ii](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/two_sum_ii.rb) + * [Two Sum II](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/two_sum_ii.rb) + * [Remove Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/remove_elements.rb) * Binary Trees * [Inorder Traversal](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/binary_trees/inorder_traversal.rb) * [Invert](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/binary_trees/invert.rb) diff --git a/data_structures/arrays/remove_elements.rb b/data_structures/arrays/remove_elements.rb new file mode 100644 index 0000000..e407c7b --- /dev/null +++ b/data_structures/arrays/remove_elements.rb @@ -0,0 +1,18 @@ +# Arrays - Remove Elements +# +# Given an array nums and a value val, remove all instances of that value in-place and return the new length. +# Do not allocate extra space for another array, +# you must do this by modifying the input array in-place with O(1) extra memory. +# The order of elements can be changed. It doesn't matter what you leave beyond the new length. +# +# Example +# +# Input: nums = [3,2,2,3], val = 3 +# Output: 2, nums = [2,2] +# +# Input: nums = [0,1,2,2,3,0,4,2], val = 2 +# Output: 5, nums = [0,1,4,0,3] + +# +# Approach 1: Brute Force +# \ No newline at end of file From ba03727fb6cef1ab4a06f994ffe549030f53f163 Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Mon, 15 Mar 2021 09:52:22 -0700 Subject: [PATCH 2/3] Add solution using delete_if Ruby method --- data_structures/arrays/remove_elements.rb | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/data_structures/arrays/remove_elements.rb b/data_structures/arrays/remove_elements.rb index e407c7b..84abca7 100644 --- a/data_structures/arrays/remove_elements.rb +++ b/data_structures/arrays/remove_elements.rb @@ -14,5 +14,16 @@ # Output: 5, nums = [0,1,4,0,3] # -# Approach 1: Brute Force -# \ No newline at end of file +# Approach 1: Use `delete_if` Ruby method +# +# Time complexity: O(n) +# +def remove_elements(nums, val) + nums.delete_if{ |num| num == val } + nums.length +end + +puts remove_elements([3,2,2,3], 3) +# => 2 +puts remove_elements([0,1,2,2,3,0,4,2], 2) +# => 5 \ No newline at end of file From 1f95ad15df10818ed7bac6f66dc37e05c8a97f79 Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Mon, 15 Mar 2021 10:08:31 -0700 Subject: [PATCH 3/3] Add solution using delete_at Ruby method --- data_structures/arrays/remove_elements.rb | 27 ++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/data_structures/arrays/remove_elements.rb b/data_structures/arrays/remove_elements.rb index 84abca7..32260ac 100644 --- a/data_structures/arrays/remove_elements.rb +++ b/data_structures/arrays/remove_elements.rb @@ -26,4 +26,29 @@ end puts remove_elements([3,2,2,3], 3) # => 2 puts remove_elements([0,1,2,2,3,0,4,2], 2) -# => 5 \ No newline at end of file +# => 5 + +# +# Approach 2: Use `delete_at`, `unshift`, and `shift` Ruby method +# +# Time complexity: O(n) +# +def remove_elements(nums, val) + result_length = nums.length + shift_length = 0 + nums.each_with_index do |num, i| + if num == val + nums.delete_at(i) + nums.unshift('removed') + result_length -=1 + shift_length += 1 + end + end + nums.shift(shift_length) + result_length +end + +puts remove_elements([3,2,2,3], 3) +# => 2 +puts remove_elements([0,1,2,2,3,0,4,2], 2) +# => 5