From 989e20d15ee85cbbc372c79801d17a4a44733bef Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Mon, 22 Mar 2021 09:00:58 -0700 Subject: [PATCH 01/83] Add shuffle array challenge --- DIRECTORY.md | 1 + data_structures/arrays/shuffle_array.rb | 36 +++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 data_structures/arrays/shuffle_array.rb diff --git a/DIRECTORY.md b/DIRECTORY.md index 5983249..4eb3612 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -13,6 +13,7 @@ * [Fizz Buzz](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/fizz_buzz.rb) * [Get Products Of All Other Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/get_products_of_all_other_elements.rb) * [Remove Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/remove_elements.rb) + * [Shuffle Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/shuffle_array.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) diff --git a/data_structures/arrays/shuffle_array.rb b/data_structures/arrays/shuffle_array.rb new file mode 100644 index 0000000..93efa84 --- /dev/null +++ b/data_structures/arrays/shuffle_array.rb @@ -0,0 +1,36 @@ +# Challenge name: Shuffle the array +# Given the array nums consisting of 2n elements +# in the form [x1,x2,...,xn,y1,y2,...,yn]. +# Return the array in the form [x1,y1,x2,y2,...,xn,yn]. +# Example 1: +# +# Input: nums = [2,5,1,3,4,7], n = 3 +# Output: [2,3,5,4,1,7] +# Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7]. +# +# Example 2: +# +# Input: nums = [1,2,3,4,4,3,2,1], n = 4 +# Output: [1,4,2,3,3,2,4,1] +# +# Example 3: +# +# Input: nums = [1,1,2,2], n = 2 +# Output: [1,2,1,2] +# @param {Integer[]} nums +# @param {Integer} n +# @return {Integer[]} +def shuffle(nums, n) +end +nums = [2, 5, 1, 3, 4, 7] +n = 3 +print(shuffle(nums, n)) +# Output: [2,3,5,4,1,7] +nums = [1, 2, 3, 4, 4, 3, 2, 1] +n = 4 +print(shuffle(nums, n)) +# Output: [1,4,2,3,3,2,4,1] +nums = [1, 1, 2, 2] +n = 2 +print(shuffle(nums, n)) +# Output: [1,2,1,2] \ No newline at end of file From 16fd8c756ff5508c611bf42173a9fc998c3f5e8e Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Tue, 23 Mar 2021 08:42:36 -0700 Subject: [PATCH 02/83] Add solution using a new array --- data_structures/arrays/shuffle_array.rb | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/data_structures/arrays/shuffle_array.rb b/data_structures/arrays/shuffle_array.rb index 93efa84..b4c6431 100644 --- a/data_structures/arrays/shuffle_array.rb +++ b/data_structures/arrays/shuffle_array.rb @@ -1,27 +1,39 @@ # Challenge name: Shuffle the array +# # Given the array nums consisting of 2n elements # in the form [x1,x2,...,xn,y1,y2,...,yn]. # Return the array in the form [x1,y1,x2,y2,...,xn,yn]. -# Example 1: # +# Example 1: # Input: nums = [2,5,1,3,4,7], n = 3 # Output: [2,3,5,4,1,7] # Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7]. # # Example 2: -# # Input: nums = [1,2,3,4,4,3,2,1], n = 4 # Output: [1,4,2,3,3,2,4,1] # # Example 3: -# # Input: nums = [1,1,2,2], n = 2 # Output: [1,2,1,2] +# # @param {Integer[]} nums # @param {Integer} n # @return {Integer[]} + +# +# Approach 1: New Array +# +# Time Complexity: O(N) +# def shuffle(nums, n) + result = [] + (0..n-1).count do |i| + result.push(nums[i], nums[i+n]) + end + result end + nums = [2, 5, 1, 3, 4, 7] n = 3 print(shuffle(nums, n)) From a8f816f984ad98f59942aae06eff775816bf83f8 Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Wed, 24 Mar 2021 08:12:31 -0700 Subject: [PATCH 03/83] Add solution using Ruby methods --- data_structures/arrays/shuffle_array.rb | 28 +++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/data_structures/arrays/shuffle_array.rb b/data_structures/arrays/shuffle_array.rb index b4c6431..2f1b13f 100644 --- a/data_structures/arrays/shuffle_array.rb +++ b/data_structures/arrays/shuffle_array.rb @@ -34,6 +34,34 @@ def shuffle(nums, n) result end +# nums = [2, 5, 1, 3, 4, 7] +# n = 3 +# print(shuffle(nums, n)) +# # Output: [2,3,5,4,1,7] +# nums = [1, 2, 3, 4, 4, 3, 2, 1] +# n = 4 +# print(shuffle(nums, n)) +# # Output: [1,4,2,3,3,2,4,1] +# nums = [1, 1, 2, 2] +# n = 2 +# print(shuffle(nums, n)) +# # Output: [1,2,1,2] + +# +# Approach 2: Use Ruby methods .insert() and .delete_at() +# +# Time Complexity: O(N) +# + +def shuffle(nums, n) + current_index = 1 + (0..n-1).each do |i| + nums.insert(current_index, nums.delete_at(i + n)) + current_index += 2 + end + nums +end + nums = [2, 5, 1, 3, 4, 7] n = 3 print(shuffle(nums, n)) From 6a6d0b675a3df7eee3867782398ba186758e4abd Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Wed, 24 Mar 2021 08:19:11 -0700 Subject: [PATCH 04/83] Add challenge --- DIRECTORY.md | 1 + data_structures/arrays/richest_customer.rb | 31 ++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 data_structures/arrays/richest_customer.rb diff --git a/DIRECTORY.md b/DIRECTORY.md index e8d246d..910dd30 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -13,6 +13,7 @@ * [Fizz Buzz](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/fizz_buzz.rb) * [Get Products Of All Other Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/get_products_of_all_other_elements.rb) * [Remove Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/remove_elements.rb) + * [Richest Customer](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/richest_customer.rb) * [Single Number](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/single_number.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) diff --git a/data_structures/arrays/richest_customer.rb b/data_structures/arrays/richest_customer.rb new file mode 100644 index 0000000..8fc8173 --- /dev/null +++ b/data_structures/arrays/richest_customer.rb @@ -0,0 +1,31 @@ +# Challenge name: Richest Customer +# +# You are given an m x n integer grid accounts where accounts[i][j] +# is the amount of money the i​​​​​​​​​​​th​​​​ customer has in the j​​​​​​​​​​​th​​​​ bank. +# +# Return the wealth that the richest customer has. +# A customer's wealth is the amount of money they have in all +# their bank accounts. The richest customer is the customer that +# has the maximum wealth. +# +# Example 1: +# Input: accounts = [[1,2,3],[3,2,1]] +# Output: 6 +# Explanation: +# 1st customer has wealth = 1 + 2 + 3 = 6 +# 2nd customer has wealth = 3 + 2 + 1 = 6 +# Both customers are considered the richest with a wealth of 6 +# each, so return 6. +# +# Example 2: +# Input: accounts = [[1,5],[7,3],[3,5]] +# Output: 10 +# Explanation: +# 1st customer has wealth = 6 +# 2nd customer has wealth = 10 +# 3rd customer has wealth = 8 +# The 2nd customer is the richest with a wealth of 10. +# +# Example 3: +# Input: accounts = [[2,8,7],[7,1,3],[1,9,5]] +# Output: 17 \ No newline at end of file From 187cdac8b137495c347c77ff0fa71b91ccb99a5b Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Thu, 25 Mar 2021 10:08:15 -0700 Subject: [PATCH 05/83] Add brute force solution --- DIRECTORY.md | 2 +- ...customer.rb => richest_customer_wealth.rb} | 29 +++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) rename data_structures/arrays/{richest_customer.rb => richest_customer_wealth.rb} (63%) diff --git a/DIRECTORY.md b/DIRECTORY.md index 910dd30..ea0a672 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -13,7 +13,7 @@ * [Fizz Buzz](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/fizz_buzz.rb) * [Get Products Of All Other Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/get_products_of_all_other_elements.rb) * [Remove Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/remove_elements.rb) - * [Richest Customer](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/richest_customer.rb) + * [Richest Customer Wealth](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/richest_customer.rb) * [Single Number](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/single_number.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) diff --git a/data_structures/arrays/richest_customer.rb b/data_structures/arrays/richest_customer_wealth.rb similarity index 63% rename from data_structures/arrays/richest_customer.rb rename to data_structures/arrays/richest_customer_wealth.rb index 8fc8173..23cf3de 100644 --- a/data_structures/arrays/richest_customer.rb +++ b/data_structures/arrays/richest_customer_wealth.rb @@ -1,4 +1,4 @@ -# Challenge name: Richest Customer +# Challenge name: Richest Customer Wealth # # You are given an m x n integer grid accounts where accounts[i][j] # is the amount of money the i​​​​​​​​​​​th​​​​ customer has in the j​​​​​​​​​​​th​​​​ bank. @@ -28,4 +28,29 @@ # # Example 3: # Input: accounts = [[2,8,7],[7,1,3],[1,9,5]] -# Output: 17 \ No newline at end of file +# Output: 17 + +# +# Approach 1: Brute Force +# +# Time Complexity: O(n) +# +def find_richest_customer_wealth(accounts) + summed_accounts = [] + accounts.each do |customer| + summed = 0 + customer.each do |account| + summed += account + end + summed_accounts.push(summed) + end + + summed_accounts.sort.pop() +end + +puts find_richest_customer_wealth([[1,2,3],[3,2,1]]) +# => 6 +puts find_richest_customer_wealth([[1,5],[7,3],[3,5]]) +# => 10 +puts find_richest_customer_wealth([[2,8,7],[7,1,3],[1,9,5]]) +# => 17 \ No newline at end of file From 5e7efbb73d0e3f2c5973013d0f0a47b77c913cf2 Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Thu, 25 Mar 2021 14:48:00 -0700 Subject: [PATCH 06/83] Add hash solution --- .../arrays/richest_customer_wealth.rb | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/data_structures/arrays/richest_customer_wealth.rb b/data_structures/arrays/richest_customer_wealth.rb index 23cf3de..5842348 100644 --- a/data_structures/arrays/richest_customer_wealth.rb +++ b/data_structures/arrays/richest_customer_wealth.rb @@ -53,4 +53,32 @@ puts find_richest_customer_wealth([[1,2,3],[3,2,1]]) puts find_richest_customer_wealth([[1,5],[7,3],[3,5]]) # => 10 puts find_richest_customer_wealth([[2,8,7],[7,1,3],[1,9,5]]) -# => 17 \ No newline at end of file +# => 17 + +# +# Approach 2: Hash +# +# Time Complexity: +# +def find_richest_customer_wealth(accounts) + result_hash = {} + accounts.each_with_index do |customer, i| + result_hash[i] = customer.sum + end + + highest_value = 0 + result_hash.each do |k, v| + if v > highest_value + highest_value = v + end + end + + highest_value +end + +puts find_richest_customer_wealth([[1,2,3],[3,2,1]]) +# => 6 +puts find_richest_customer_wealth([[1,5],[7,3],[3,5]]) +# => 10 +puts find_richest_customer_wealth([[2,8,7],[7,1,3],[1,9,5]]) +# => 17 From b4806f090372d4e2fadb66d3a5bb26eae031b02f Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Fri, 26 Mar 2021 09:48:52 -0700 Subject: [PATCH 07/83] Add remove vowels challenge --- DIRECTORY.md | 1 + data_structures/arrays/remove_vowels.rb | 29 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 data_structures/arrays/remove_vowels.rb diff --git a/DIRECTORY.md b/DIRECTORY.md index 8fea99b..4c10a66 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -14,6 +14,7 @@ * [Get Products Of All Other Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/get_products_of_all_other_elements.rb) * [Jewels And Stones](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/jewels_and_stones.rb) * [Remove Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/remove_elements.rb) + * [Remove Vowels](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/remove_vowels.rb) * [Single Number](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/single_number.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) diff --git a/data_structures/arrays/remove_vowels.rb b/data_structures/arrays/remove_vowels.rb new file mode 100644 index 0000000..d1dbabf --- /dev/null +++ b/data_structures/arrays/remove_vowels.rb @@ -0,0 +1,29 @@ +# Challenge name: Remove vowels from a string +# +# Given a string s, remove the vowels 'a', 'e', 'i', 'o', and 'u' +# from it, and return the new string. +# Example 1: +# Input: s = "leetcodeisacommunityforcoders" +# Output: "ltcdscmmntyfrcdrs" +# +# Example 2: +# Input: s = "aeiou" +# Output: "" +# +# @param {String} s +# @return {String} + +# +# Approach 1: +# +# Time Complexity: +# +def remove_vowels(s) +end + +s = 'leetcodeisacommunityforcoders' +print(remove_vowels(s)) +# => "ltcdscmmntyfrcdrs" +s = 'aeiou' +print(remove_vowels(s)) +# => "" \ No newline at end of file From 8bafd5c74a329a86fd7f29954fb56fcb335d1a3c Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Fri, 26 Mar 2021 10:03:45 -0700 Subject: [PATCH 08/83] Add brute force solution --- data_structures/arrays/remove_vowels.rb | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/data_structures/arrays/remove_vowels.rb b/data_structures/arrays/remove_vowels.rb index d1dbabf..c5c7af8 100644 --- a/data_structures/arrays/remove_vowels.rb +++ b/data_structures/arrays/remove_vowels.rb @@ -14,11 +14,22 @@ # @return {String} # -# Approach 1: +# Approach 1: Brute Force # -# Time Complexity: +# Time Complexity: O(n) # def remove_vowels(s) + result_array = [] + s.downcase + start_array = s.split('') + + start_array.each do |letter| + if letter != 'a' && letter != 'e' && letter != 'i' && letter != 'o' && letter != 'u' + result_array.push(letter) + end + end + + result_array.join('') end s = 'leetcodeisacommunityforcoders' From 27abece8ad34ba84d0d353e65e8d137e229da498 Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Fri, 26 Mar 2021 10:19:29 -0700 Subject: [PATCH 09/83] Add solution using regex --- data_structures/arrays/remove_vowels.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/data_structures/arrays/remove_vowels.rb b/data_structures/arrays/remove_vowels.rb index c5c7af8..d9b800f 100644 --- a/data_structures/arrays/remove_vowels.rb +++ b/data_structures/arrays/remove_vowels.rb @@ -32,6 +32,26 @@ def remove_vowels(s) result_array.join('') end +# s = 'leetcodeisacommunityforcoders' +# print(remove_vowels(s)) +# # => "ltcdscmmntyfrcdrs" +# s = 'aeiou' +# print(remove_vowels(s)) +# # => "" + +# +# Approach 2: Regex +# +# Time Complexity: O(n) +# +def remove_vowels(s) + vowels = /[aeiou]+/ + s.scan(vowels).each do |letter| + s.sub!(letter, '') + end + s +end + s = 'leetcodeisacommunityforcoders' print(remove_vowels(s)) # => "ltcdscmmntyfrcdrs" From 89a7af33e1a506f2f04e51808920a9b2f1ea27cb Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Fri, 26 Mar 2021 10:34:28 -0700 Subject: [PATCH 10/83] Add solution using Ruby .delete() method --- data_structures/arrays/remove_vowels.rb | 34 +++++++++++++++++-------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/data_structures/arrays/remove_vowels.rb b/data_structures/arrays/remove_vowels.rb index d9b800f..d8ddc31 100644 --- a/data_structures/arrays/remove_vowels.rb +++ b/data_structures/arrays/remove_vowels.rb @@ -32,12 +32,12 @@ def remove_vowels(s) result_array.join('') end -# s = 'leetcodeisacommunityforcoders' -# print(remove_vowels(s)) -# # => "ltcdscmmntyfrcdrs" -# s = 'aeiou' -# print(remove_vowels(s)) -# # => "" +s = 'leetcodeisacommunityforcoders' +print(remove_vowels(s)) +# => "ltcdscmmntyfrcdrs" +s = 'aeiou' +print(remove_vowels(s)) +# => "" # # Approach 2: Regex @@ -45,13 +45,27 @@ end # Time Complexity: O(n) # def remove_vowels(s) - vowels = /[aeiou]+/ - s.scan(vowels).each do |letter| - s.sub!(letter, '') - end + vowels = /[aeiou]/i + s.gsub!(vowels, '') s end +s = 'leetcodeisacommunityforcoders' +print(remove_vowels(s)) +# => "ltcdscmmntyfrcdrs" +s = 'aeiou' +print(remove_vowels(s)) +# => "" + +# +# Approach 3: Using Ruby .delete() method +# +# Time Complexity: O(n) +# +def remove_vowels(s) + s.downcase.delete('aeiou') +end + s = 'leetcodeisacommunityforcoders' print(remove_vowels(s)) # => "ltcdscmmntyfrcdrs" From 1e4d510748582d219d670a29917702de9efc4805 Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Fri, 26 Mar 2021 10:43:21 -0700 Subject: [PATCH 11/83] Add anagram checker challenge --- DIRECTORY.md | 1 + data_structures/arrays/anagram_checker.rb | 31 +++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 data_structures/arrays/anagram_checker.rb diff --git a/DIRECTORY.md b/DIRECTORY.md index 8fea99b..fe034ed 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -8,6 +8,7 @@ ## Data Structures * Arrays * [Add Digits](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/add_digits.rb) + * [Anagram Checker](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/anagram_checker.rb) * [Find All Duplicates In An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/find_all_duplicates_in_an_array.rb) * [Find The Highest Altitude](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/find_the_highest_altitude.rb) * [Fizz Buzz](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/fizz_buzz.rb) diff --git a/data_structures/arrays/anagram_checker.rb b/data_structures/arrays/anagram_checker.rb new file mode 100644 index 0000000..d082a36 --- /dev/null +++ b/data_structures/arrays/anagram_checker.rb @@ -0,0 +1,31 @@ +# Challenge name: Is anagram +# +# Given two strings s and t , write a function to determine +# if t is an anagram of s. +# +# Note: +# You may assume the string contains only lowercase alphabets. +# +# Follow up: +# What if the inputs contain unicode characters? +# How would you adapt your solution to such case? +# +# @param {String} s +# @param {String} t +# @return {Boolean} + +def is_anagram(s, t) +end + +s = 'anagram' +t = 'nagaram' +puts(is_anagram(s, t)) +# => true +s = 'rat' +t = 'car' +puts(is_anagram(s, t)) +# => false +s = 'a' +t = 'ab' +puts(is_anagram(s, t)) +# => false \ No newline at end of file From 08e35de175bd75ca2d285c36e5b65ed409510a99 Mon Sep 17 00:00:00 2001 From: Jessica Kwok <60627484+jsca-kwok@users.noreply.github.com> Date: Fri, 26 Mar 2021 10:54:37 -0700 Subject: [PATCH 12/83] Update data_structures/arrays/richest_customer_wealth.rb Co-authored-by: Vitor Oliveira --- data_structures/arrays/richest_customer_wealth.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/arrays/richest_customer_wealth.rb b/data_structures/arrays/richest_customer_wealth.rb index 5842348..7680ec9 100644 --- a/data_structures/arrays/richest_customer_wealth.rb +++ b/data_structures/arrays/richest_customer_wealth.rb @@ -58,7 +58,7 @@ puts find_richest_customer_wealth([[2,8,7],[7,1,3],[1,9,5]]) # # Approach 2: Hash # -# Time Complexity: +# Time Complexity: O(n) # def find_richest_customer_wealth(accounts) result_hash = {} From 6a80485c87a462f589fe55c91109544462d175e7 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 28 Mar 2021 17:19:48 +0000 Subject: [PATCH 13/83] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 8fea99b..dbb7695 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -56,6 +56,7 @@ * [Fibonacci](https://github.com/TheAlgorithms/Ruby/blob/master/maths/fibonacci.rb) * [Number Of Digits](https://github.com/TheAlgorithms/Ruby/blob/master/maths/number_of_digits.rb) * [Power Of Two](https://github.com/TheAlgorithms/Ruby/blob/master/maths/power_of_two.rb) + * [Prime Number](https://github.com/TheAlgorithms/Ruby/blob/master/maths/prime_number.rb) * [Square Root](https://github.com/TheAlgorithms/Ruby/blob/master/maths/square_root.rb) * [Square Root Test](https://github.com/TheAlgorithms/Ruby/blob/master/maths/square_root_test.rb) * [Sum Of Digits](https://github.com/TheAlgorithms/Ruby/blob/master/maths/sum_of_digits.rb) From 5ed65199d74cb46fd2c187d4534f70ea313b6216 Mon Sep 17 00:00:00 2001 From: Jessica Kwok <60627484+jsca-kwok@users.noreply.github.com> Date: Mon, 29 Mar 2021 09:59:32 -0700 Subject: [PATCH 14/83] Update data_structures/arrays/remove_vowels.rb Co-authored-by: vzvu3k6k --- data_structures/arrays/remove_vowels.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/arrays/remove_vowels.rb b/data_structures/arrays/remove_vowels.rb index d8ddc31..7a72f87 100644 --- a/data_structures/arrays/remove_vowels.rb +++ b/data_structures/arrays/remove_vowels.rb @@ -20,7 +20,7 @@ # def remove_vowels(s) result_array = [] - s.downcase + s.downcase! start_array = s.split('') start_array.each do |letter| @@ -71,4 +71,4 @@ print(remove_vowels(s)) # => "ltcdscmmntyfrcdrs" s = 'aeiou' print(remove_vowels(s)) -# => "" \ No newline at end of file +# => "" From 5bf141f64f65e0286d4a0ac3b2760cc13dceccf1 Mon Sep 17 00:00:00 2001 From: Jessica Kwok <60627484+jsca-kwok@users.noreply.github.com> Date: Mon, 29 Mar 2021 10:00:33 -0700 Subject: [PATCH 15/83] Update data_structures/arrays/remove_vowels.rb Co-authored-by: vzvu3k6k --- data_structures/arrays/remove_vowels.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/arrays/remove_vowels.rb b/data_structures/arrays/remove_vowels.rb index 7a72f87..bd6da25 100644 --- a/data_structures/arrays/remove_vowels.rb +++ b/data_structures/arrays/remove_vowels.rb @@ -33,7 +33,7 @@ def remove_vowels(s) end s = 'leetcodeisacommunityforcoders' -print(remove_vowels(s)) +puts(remove_vowels(s)) # => "ltcdscmmntyfrcdrs" s = 'aeiou' print(remove_vowels(s)) From 31d9e8e0a22aefc791a912f14e145a2abd7257c0 Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Mon, 29 Mar 2021 10:05:15 -0700 Subject: [PATCH 16/83] Change print to puts --- data_structures/arrays/remove_vowels.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/data_structures/arrays/remove_vowels.rb b/data_structures/arrays/remove_vowels.rb index bd6da25..5fa39fc 100644 --- a/data_structures/arrays/remove_vowels.rb +++ b/data_structures/arrays/remove_vowels.rb @@ -36,7 +36,7 @@ s = 'leetcodeisacommunityforcoders' puts(remove_vowels(s)) # => "ltcdscmmntyfrcdrs" s = 'aeiou' -print(remove_vowels(s)) +puts(remove_vowels(s)) # => "" # @@ -51,10 +51,10 @@ def remove_vowels(s) end s = 'leetcodeisacommunityforcoders' -print(remove_vowels(s)) +puts(remove_vowels(s)) # => "ltcdscmmntyfrcdrs" s = 'aeiou' -print(remove_vowels(s)) +puts(remove_vowels(s)) # => "" # @@ -67,8 +67,8 @@ def remove_vowels(s) end s = 'leetcodeisacommunityforcoders' -print(remove_vowels(s)) +puts(remove_vowels(s)) # => "ltcdscmmntyfrcdrs" s = 'aeiou' -print(remove_vowels(s)) +puts(remove_vowels(s)) # => "" From ad685ac4ee922161ba193a13050e38f3f050825d Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Mon, 29 Mar 2021 10:22:09 -0700 Subject: [PATCH 17/83] Add two pointers approach --- data_structures/arrays/shuffle_array.rb | 57 +++++++++++++++++++------ 1 file changed, 45 insertions(+), 12 deletions(-) diff --git a/data_structures/arrays/shuffle_array.rb b/data_structures/arrays/shuffle_array.rb index 2f1b13f..22ec07a 100644 --- a/data_structures/arrays/shuffle_array.rb +++ b/data_structures/arrays/shuffle_array.rb @@ -34,18 +34,18 @@ def shuffle(nums, n) result end -# nums = [2, 5, 1, 3, 4, 7] -# n = 3 -# print(shuffle(nums, n)) -# # Output: [2,3,5,4,1,7] -# nums = [1, 2, 3, 4, 4, 3, 2, 1] -# n = 4 -# print(shuffle(nums, n)) -# # Output: [1,4,2,3,3,2,4,1] -# nums = [1, 1, 2, 2] -# n = 2 -# print(shuffle(nums, n)) -# # Output: [1,2,1,2] +nums = [2, 5, 1, 3, 4, 7] +n = 3 +print(shuffle(nums, n)) +# Output: [2,3,5,4,1,7] +nums = [1, 2, 3, 4, 4, 3, 2, 1] +n = 4 +print(shuffle(nums, n)) +# Output: [1,4,2,3,3,2,4,1] +nums = [1, 1, 2, 2] +n = 2 +print(shuffle(nums, n)) +# Output: [1,2,1,2] # # Approach 2: Use Ruby methods .insert() and .delete_at() @@ -62,6 +62,39 @@ def shuffle(nums, n) nums end +nums = [2, 5, 1, 3, 4, 7] +n = 3 +print(shuffle(nums, n)) +# Output: [2,3,5,4,1,7] +nums = [1, 2, 3, 4, 4, 3, 2, 1] +n = 4 +print(shuffle(nums, n)) +# Output: [1,4,2,3,3,2,4,1] +nums = [1, 1, 2, 2] +n = 2 +print(shuffle(nums, n)) +# Output: [1,2,1,2] + +# +# Approach 3: Two Pointers +# +# Time Complexity: O(N) +# + +def shuffle(nums, n) + result = [] + p1 = 0 + p2 = n + + while p1 < n + result.push(nums[p1], nums[p2]) + p1 +=1 + p2 +=1 + end + + result +end + nums = [2, 5, 1, 3, 4, 7] n = 3 print(shuffle(nums, n)) From fded3fa4df2090ed3ae0d85207a209d59d0da2af Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Mon, 29 Mar 2021 14:33:12 -0700 Subject: [PATCH 18/83] Move approach to hash_table folder --- .../arrays/richest_customer_wealth.rb | 30 +--------- .../hash_table/richest_customer_wealth.rb | 59 +++++++++++++++++++ 2 files changed, 60 insertions(+), 29 deletions(-) create mode 100644 data_structures/hash_table/richest_customer_wealth.rb diff --git a/data_structures/arrays/richest_customer_wealth.rb b/data_structures/arrays/richest_customer_wealth.rb index 7680ec9..4efabeb 100644 --- a/data_structures/arrays/richest_customer_wealth.rb +++ b/data_structures/arrays/richest_customer_wealth.rb @@ -31,7 +31,7 @@ # Output: 17 # -# Approach 1: Brute Force +# Approach: Brute Force # # Time Complexity: O(n) # @@ -54,31 +54,3 @@ puts find_richest_customer_wealth([[1,5],[7,3],[3,5]]) # => 10 puts find_richest_customer_wealth([[2,8,7],[7,1,3],[1,9,5]]) # => 17 - -# -# Approach 2: Hash -# -# Time Complexity: O(n) -# -def find_richest_customer_wealth(accounts) - result_hash = {} - accounts.each_with_index do |customer, i| - result_hash[i] = customer.sum - end - - highest_value = 0 - result_hash.each do |k, v| - if v > highest_value - highest_value = v - end - end - - highest_value -end - -puts find_richest_customer_wealth([[1,2,3],[3,2,1]]) -# => 6 -puts find_richest_customer_wealth([[1,5],[7,3],[3,5]]) -# => 10 -puts find_richest_customer_wealth([[2,8,7],[7,1,3],[1,9,5]]) -# => 17 diff --git a/data_structures/hash_table/richest_customer_wealth.rb b/data_structures/hash_table/richest_customer_wealth.rb new file mode 100644 index 0000000..11849e1 --- /dev/null +++ b/data_structures/hash_table/richest_customer_wealth.rb @@ -0,0 +1,59 @@ +# Challenge name: Richest Customer Wealth +# +# You are given an m x n integer grid accounts where accounts[i][j] +# is the amount of money the i​​​​​​​​​​​th​​​​ customer has in the j​​​​​​​​​​​th​​​​ bank. +# +# Return the wealth that the richest customer has. +# A customer's wealth is the amount of money they have in all +# their bank accounts. The richest customer is the customer that +# has the maximum wealth. +# +# Example 1: +# Input: accounts = [[1,2,3],[3,2,1]] +# Output: 6 +# Explanation: +# 1st customer has wealth = 1 + 2 + 3 = 6 +# 2nd customer has wealth = 3 + 2 + 1 = 6 +# Both customers are considered the richest with a wealth of 6 +# each, so return 6. +# +# Example 2: +# Input: accounts = [[1,5],[7,3],[3,5]] +# Output: 10 +# Explanation: +# 1st customer has wealth = 6 +# 2nd customer has wealth = 10 +# 3rd customer has wealth = 8 +# The 2nd customer is the richest with a wealth of 10. +# +# Example 3: +# Input: accounts = [[2,8,7],[7,1,3],[1,9,5]] +# Output: 17 + +# +# Approach: Hash +# +# Time Complexity: O(n) +# +def find_richest_customer_wealth(accounts) + result_hash = {} + accounts.each_with_index do |customer, i| + result_hash[i] = customer.sum + end + + highest_value = 0 + result_hash.each do |k, v| + if v > highest_value + highest_value = v + end + end + + highest_value +end + +puts find_richest_customer_wealth([[1,2,3],[3,2,1]]) +# => 6 +puts find_richest_customer_wealth([[1,5],[7,3],[3,5]]) +# => 10 +puts find_richest_customer_wealth([[2,8,7],[7,1,3],[1,9,5]]) +# => 17 From c0f28a73812b4a3f9b1576073486d4a58fb93a3f Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 29 Mar 2021 21:34:30 +0000 Subject: [PATCH 19/83] updating DIRECTORY.md --- DIRECTORY.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 361854e..059eebf 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -14,7 +14,7 @@ * [Get Products Of All Other Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/get_products_of_all_other_elements.rb) * [Jewels And Stones](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/jewels_and_stones.rb) * [Remove Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/remove_elements.rb) - * [Richest Customer Wealth](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/richest_customer.rb) + * [Richest Customer Wealth](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/richest_customer_wealth.rb) * [Single Number](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/single_number.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) @@ -24,6 +24,8 @@ * [Invert](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/binary_trees/invert.rb) * [Postorder Traversal](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/binary_trees/postorder_traversal.rb) * [Preorder Traversal](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/binary_trees/preorder_traversal.rb) + * Hash Table + * [Richest Customer Wealth](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/richest_customer_wealth.rb) * Linked Lists * [Circular Linked List](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/linked_lists/circular_linked_list.rb) * [Doubly Linked List](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/linked_lists/doubly_linked_list.rb) From c94b74543f893cac6a311ebf8fbf38302433f7cd Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 29 Mar 2021 21:37:05 +0000 Subject: [PATCH 20/83] updating DIRECTORY.md --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 488e66b..698ebfc 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -15,8 +15,8 @@ * [Jewels And Stones](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/jewels_and_stones.rb) * [Remove Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/remove_elements.rb) * [Richest Customer Wealth](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/richest_customer_wealth.rb) - * [Single Number](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/single_number.rb) * [Shuffle Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/shuffle_array.rb) + * [Single Number](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/single_number.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) From 3270667f8edc81b2d08d2058a4d35febb377f6e6 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Mon, 29 Mar 2021 14:52:06 -0700 Subject: [PATCH 21/83] Move algorithsm to appropriate folder --- data_structures/arrays/{ => strings}/remove_vowels.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) rename data_structures/arrays/{ => strings}/remove_vowels.rb (99%) diff --git a/data_structures/arrays/remove_vowels.rb b/data_structures/arrays/strings/remove_vowels.rb similarity index 99% rename from data_structures/arrays/remove_vowels.rb rename to data_structures/arrays/strings/remove_vowels.rb index 5fa39fc..30d3042 100644 --- a/data_structures/arrays/remove_vowels.rb +++ b/data_structures/arrays/strings/remove_vowels.rb @@ -2,6 +2,7 @@ # # Given a string s, remove the vowels 'a', 'e', 'i', 'o', and 'u' # from it, and return the new string. +# # Example 1: # Input: s = "leetcodeisacommunityforcoders" # Output: "ltcdscmmntyfrcdrs" @@ -18,6 +19,7 @@ # # Time Complexity: O(n) # + def remove_vowels(s) result_array = [] s.downcase! @@ -57,7 +59,7 @@ s = 'aeiou' puts(remove_vowels(s)) # => "" -# +# # Approach 3: Using Ruby .delete() method # # Time Complexity: O(n) From fb1ee12e8d66b3ee00617eec0689918f82f32c23 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 29 Mar 2021 21:54:54 +0000 Subject: [PATCH 22/83] updating DIRECTORY.md --- DIRECTORY.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 2d90daf..d1baadc 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -14,11 +14,12 @@ * [Get Products Of All Other Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/get_products_of_all_other_elements.rb) * [Jewels And Stones](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/jewels_and_stones.rb) * [Remove Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/remove_elements.rb) - * [Remove Vowels](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/remove_vowels.rb) * [Richest Customer Wealth](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/richest_customer_wealth.rb) * [Shuffle Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/shuffle_array.rb) * [Single Number](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/single_number.rb) * [Sort Squares Of An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/sort_squares_of_an_array.rb) + * Strings + * [Remove Vowels](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/remove_vowels.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) * Binary Trees From 66756bf5d43c1ecbc04e2aa3b4ddff1e61a918c3 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Mon, 29 Mar 2021 15:33:57 -0700 Subject: [PATCH 23/83] Move remove_vowels to strings folder --- data_structures/arrays/{ => strings}/jewels_and_stones.rb | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename data_structures/arrays/{ => strings}/jewels_and_stones.rb (100%) diff --git a/data_structures/arrays/jewels_and_stones.rb b/data_structures/arrays/strings/jewels_and_stones.rb similarity index 100% rename from data_structures/arrays/jewels_and_stones.rb rename to data_structures/arrays/strings/jewels_and_stones.rb From 57a46a8271c8be49b199142fc0efa589a9bf6702 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Mon, 29 Mar 2021 15:35:18 -0700 Subject: [PATCH 24/83] Move two_sum to hash table folder --- data_structures/arrays/two_sum.rb | 45 ----------------- data_structures/hash_table/two_sum.rb | 70 +++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 45 deletions(-) create mode 100644 data_structures/hash_table/two_sum.rb diff --git a/data_structures/arrays/two_sum.rb b/data_structures/arrays/two_sum.rb index b872625..a08eefd 100644 --- a/data_structures/arrays/two_sum.rb +++ b/data_structures/arrays/two_sum.rb @@ -86,48 +86,3 @@ print(two_sum([3, 2, 4], 6)) print(two_sum([3, 3], 6)) # => [0,1] - -# -# Approach 3: Using a Hash -# - -# Complexity analysis - -# Time complexity: O(n). We traverse the list containing n elements exactly twice. -# Since the hash table reduces the lookup time to O(1), the time complexity is O(n). - -# Space complexity: O(n). The extra space required depends on the number of items -# stored in the hash table, which stores exactly n elements. - -def two_sum(nums, target) - hash = {} - - # create a hash to store values and their indices - nums.each_with_index do |num, i| - hash[num] = i - end - - # iterate over nums array to find the target (difference between sum target and num) - nums.each_with_index do |num, i| - difference_target = target - num - - if hash[difference_target] && hash[difference_target] != i - return [i, hash[difference_target]] - end - end -end - -nums = [2, 7, 11, 15] -target = 9 -print(two_sum(nums, target)) -# => [0,1] - -nums = [3, 2, 4] -target = 6 -print(two_sum(nums, target)) -# => [1,2] - -nums = [3, 3] -target = 6 -print(two_sum(nums, target)) -# => [0,1] diff --git a/data_structures/hash_table/two_sum.rb b/data_structures/hash_table/two_sum.rb new file mode 100644 index 0000000..14dafcf --- /dev/null +++ b/data_structures/hash_table/two_sum.rb @@ -0,0 +1,70 @@ +# Challenge name: Two Sum +# +# Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. +# +# You may assume that each input would have exactly one solution, and you may not use the same element twice. +# +# You can return the answer in any order. +# +# +# Examples +# +# Input: nums = [2, 7, 11, 15], target = 9 +# Output: [0,1] +# Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]. +# +# Input: nums = [3, 2, 4], target = 6 +# Output: [1,2] +# +# Input: nums = [3, 3], target = 6 +# Output: [0,1] +# Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]. +# +# @param {Integer[]} nums +# @param {Integer} target +# @return {Integer[]} + +# +# Approach: Using Hash table +# + +# Complexity analysis + +# Time complexity: O(n). We traverse the list containing n elements exactly twice. +# Since the hash table reduces the lookup time to O(1), the time complexity is O(n). + +# Space complexity: O(n). The extra space required depends on the number of items +# stored in the hash table, which stores exactly n elements. + +def two_sum(nums, target) + hash = {} + + # create a hash to store values and their indices + nums.each_with_index do |num, i| + hash[num] = i + end + + # iterate over nums array to find the target (difference between sum target and num) + nums.each_with_index do |num, i| + difference_target = target - num + + if hash[difference_target] && hash[difference_target] != i + return [i, hash[difference_target]] + end + end +end + +nums = [2, 7, 11, 15] +target = 9 +print(two_sum(nums, target)) +# => [0,1] + +nums = [3, 2, 4] +target = 6 +print(two_sum(nums, target)) +# => [1,2] + +nums = [3, 3] +target = 6 +print(two_sum(nums, target)) +# => [0,1] From f6a84ea326f626869b7876fff5f564b2f49a6de1 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Mon, 29 Mar 2021 15:37:02 -0700 Subject: [PATCH 25/83] Move more algos to hash table folders --- .../arrays/find_all_duplicates_in_an_array.rb | 41 ------------- .../find_all_duplicates_in_an_array.rb | 61 +++++++++++++++++++ 2 files changed, 61 insertions(+), 41 deletions(-) create mode 100644 data_structures/hash_table/find_all_duplicates_in_an_array.rb diff --git a/data_structures/arrays/find_all_duplicates_in_an_array.rb b/data_structures/arrays/find_all_duplicates_in_an_array.rb index 63e6517..7ee99ff 100644 --- a/data_structures/arrays/find_all_duplicates_in_an_array.rb +++ b/data_structures/arrays/find_all_duplicates_in_an_array.rb @@ -87,44 +87,3 @@ Benchmark.bmbm do |x| print(find_duplicates(long_array)) end end - -# -# Approach 3: Hash map -# - -# -# Complexity Analysis -# -# Time complexity: O(n) average case. -# - -def find_duplicates(array) - result_hash = {} - result_array = [] - - # loop through array and build a hash with counters - # where the key is the array element and the counter is the value - # increase counter when duplicate is found - array.each do |num| - if result_hash[num].nil? - result_hash[num] = 1 - else - result_hash[num] += 1 - end - end - - # loop through hash and look for values > 1 - result_hash.each do |k, v| - result_array.push(k) if v > 1 - end - - # return keys - result_array -end - -Benchmark.bmbm do |x| - x.report('execute algorithm 3') do - print(find_duplicates(array)) - print(find_duplicates(long_array)) - end -end diff --git a/data_structures/hash_table/find_all_duplicates_in_an_array.rb b/data_structures/hash_table/find_all_duplicates_in_an_array.rb new file mode 100644 index 0000000..bc68423 --- /dev/null +++ b/data_structures/hash_table/find_all_duplicates_in_an_array.rb @@ -0,0 +1,61 @@ +# Find All Duplicates in an Array +# +# Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), +# some elements appear twice and others appear once. +# +# Find all the elements that appear twice in this array. +# +# Could you do it without extra space and in O(n) runtime? +# +# Example: +# Input: +# [4,3,2,7,8,2,3,1] +# +# Output: +# [2,3] + +require 'benchmark' + +array = [4, 3, 2, 7, 8, 2, 3, 1] +long_array = [4, 3, 2, 7, 8, 2, 3, 1] * 100 + +# +# Approach: Hash table +# + +# +# Complexity Analysis +# +# Time complexity: O(n) average case. +# + +def find_duplicates(array) + result_hash = {} + result_array = [] + + # loop through array and build a hash with counters + # where the key is the array element and the counter is the value + # increase counter when duplicate is found + array.each do |num| + if result_hash[num].nil? + result_hash[num] = 1 + else + result_hash[num] += 1 + end + end + + # loop through hash and look for values > 1 + result_hash.each do |k, v| + result_array.push(k) if v > 1 + end + + # return keys + result_array +end + +Benchmark.bmbm do |x| + x.report('execute algorithm 3') do + print(find_duplicates(array)) + print(find_duplicates(long_array)) + end +end From e60cd1b5e66b6425820cf3577c4cf8bf2b6928b1 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 29 Mar 2021 22:37:23 +0000 Subject: [PATCH 26/83] updating DIRECTORY.md --- DIRECTORY.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index d1baadc..03f2942 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -12,13 +12,13 @@ * [Find The Highest Altitude](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/find_the_highest_altitude.rb) * [Fizz Buzz](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/fizz_buzz.rb) * [Get Products Of All Other Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/get_products_of_all_other_elements.rb) - * [Jewels And Stones](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/jewels_and_stones.rb) * [Remove Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/remove_elements.rb) * [Richest Customer Wealth](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/richest_customer_wealth.rb) * [Shuffle Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/shuffle_array.rb) * [Single Number](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/single_number.rb) * [Sort Squares Of An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/sort_squares_of_an_array.rb) * Strings + * [Jewels And Stones](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/jewels_and_stones.rb) * [Remove Vowels](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/remove_vowels.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) @@ -28,7 +28,9 @@ * [Postorder Traversal](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/binary_trees/postorder_traversal.rb) * [Preorder Traversal](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/binary_trees/preorder_traversal.rb) * Hash Table + * [Find All Duplicates In An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/find_all_duplicates_in_an_array.rb) * [Richest Customer Wealth](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/richest_customer_wealth.rb) + * [Two Sum](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/two_sum.rb) * Linked Lists * [Circular Linked List](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/linked_lists/circular_linked_list.rb) * [Doubly Linked List](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/linked_lists/doubly_linked_list.rb) From 6c785524fbad57ffb26d8858fdfb2f348a4fd296 Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Tue, 30 Mar 2021 14:19:43 -0700 Subject: [PATCH 27/83] Add sort and compare solution --- DIRECTORY.md | 2 +- .../arrays/{ => strings}/anagram_checker.rb | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) rename data_structures/arrays/{ => strings}/anagram_checker.rb (76%) diff --git a/DIRECTORY.md b/DIRECTORY.md index 3ae4c04..0014e38 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -8,7 +8,6 @@ ## Data Structures * Arrays * [Add Digits](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/add_digits.rb) - * [Anagram Checker](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/anagram_checker.rb) * [Find All Duplicates In An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/find_all_duplicates_in_an_array.rb) * [Find The Highest Altitude](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/find_the_highest_altitude.rb) * [Fizz Buzz](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/fizz_buzz.rb) @@ -19,6 +18,7 @@ * [Single Number](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/single_number.rb) * [Sort Squares Of An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/sort_squares_of_an_array.rb) * Strings + * [Anagram Checker](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/anagram_checker.rb) * [Jewels And Stones](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/jewels_and_stones.rb) * [Remove Vowels](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/remove_vowels.rb) * [Two Sum](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/two_sum.rb) diff --git a/data_structures/arrays/anagram_checker.rb b/data_structures/arrays/strings/anagram_checker.rb similarity index 76% rename from data_structures/arrays/anagram_checker.rb rename to data_structures/arrays/strings/anagram_checker.rb index d082a36..b66eb3f 100644 --- a/data_structures/arrays/anagram_checker.rb +++ b/data_structures/arrays/strings/anagram_checker.rb @@ -14,7 +14,18 @@ # @param {String} t # @return {Boolean} +# +# Approach 1: Sort and Compare +# +# Time Complexity: O(n log n) +# def is_anagram(s, t) + return false if s.length != t.length + + arr1 = s.split('').sort + arr2 = t.split('').sort + + arr1 == arr2 end s = 'anagram' From 2fe1098805de6fb91f5b567484f01d3f90ae720a Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Wed, 31 Mar 2021 16:40:23 -0700 Subject: [PATCH 28/83] Add intersection challenge --- DIRECTORY.md | 1 + data_structures/arrays/intersection.rb | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 data_structures/arrays/intersection.rb diff --git a/DIRECTORY.md b/DIRECTORY.md index 03f2942..878bdcb 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -12,6 +12,7 @@ * [Find The Highest Altitude](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/find_the_highest_altitude.rb) * [Fizz Buzz](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/fizz_buzz.rb) * [Get Products Of All Other Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/get_products_of_all_other_elements.rb) + * [Intersection](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/intersection.rb) * [Remove Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/remove_elements.rb) * [Richest Customer Wealth](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/richest_customer_wealth.rb) * [Shuffle Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/shuffle_array.rb) diff --git a/data_structures/arrays/intersection.rb b/data_structures/arrays/intersection.rb new file mode 100644 index 0000000..bd826a0 --- /dev/null +++ b/data_structures/arrays/intersection.rb @@ -0,0 +1,23 @@ +# Challenge name: Intersection of two arrays ii +# +# Given two arrays, write a function to compute their intersection. +# +# @param {Integer[]} nums1 +# @param {Integer[]} nums2 +# @return {Integer[]} + +# +# Approach 1: +# +# Time Complexity: +# +def intersect(arr1, arr2) +end +nums1 = [1, 2, 2, 1] +nums2 = [2, 2] +intersect(nums1, nums2) +# => [2,2] +nums1 = [4, 9, 5] +nums2 = [9, 4, 9, 8, 4] +intersect(nums1, nums2) +# => [4,9] \ No newline at end of file From 3bc81b8195b91a2282b7eb4b3fdc916364ec373f Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Wed, 31 Mar 2021 18:37:57 -0700 Subject: [PATCH 29/83] Update data_structures/arrays/strings/anagram_checker.rb --- data_structures/arrays/strings/anagram_checker.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/data_structures/arrays/strings/anagram_checker.rb b/data_structures/arrays/strings/anagram_checker.rb index b66eb3f..71f59d6 100644 --- a/data_structures/arrays/strings/anagram_checker.rb +++ b/data_structures/arrays/strings/anagram_checker.rb @@ -17,7 +17,9 @@ # # Approach 1: Sort and Compare # -# Time Complexity: O(n log n) +# Complexity analysis: +# +# Time Complexity: O(n log n). Assume that n is the length of s, sorting costs O(n log n), and comparing two strings costs O(n). Sorting time dominates and the overall time complexity is O(n log n). # def is_anagram(s, t) return false if s.length != t.length @@ -39,4 +41,4 @@ puts(is_anagram(s, t)) s = 'a' t = 'ab' puts(is_anagram(s, t)) -# => false \ No newline at end of file +# => false From a93d5cc072d98981331661186ad8650405dc6bde Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Wed, 31 Mar 2021 18:38:01 -0700 Subject: [PATCH 30/83] Update data_structures/arrays/strings/anagram_checker.rb --- data_structures/arrays/strings/anagram_checker.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/data_structures/arrays/strings/anagram_checker.rb b/data_structures/arrays/strings/anagram_checker.rb index 71f59d6..bcd5e36 100644 --- a/data_structures/arrays/strings/anagram_checker.rb +++ b/data_structures/arrays/strings/anagram_checker.rb @@ -20,6 +20,7 @@ # Complexity analysis: # # Time Complexity: O(n log n). Assume that n is the length of s, sorting costs O(n log n), and comparing two strings costs O(n). Sorting time dominates and the overall time complexity is O(n log n). +# Space Complexity: O(1). Space depends on the sorting implementation which, usually, costs O(1) auxiliary space if heapsort is used. # def is_anagram(s, t) return false if s.length != t.length From a472f9a6ffdd32cb41772b0dc14c969249b83a05 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Thu, 1 Apr 2021 01:57:23 +0000 Subject: [PATCH 31/83] updating DIRECTORY.md --- DIRECTORY.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 0014e38..d0c4469 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -5,6 +5,9 @@ ## Ciphers * [Merkle Hellman Cryptosystem](https://github.com/TheAlgorithms/Ruby/blob/master/ciphers/merkle_hellman_cryptosystem.rb) +## Conversions + * [Temperature Conversions](https://github.com/TheAlgorithms/Ruby/blob/master/conversions/temperature_conversions.rb) + ## Data Structures * Arrays * [Add Digits](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/add_digits.rb) From 4674c2d97b205f14a2b471d00c24afca4e44048f Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Wed, 31 Mar 2021 19:02:57 -0700 Subject: [PATCH 32/83] Valid anagram: hash table approach --- .../arrays/strings/anagram_checker.rb | 2 +- data_structures/hash_table/anagram_checker.rb | 55 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 data_structures/hash_table/anagram_checker.rb diff --git a/data_structures/arrays/strings/anagram_checker.rb b/data_structures/arrays/strings/anagram_checker.rb index bcd5e36..1918b07 100644 --- a/data_structures/arrays/strings/anagram_checker.rb +++ b/data_structures/arrays/strings/anagram_checker.rb @@ -15,7 +15,7 @@ # @return {Boolean} # -# Approach 1: Sort and Compare +# Approach: Sort and Compare # # Complexity analysis: # diff --git a/data_structures/hash_table/anagram_checker.rb b/data_structures/hash_table/anagram_checker.rb new file mode 100644 index 0000000..43163d4 --- /dev/null +++ b/data_structures/hash_table/anagram_checker.rb @@ -0,0 +1,55 @@ +# Challenge name: Is anagram +# +# Given two strings s and t , write a function to determine +# if t is an anagram of s. +# +# Note: +# You may assume the string contains only lowercase alphabets. +# +# Follow up: +# What if the inputs contain unicode characters? +# How would you adapt your solution to such case? +# +# @param {String} s +# @param {String} t +# @return {Boolean} + +# +# Approach: Hash table +# +# Complexity analysis: +# +# Time complexity: O(n). Time complexity is O(n) since accessing the counter table is a constant time operation. +# Space complexity: O(1). Although we do use extra space, the space complexity is O(1) because the table's size stays constant no matter how large n is. +# +def is_anagram(s, t) + s_length = s.length + t_length = t.length + counter = Hash.new(0) + + return false unless s_length == t_length + + (0...s_length).each do |i| + counter[s[i]] += 1 + counter[t[i]] -= 1 + end + + counter.each do |k, v| + return false unless v == 0 + end + + true +end + +s = 'anagram' +t = 'nagaram' +puts(is_anagram(s, t)) +# => true +s = 'rat' +t = 'car' +puts(is_anagram(s, t)) +# => false +s = 'a' +t = 'ab' +puts(is_anagram(s, t)) +# => false From 2da2ec55f6e32b1c418e44c6b2a4576ecfbfb54f Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Thu, 1 Apr 2021 02:03:14 +0000 Subject: [PATCH 33/83] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index d0c4469..42b9add 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -32,6 +32,7 @@ * [Postorder Traversal](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/binary_trees/postorder_traversal.rb) * [Preorder Traversal](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/binary_trees/preorder_traversal.rb) * Hash Table + * [Anagram Checker](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/anagram_checker.rb) * [Find All Duplicates In An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/find_all_duplicates_in_an_array.rb) * [Richest Customer Wealth](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/richest_customer_wealth.rb) * [Two Sum](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/two_sum.rb) From ad959fcfb037a205a42bfed1b496624e2faa05ad Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Wed, 31 Mar 2021 19:04:39 -0700 Subject: [PATCH 34/83] Minor changes --- data_structures/hash_table/anagram_checker.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/data_structures/hash_table/anagram_checker.rb b/data_structures/hash_table/anagram_checker.rb index 43163d4..03f72fa 100644 --- a/data_structures/hash_table/anagram_checker.rb +++ b/data_structures/hash_table/anagram_checker.rb @@ -19,8 +19,11 @@ # # Complexity analysis: # -# Time complexity: O(n). Time complexity is O(n) since accessing the counter table is a constant time operation. -# Space complexity: O(1). Although we do use extra space, the space complexity is O(1) because the table's size stays constant no matter how large n is. +# Time complexity: O(n). Time complexity is O(n) since accessing the counter +# table is a constant time operation. +# Space complexity: O(1). Although we do use extra space, +# the space complexity is O(1) because the table's size stays constant no +# matter how large n is. # def is_anagram(s, t) s_length = s.length @@ -45,10 +48,12 @@ s = 'anagram' t = 'nagaram' puts(is_anagram(s, t)) # => true + s = 'rat' t = 'car' puts(is_anagram(s, t)) # => false + s = 'a' t = 'ab' puts(is_anagram(s, t)) From 6b8529a1a698e7316cb809fd01c557f6b7726142 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Wed, 31 Mar 2021 19:05:51 -0700 Subject: [PATCH 35/83] Update anagram_checker.rb --- data_structures/hash_table/anagram_checker.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/data_structures/hash_table/anagram_checker.rb b/data_structures/hash_table/anagram_checker.rb index 03f72fa..1158134 100644 --- a/data_structures/hash_table/anagram_checker.rb +++ b/data_structures/hash_table/anagram_checker.rb @@ -17,6 +17,8 @@ # # Approach: Hash table # + +# # Complexity analysis: # # Time complexity: O(n). Time complexity is O(n) since accessing the counter From dc533283a2a3c5743fae1548210f8968fb7a8d08 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Thu, 1 Apr 2021 09:36:46 -0700 Subject: [PATCH 36/83] Add hash table approach --- data_structures/hash_table/anagram_checker.rb | 73 ++++++++++++++++++- 1 file changed, 71 insertions(+), 2 deletions(-) diff --git a/data_structures/hash_table/anagram_checker.rb b/data_structures/hash_table/anagram_checker.rb index 1158134..0d0f2f6 100644 --- a/data_structures/hash_table/anagram_checker.rb +++ b/data_structures/hash_table/anagram_checker.rb @@ -17,8 +17,6 @@ # # Approach: Hash table # - -# # Complexity analysis: # # Time complexity: O(n). Time complexity is O(n) since accessing the counter @@ -60,3 +58,74 @@ s = 'a' t = 'ab' puts(is_anagram(s, t)) # => false + + +def is_anagram(s, t) + s_length = s.length + t_length = t.length + counter = Hash.new(0) + + return false unless s_length == t_length + + (0...s_length).each do |i| + counter[s[i]] += 1 + end + + (0...s_length).each do |i| + counter[t[i]] -= 1 + + return false if counter[t[i]] < 0 + end + + true +end + +s = 'anagram' +t = 'nagaram' +puts(is_anagram(s, t)) +# => true + +s = 'rat' +t = 'car' +puts(is_anagram(s, t)) +# => false + +s = 'a' +t = 'ab' +puts(is_anagram(s, t)) +# => false + +# +# Approach 2: Hash table +# + +# Algorithm: we could also first increment the counter for s, +# then decrement the counter for t. If at any point the counter +# drops below zero, we know that t contains an extra letter, +# not in s, and return false immediately. + +# Complexity analysis: +# +# Time complexity: O(n). +# Space complexity: O(1). +# + +def is_anagram(s, t) + s_length = s.length + t_length = t.length + counter = Hash.new(0) + + return false unless s_length == t_length + + (0...s_length).each do |i| + counter[s[i]] += 1 + end + + (0...s_length).each do |i| + counter[t[i]] -= 1 + + return false if counter[t[i]] < 0 + end + + true +end From 44c32084541b2679161577a1257d1454d4f4752d Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Thu, 1 Apr 2021 09:36:52 -0700 Subject: [PATCH 37/83] Add another hash table approach --- data_structures/hash_table/anagram_checker.rb | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/data_structures/hash_table/anagram_checker.rb b/data_structures/hash_table/anagram_checker.rb index 0d0f2f6..16fc429 100644 --- a/data_structures/hash_table/anagram_checker.rb +++ b/data_structures/hash_table/anagram_checker.rb @@ -129,3 +129,38 @@ def is_anagram(s, t) true end + +# +# Approach 3: populate 2 hashes and compare them +# + +def is_anagram(s, t) + s = s.chars + t = t.chars + + return false if s.count != t.count + + hash1 = {} + s.chars.each do |value| + hash1[value] = if hash1[value] + hash1[value] + 1 + else + 1 + end + end + + hash2 = {} + t.chars.each do |value| + hash2[value] = if hash2[value] + hash2[value] + 1 + else + 1 + end + end + + hash1.keys.each do |key| + return false if hash2[key] != hash1[key] + end + + true +end From 434e72a4f7cc0cd977e98c941449e492929e1d2f Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Thu, 1 Apr 2021 09:38:27 -0700 Subject: [PATCH 38/83] Add very simple tests --- data_structures/hash_table/anagram_checker.rb | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/data_structures/hash_table/anagram_checker.rb b/data_structures/hash_table/anagram_checker.rb index 16fc429..abbb209 100644 --- a/data_structures/hash_table/anagram_checker.rb +++ b/data_structures/hash_table/anagram_checker.rb @@ -130,6 +130,21 @@ def is_anagram(s, t) true end +s = 'anagram' +t = 'nagaram' +puts(is_anagram(s, t)) +# => true + +s = 'rat' +t = 'car' +puts(is_anagram(s, t)) +# => false + +s = 'a' +t = 'ab' +puts(is_anagram(s, t)) +# => false + # # Approach 3: populate 2 hashes and compare them # @@ -141,7 +156,7 @@ def is_anagram(s, t) return false if s.count != t.count hash1 = {} - s.chars.each do |value| + s.each do |value| hash1[value] = if hash1[value] hash1[value] + 1 else @@ -150,7 +165,7 @@ def is_anagram(s, t) end hash2 = {} - t.chars.each do |value| + t.each do |value| hash2[value] = if hash2[value] hash2[value] + 1 else @@ -164,3 +179,18 @@ def is_anagram(s, t) true end + +s = 'anagram' +t = 'nagaram' +puts(is_anagram(s, t)) +# => true + +s = 'rat' +t = 'car' +puts(is_anagram(s, t)) +# => false + +s = 'a' +t = 'ab' +puts(is_anagram(s, t)) +# => false From 515b076e61872aaeac08d09ad6c77007ce1d75d5 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Thu, 1 Apr 2021 09:42:45 -0700 Subject: [PATCH 39/83] remove duplication --- data_structures/hash_table/anagram_checker.rb | 37 +------------------ 1 file changed, 1 insertion(+), 36 deletions(-) diff --git a/data_structures/hash_table/anagram_checker.rb b/data_structures/hash_table/anagram_checker.rb index abbb209..e15eed4 100644 --- a/data_structures/hash_table/anagram_checker.rb +++ b/data_structures/hash_table/anagram_checker.rb @@ -17,6 +17,7 @@ # # Approach: Hash table # + # Complexity analysis: # # Time complexity: O(n). Time complexity is O(n) since accessing the counter @@ -59,42 +60,6 @@ t = 'ab' puts(is_anagram(s, t)) # => false - -def is_anagram(s, t) - s_length = s.length - t_length = t.length - counter = Hash.new(0) - - return false unless s_length == t_length - - (0...s_length).each do |i| - counter[s[i]] += 1 - end - - (0...s_length).each do |i| - counter[t[i]] -= 1 - - return false if counter[t[i]] < 0 - end - - true -end - -s = 'anagram' -t = 'nagaram' -puts(is_anagram(s, t)) -# => true - -s = 'rat' -t = 'car' -puts(is_anagram(s, t)) -# => false - -s = 'a' -t = 'ab' -puts(is_anagram(s, t)) -# => false - # # Approach 2: Hash table # From c2bd60c2231b3ccbc385a4dd46d3e7ead10aab9b Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Thu, 1 Apr 2021 09:31:12 -0700 Subject: [PATCH 40/83] Add palindrome challenge --- DIRECTORY.md | 1 + data_structures/arrays/strings/palindrome.rb | 44 ++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 data_structures/arrays/strings/palindrome.rb diff --git a/DIRECTORY.md b/DIRECTORY.md index d0c4469..bca02fb 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -23,6 +23,7 @@ * Strings * [Anagram Checker](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/anagram_checker.rb) * [Jewels And Stones](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/jewels_and_stones.rb) + * [Palindrome](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/palindrome.rb) * [Remove Vowels](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/remove_vowels.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) diff --git a/data_structures/arrays/strings/palindrome.rb b/data_structures/arrays/strings/palindrome.rb new file mode 100644 index 0000000..1ebcfbe --- /dev/null +++ b/data_structures/arrays/strings/palindrome.rb @@ -0,0 +1,44 @@ +# Challenge name: Valid Palindrome +# +# Given a string s, determine if it is a palindrome, +# considering only alphanumeric characters and ignoring cases. +# +# Example 1: +# Input: s = "A man, a plan, a canal: Panama" +# Output: true +# Explanation: "amanaplanacanalpanama" is a palindrome. +# +# Example 2: +# Input: s = "race a car" +# Output: false +# Explanation: "raceacar" is not a palindrome. +# +# Constraints: +# 1 <= s.length <= 2 * 105 +# s consists only of printable ASCII characters. +# @param {String} s +# @return {Boolean} + +# +# Approach 1: Brute Force +# +# Time Complexity: +# +def is_palindrome(s) + s.downcase +end + +s = 'A man, a plan, a canal: Panama' +puts is_palindrome(s) +# Output: true +# Explanation: "amanaplanacanalpanama" is a palindrome. + +s = 'race a car' +puts is_palindrome(s) +# Output: false +# Explanation: "raceacar" is not a palindrome. + +s = 'ab_a' +puts is_palindrome(s) +# Output: true +# Explanation: "aba" is a palindrome. \ No newline at end of file From 13aec345eaca265cc5ea218754faefc62fe557a6 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Thu, 1 Apr 2021 09:54:34 -0700 Subject: [PATCH 41/83] add generate_parenthesis backtracking approach --- backtracking/generate_paranthesis.rb | 47 ++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 backtracking/generate_paranthesis.rb diff --git a/backtracking/generate_paranthesis.rb b/backtracking/generate_paranthesis.rb new file mode 100644 index 0000000..5881aea --- /dev/null +++ b/backtracking/generate_paranthesis.rb @@ -0,0 +1,47 @@ +# Given n pairs of parentheses, write a function to generate all combinations +# of well-formed parentheses. +# +# Example 1: +# +# Input: n = 3 +# Output: ["((()))","(()())","(())()","()(())","()()()"] +# Example 2: +# +# Input: n = 1 +# Output: ["()"] +# +# +# Constraints: +# +# 1 <= n <= 8 + +# @param {Integer} n +# @return {String[]} +def generate_parenthesis(n) + parenthesis = [] + backtrack(parenthesis, "", 0, 0, n) + parenthesis +end + +def backtrack(parenthesis, curr, open, close, max) + if curr.length == max * 2 + parenthesis.push(curr) + return + end + + if open < max + backtrack(parenthesis, curr + "(", open + 1, close, max) + end + + if close < open + backtrack(parenthesis, curr + ")", open, close + 1, max) + end +end + +n = 3 +print(generate_parenthesis(n)) +# Output: ["((()))","(()())","(())()","()(())","()()()"] + +n = 1 +print(generate_parenthesis(n)) +# Output: ["()"] From 1350b46474aa437169639166e041ebdfdaa469af Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Thu, 1 Apr 2021 16:54:50 +0000 Subject: [PATCH 42/83] updating DIRECTORY.md --- DIRECTORY.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 42b9add..7c86a15 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -1,4 +1,7 @@ +## Backtracking + * [Generate Paranthesis](https://github.com/TheAlgorithms/Ruby/blob/master/backtracking/generate_paranthesis.rb) + ## Bit Manipulation * [Power Of Two](https://github.com/TheAlgorithms/Ruby/blob/master/bit_manipulation/power_of_two.rb) From 1d974b2ea18e646fe277ada716902d42ed95c659 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Thu, 1 Apr 2021 09:58:04 -0700 Subject: [PATCH 43/83] Add approach description --- backtracking/generate_paranthesis.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/backtracking/generate_paranthesis.rb b/backtracking/generate_paranthesis.rb index 5881aea..587a25c 100644 --- a/backtracking/generate_paranthesis.rb +++ b/backtracking/generate_paranthesis.rb @@ -15,6 +15,16 @@ # # 1 <= n <= 8 +# Approach: +# +# Let's only add '(' or ')' when we know it will remain a valid sequence. +# We can do this by keeping track of the number of opening and closing brackets +# we have placed so far. +# +# We can start an opening bracket if we still have one (of n) left to place. +# And we could start a closing bracket if it'd not exceed the number of opening +# brackets. + # @param {Integer} n # @return {String[]} def generate_parenthesis(n) From 7eea20ed6e0369db5d5ff8d2f99aa431ac78a224 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Thu, 1 Apr 2021 10:06:40 -0700 Subject: [PATCH 44/83] Add complexity analysis and detailed example --- backtracking/generate_paranthesis.rb | 49 ++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/backtracking/generate_paranthesis.rb b/backtracking/generate_paranthesis.rb index 587a25c..0ea684e 100644 --- a/backtracking/generate_paranthesis.rb +++ b/backtracking/generate_paranthesis.rb @@ -25,6 +25,14 @@ # And we could start a closing bracket if it'd not exceed the number of opening # brackets. +# Complexity Analysis +# +# Time Complexity: O(4^n/sqrt(n). Each valid sequence has at most n steps during the backtracking procedure. +# Space Complexity: O(4^n/sqrt(n), as described above, and using O(n) space to store the sequence. + +# Refer to the attached diagram for recursion, +# The numbers next to each node are the counts of left and right parantheses + # @param {Integer} n # @return {String[]} def generate_parenthesis(n) @@ -52,6 +60,47 @@ n = 3 print(generate_parenthesis(n)) # Output: ["((()))","(()())","(())()","()(())","()()()"] +# *** Example: n = 3 *** Space after each DFS instance +# backtrack called with 0 0 [] +# backtrack called with ( 1 0 [] +# backtrack called with (( 2 0 [] +# backtrack called with ((( 3 0 [] +# backtrack called with ((() 3 1 [] +# backtrack called with ((()) 3 2 [] +# backtrack called with ((())) 3 3 [] +# backtrack return with ((()) 3 2 ["((()))"] +# backtrack return with ((() 3 1 ["((()))"] +# backtrack return with ((( 3 0 ["((()))"] +# backtrack called with (() 2 1 ["((()))"] +# backtrack called with (()( 3 1 ["((()))"] +# backtrack called with (()() 3 2 ["((()))"] +# backtrack called with (()()) 3 3 ["((()))"] +# backtrack return with (()() 3 2 ["((()))", "(()())"] +# backtrack return with (()( 3 1 ["((()))", "(()())"] +# backtrack called with (()) 2 2 ["((()))", "(()())"] +# backtrack called with (())( 3 2 ["((()))", "(()())"] +# backtrack called with (())() 3 3 ["((()))", "(()())"] +# backtrack return with (())( 3 2 ["((()))", "(()())", "(())()"] +# backtrack return with (()) 2 2 ["((()))", "(()())", "(())()"] +# backtrack return with (() 2 1 ["((()))", "(()())", "(())()"] +# backtrack return with (( 2 0 ["((()))", "(()())", "(())()"] +# backtrack called with () 1 1 ["((()))", "(()())", "(())()"] +# backtrack called with ()( 2 1 ["((()))", "(()())", "(())()"] +# backtrack called with ()(( 3 1 ["((()))", "(()())", "(())()"] +# backtrack called with ()(() 3 2 ["((()))", "(()())", "(())()"] +# backtrack called with ()(()) 3 3 ["((()))", "(()())", "(())()"] +# backtrack return with ()(() 3 2 ["((()))", "(()())", "(())()", "()(())"] +# backtrack return with ()(( 3 1 ["((()))", "(()())", "(())()", "()(())"] +# backtrack called with ()() 2 2 ["((()))", "(()())", "(())()", "()(())"] +# backtrack called with ()()( 3 2 ["((()))", "(()())", "(())()", "()(())"] +# backtrack called with ()()() 3 3 ["((()))", "(()())", "(())()", "()(())"] +# backtrack return with ()()( 3 2 ["((()))", "(()())", "(())()", "()(())", "()()()"] +# backtrack return with ()() 2 2 ["((()))", "(()())", "(())()", "()(())", "()()()"] +# backtrack return with ()( 2 1 ["((()))", "(()())", "(())()", "()(())", "()()()"] +# backtrack return with () 1 1 ["((()))", "(()())", "(())()", "()(())", "()()()"] +# backtrack return with ( 1 0 ["((()))", "(()())", "(())()", "()(())", "()()()"] +# backtrack return with 0 0 ["((()))", "(()())", "(())()", "()(())", "()()()"] + n = 1 print(generate_parenthesis(n)) # Output: ["()"] From fc0c664f6bd123fd2e6f5dba7a06c05497240866 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Thu, 1 Apr 2021 10:45:41 -0700 Subject: [PATCH 45/83] close ) --- backtracking/generate_paranthesis.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backtracking/generate_paranthesis.rb b/backtracking/generate_paranthesis.rb index 0ea684e..5c0e3fa 100644 --- a/backtracking/generate_paranthesis.rb +++ b/backtracking/generate_paranthesis.rb @@ -27,8 +27,8 @@ # Complexity Analysis # -# Time Complexity: O(4^n/sqrt(n). Each valid sequence has at most n steps during the backtracking procedure. -# Space Complexity: O(4^n/sqrt(n), as described above, and using O(n) space to store the sequence. +# Time Complexity: O(4^n/sqrt(n)). Each valid sequence has at most n steps during the backtracking procedure. +# Space Complexity: O(4^n/sqrt(n)), as described above, and using O(n) space to store the sequence. # Refer to the attached diagram for recursion, # The numbers next to each node are the counts of left and right parantheses From e90238e1f712285bb87d421acdb34de0760d8ef5 Mon Sep 17 00:00:00 2001 From: Abhinav Anand Date: Fri, 2 Apr 2021 23:01:29 +0530 Subject: [PATCH 46/83] Added factorial iterative program --- maths/factorial_iterative.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 maths/factorial_iterative.rb diff --git a/maths/factorial_iterative.rb b/maths/factorial_iterative.rb new file mode 100644 index 0000000..cc070b3 --- /dev/null +++ b/maths/factorial_iterative.rb @@ -0,0 +1,28 @@ +=begin +A ruby program calculate factorial of a given number. +Mathematical Explanation: The factorial of a number is the product of all the integers from 1 to that number. +i.e: n! = n*(n-1)*(n-2)......*2*1 +=end + +def factorial(n) + if n < 0 + return nil + end + + fac = 1 + while n > 0 + fac = fac * n + n -= 1 + end + + return fac +end + +puts '4! = ' + factorial(4).to_s +# 4! = 24 + +puts '0! = ' + factorial(0).to_s +# 0! = 1 + +puts '10! = ' + factorial(10).to_s +# 10! = 3628800 From 30c9176098283561bee7598eaaebe3019bd345cd Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Fri, 2 Apr 2021 11:22:53 -0700 Subject: [PATCH 47/83] minor changes --- .../{factorial_iterative.rb => factorial.rb} | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) rename maths/{factorial_iterative.rb => factorial.rb} (72%) diff --git a/maths/factorial_iterative.rb b/maths/factorial.rb similarity index 72% rename from maths/factorial_iterative.rb rename to maths/factorial.rb index cc070b3..e83b347 100644 --- a/maths/factorial_iterative.rb +++ b/maths/factorial.rb @@ -4,18 +4,21 @@ Mathematical Explanation: The factorial of a number is the product of all the in i.e: n! = n*(n-1)*(n-2)......*2*1 =end +# +# Approach: Interative +# + def factorial(n) - if n < 0 - return nil - end - - fac = 1 - while n > 0 - fac = fac * n - n -= 1 - end - - return fac + return nil if n < 0 + + fac = 1 + + while n > 0 + fac *= n + n -= 1 + end + + fac end puts '4! = ' + factorial(4).to_s From 46ec0d4fecc019582272ee848f6be2659abd9d21 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Fri, 2 Apr 2021 18:23:23 +0000 Subject: [PATCH 48/83] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 42b9add..b732e81 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -66,6 +66,7 @@ * [Ceil](https://github.com/TheAlgorithms/Ruby/blob/master/maths/ceil.rb) * [Ceil Test](https://github.com/TheAlgorithms/Ruby/blob/master/maths/ceil_test.rb) * [Decimal To Binary](https://github.com/TheAlgorithms/Ruby/blob/master/maths/decimal_to_binary.rb) + * [Factorial](https://github.com/TheAlgorithms/Ruby/blob/master/maths/factorial.rb) * [Fibonacci](https://github.com/TheAlgorithms/Ruby/blob/master/maths/fibonacci.rb) * [Number Of Digits](https://github.com/TheAlgorithms/Ruby/blob/master/maths/number_of_digits.rb) * [Power Of Two](https://github.com/TheAlgorithms/Ruby/blob/master/maths/power_of_two.rb) From 04a4c9aaedfac2f3b9145be39f1201362a053a0d Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Mon, 5 Apr 2021 13:57:41 -0700 Subject: [PATCH 49/83] Add solution using .reverse --- data_structures/arrays/strings/palindrome.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/data_structures/arrays/strings/palindrome.rb b/data_structures/arrays/strings/palindrome.rb index 1ebcfbe..03490ac 100644 --- a/data_structures/arrays/strings/palindrome.rb +++ b/data_structures/arrays/strings/palindrome.rb @@ -20,12 +20,13 @@ # @return {Boolean} # -# Approach 1: Brute Force +# Approach 1: Using Ruby method .reverse # -# Time Complexity: +# Time Complexity: O(n) # def is_palindrome(s) - s.downcase + letters_only = s.downcase.gsub(/[^0-9a-z]/i, '') + letters_only.reverse == letters_only end s = 'A man, a plan, a canal: Panama' From 6c7b37ab76b1a52c93205d91fb65a9d0413cc1db Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Mon, 5 Apr 2021 14:08:23 -0700 Subject: [PATCH 50/83] Add solution using a reversed array --- data_structures/arrays/strings/palindrome.rb | 29 ++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/data_structures/arrays/strings/palindrome.rb b/data_structures/arrays/strings/palindrome.rb index 03490ac..4d70171 100644 --- a/data_structures/arrays/strings/palindrome.rb +++ b/data_structures/arrays/strings/palindrome.rb @@ -39,6 +39,35 @@ puts is_palindrome(s) # Output: false # Explanation: "raceacar" is not a palindrome. +s = 'ab_a' +puts is_palindrome(s) +# Output: true +# Explanation: "aba" is a palindrome. + +# +# Approach 2: Reversed array +# +# Time Complexity: O(n) +# +def is_palindrome(s) + letters_only_array = s.downcase.gsub(/[^0-9a-z]/i, '').split('') + reversed_array = [] + letters_only_array.each do |letter| + reversed_array.unshift(letter) + end + letters_only_array == reversed_array +end + +s = 'A man, a plan, a canal: Panama' +puts is_palindrome(s) +# Output: true +# Explanation: "amanaplanacanalpanama" is a palindrome. + +s = 'race a car' +puts is_palindrome(s) +# Output: false +# Explanation: "raceacar" is not a palindrome. + s = 'ab_a' puts is_palindrome(s) # Output: true From 7655003b6cc333ee1512a33f7c9fa0901974189d Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Mon, 5 Apr 2021 14:31:57 -0700 Subject: [PATCH 51/83] Add solution using two pointers --- data_structures/arrays/strings/palindrome.rb | 36 ++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/data_structures/arrays/strings/palindrome.rb b/data_structures/arrays/strings/palindrome.rb index 4d70171..28e7921 100644 --- a/data_structures/arrays/strings/palindrome.rb +++ b/data_structures/arrays/strings/palindrome.rb @@ -68,6 +68,42 @@ puts is_palindrome(s) # Output: false # Explanation: "raceacar" is not a palindrome. +s = 'ab_a' +puts is_palindrome(s) +# Output: true +# Explanation: "aba" is a palindrome. + +# +# Approach 2: Two Pointers +# +# Time Complexity: O(n) +# +def is_palindrome(s) + letters_only = s.downcase.gsub(/[^0-9a-z]/i, '') + p1 = 0 + p2 = letters_only.length - 1 + + while p1 < p2 + if letters_only[p1] == letters_only[p2] + p1 += 1 + p2 -= 1 + else + return false + end + end + true +end + +s = 'A man, a plan, a canal: Panama' +puts is_palindrome(s) +# Output: true +# Explanation: "amanaplanacanalpanama" is a palindrome. + +s = 'race a car' +puts is_palindrome(s) +# Output: false +# Explanation: "raceacar" is not a palindrome. + s = 'ab_a' puts is_palindrome(s) # Output: true From e6b674275ea56995dafe8e7e080e3789ea503f91 Mon Sep 17 00:00:00 2001 From: nyapsilon Date: Fri, 9 Apr 2021 21:24:04 +0300 Subject: [PATCH 52/83] Minor code style improvements Tabs are corrected, semantic blocks are separated. --- Searches/binary_search.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Searches/binary_search.rb b/Searches/binary_search.rb index bbc8efa..d0710a2 100644 --- a/Searches/binary_search.rb +++ b/Searches/binary_search.rb @@ -8,18 +8,21 @@ def binary_search(array, key) return middle if array[middle] == key if key < array[middle] - back = middle - 1 -else - front = middle + 1 -end + back = middle - 1 + else + front = middle + 1 + end end + nil end puts "Enter a sorted space-separated list:" arr = gets.chomp.split(' ').map(&:to_i) + puts "Enter the value to be searched:" value = gets.chomp.to_i + puts if binary_search(arr, value) != nil "Found at index #{binary_search(arr, value)}" else From bd6b1067c0b56d206e7c4a9307c57312289bc197 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Fri, 9 Apr 2021 21:07:54 -0700 Subject: [PATCH 53/83] Update data_structures/arrays/strings/palindrome.rb --- data_structures/arrays/strings/palindrome.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/data_structures/arrays/strings/palindrome.rb b/data_structures/arrays/strings/palindrome.rb index 28e7921..176243e 100644 --- a/data_structures/arrays/strings/palindrome.rb +++ b/data_structures/arrays/strings/palindrome.rb @@ -76,7 +76,14 @@ puts is_palindrome(s) # # Approach 2: Two Pointers # -# Time Complexity: O(n) + +# +# Complexity Analysis: +# +# Time Complexity: O(n), in length n of the string. We traverse over each +# character at most once until the two pointers meet in the middle, or when +# we break and return early. +# Space Complexity: O(1). No extra space required, at all. # def is_palindrome(s) letters_only = s.downcase.gsub(/[^0-9a-z]/i, '') @@ -107,4 +114,4 @@ puts is_palindrome(s) s = 'ab_a' puts is_palindrome(s) # Output: true -# Explanation: "aba" is a palindrome. \ No newline at end of file +# Explanation: "aba" is a palindrome. From 49db26c56be44d2ca3ab00034e264a1ae877604e Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Fri, 9 Apr 2021 21:07:57 -0700 Subject: [PATCH 54/83] Update data_structures/arrays/strings/palindrome.rb --- data_structures/arrays/strings/palindrome.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/data_structures/arrays/strings/palindrome.rb b/data_structures/arrays/strings/palindrome.rb index 176243e..587e242 100644 --- a/data_structures/arrays/strings/palindrome.rb +++ b/data_structures/arrays/strings/palindrome.rb @@ -47,7 +47,17 @@ puts is_palindrome(s) # # Approach 2: Reversed array # -# Time Complexity: O(n) +# Complexity Analysis: +# +# Time Complexity: O(n), in length n of the string. +# +# We need to iterate through the string: When we filter out non-alphanumeric characters and convert the remaining +# characters to lower-case. When we reverse the string. When we compare the original and the reversed strings. +# Each iteration runs linearly in time (since each character operation completes in constant time). +# Thus, the effective run-time complexity is linear. +# +# Space Complexity: O(n), in length n of the string. We need O(n) additional +# space to store the filtered string and the reversed string. # def is_palindrome(s) letters_only_array = s.downcase.gsub(/[^0-9a-z]/i, '').split('') From 443cf9db8850e78d45f1fde83907f1d021e597e0 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 10 Apr 2021 04:07:57 +0000 Subject: [PATCH 55/83] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index d118dca..c94db91 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -65,6 +65,7 @@ * [Add Digits](https://github.com/TheAlgorithms/Ruby/blob/master/maths/add_digits.rb) * [Aliquot Sum](https://github.com/TheAlgorithms/Ruby/blob/master/maths/aliquot_sum.rb) * [Aliquot Sum Test](https://github.com/TheAlgorithms/Ruby/blob/master/maths/aliquot_sum_test.rb) + * [Average Mean](https://github.com/TheAlgorithms/Ruby/blob/master/maths/average_mean.rb) * [Binary To Decimal](https://github.com/TheAlgorithms/Ruby/blob/master/maths/binary_to_decimal.rb) * [Ceil](https://github.com/TheAlgorithms/Ruby/blob/master/maths/ceil.rb) * [Ceil Test](https://github.com/TheAlgorithms/Ruby/blob/master/maths/ceil_test.rb) From 9fbd75fa66dd64a7766fa250075e37bcd26c406f Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 10 Apr 2021 09:56:05 -0700 Subject: [PATCH 56/83] Add hash table approach --- data_structures/arrays/arrays_intersection.rb | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 data_structures/arrays/arrays_intersection.rb diff --git a/data_structures/arrays/arrays_intersection.rb b/data_structures/arrays/arrays_intersection.rb new file mode 100644 index 0000000..cf1c1dc --- /dev/null +++ b/data_structures/arrays/arrays_intersection.rb @@ -0,0 +1,55 @@ +# Given three integer arrays arr1, arr2 and arr3 sorted in strictly increasing order, return a sorted array of only the integers that appeared in all three arrays. +# +# Example 1: +# +# Input: arr1 = [1,2,3,4,5], arr2 = [1,2,5,7,9], arr3 = [1,3,4,5,8] +# Output: [1,5] +# Explanation: Only 1 and 5 appeared in the three arrays. +# +# Example 2: +# +# Input: arr1 = [197,418,523,876,1356], arr2 = [501,880,1593,1710,1870], arr3 = [521,682,1337,1395,1764] +# Output: [] +# +# + +# +# Approach: Hash table +# + +# Complexity Analysis + +# Time Complexity: O(n) - n is the total length of +# all of the input arrays. +# Space Complexity: O(n) - n is the total length of all of the +# input arrays. This is because we adopted a Hash table to store all +# numbers and their number of appearances as values. + +def arrays_intersection(arr1, arr2, arr3) + hash = Hash.new(0) + + add_to_hash(arr1, hash) + add_to_hash(arr2, hash) + add_to_hash(arr3, hash) + + hash.select { |key, value| value == 3 }.keys +end + +def add_to_hash(arr, hash) + arr.each_with_index do |value, index| + hash[value] += 1 + end +end + +arr1 = [1,2,3,4,5] +arr2 = [1,2,5,7,9] +arr3 = [1,3,4,5,8] +print arrays_intersection(arr1, arr2, arr3) +# Output: [1,5] +# Explanation: Only 1 and 5 appeared in the three arrays. + +arr1 = [197,418,523,876,1356] +arr2 = [501,880,1593,1710,1870] +arr3 = [521,682,1337,1395,1764] +print arrays_intersection(arr1, arr2, arr3) +# Output: [] From 21e8bb04ef3d4346e1ba94dfb093fb7e1ed3cba3 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 10 Apr 2021 16:56:19 +0000 Subject: [PATCH 57/83] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index a34b83d..bd38b0d 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -14,6 +14,7 @@ ## Data Structures * Arrays * [Add Digits](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/add_digits.rb) + * [Arrays Intersection](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/arrays_intersection.rb) * [Find All Duplicates In An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/find_all_duplicates_in_an_array.rb) * [Find The Highest Altitude](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/find_the_highest_altitude.rb) * [Fizz Buzz](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/fizz_buzz.rb) From fe4e56e8a4a04b3fc317477ad6a16e369f50bf1a Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 10 Apr 2021 09:57:25 -0700 Subject: [PATCH 58/83] Move to correct path --- data_structures/{arrays => hash_table}/arrays_intersection.rb | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename data_structures/{arrays => hash_table}/arrays_intersection.rb (100%) diff --git a/data_structures/arrays/arrays_intersection.rb b/data_structures/hash_table/arrays_intersection.rb similarity index 100% rename from data_structures/arrays/arrays_intersection.rb rename to data_structures/hash_table/arrays_intersection.rb From d6e7b9f1d8411d02a81664f517dbec81b56f9e27 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 10 Apr 2021 16:57:53 +0000 Subject: [PATCH 59/83] updating DIRECTORY.md --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index bd38b0d..daeb2cc 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -14,7 +14,6 @@ ## Data Structures * Arrays * [Add Digits](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/add_digits.rb) - * [Arrays Intersection](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/arrays_intersection.rb) * [Find All Duplicates In An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/find_all_duplicates_in_an_array.rb) * [Find The Highest Altitude](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/find_the_highest_altitude.rb) * [Fizz Buzz](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/fizz_buzz.rb) @@ -38,6 +37,7 @@ * [Preorder Traversal](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/binary_trees/preorder_traversal.rb) * Hash Table * [Anagram Checker](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/anagram_checker.rb) + * [Arrays Intersection](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/arrays_intersection.rb) * [Find All Duplicates In An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/find_all_duplicates_in_an_array.rb) * [Richest Customer Wealth](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/richest_customer_wealth.rb) * [Two Sum](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/two_sum.rb) From 745b66227bb05231d401d51e558a00aae42d3a39 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 10 Apr 2021 10:04:40 -0700 Subject: [PATCH 60/83] Minor changes --- .../hash_table/arrays_intersection.rb | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/data_structures/hash_table/arrays_intersection.rb b/data_structures/hash_table/arrays_intersection.rb index cf1c1dc..7d270fc 100644 --- a/data_structures/hash_table/arrays_intersection.rb +++ b/data_structures/hash_table/arrays_intersection.rb @@ -32,24 +32,25 @@ def arrays_intersection(arr1, arr2, arr3) add_to_hash(arr2, hash) add_to_hash(arr3, hash) - hash.select { |key, value| value == 3 }.keys + hash.select { |_key, value| value == 3 }.keys end def add_to_hash(arr, hash) - arr.each_with_index do |value, index| - hash[value] += 1 + arr.count.times do |pointer| + value = arr[pointer] + hash[value] += 1 end end -arr1 = [1,2,3,4,5] -arr2 = [1,2,5,7,9] -arr3 = [1,3,4,5,8] +arr1 = [1, 2, 3, 4, 5] +arr2 = [1, 2, 5, 7, 9] +arr3 = [1, 3, 4, 5, 8] print arrays_intersection(arr1, arr2, arr3) # Output: [1,5] # Explanation: Only 1 and 5 appeared in the three arrays. -arr1 = [197,418,523,876,1356] -arr2 = [501,880,1593,1710,1870] -arr3 = [521,682,1337,1395,1764] +arr1 = [197, 418, 523, 876, 1356] +arr2 = [501, 880, 1593, 1710, 1870] +arr3 = [521, 682, 1337, 1395, 1764] print arrays_intersection(arr1, arr2, arr3) # Output: [] From 2197a587fb4d6405adc425adfa81bf06922c550b Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 10 Apr 2021 10:15:00 -0700 Subject: [PATCH 61/83] Update arrays_intersection.rb --- data_structures/hash_table/arrays_intersection.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/hash_table/arrays_intersection.rb b/data_structures/hash_table/arrays_intersection.rb index 7d270fc..f100e37 100644 --- a/data_structures/hash_table/arrays_intersection.rb +++ b/data_structures/hash_table/arrays_intersection.rb @@ -32,7 +32,7 @@ def arrays_intersection(arr1, arr2, arr3) add_to_hash(arr2, hash) add_to_hash(arr3, hash) - hash.select { |_key, value| value == 3 }.keys + hash.select { |key, value| value == 3 }.keys end def add_to_hash(arr, hash) From ce0d1863a292cb69ab7ab01b4145e8e462058f29 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 10 Apr 2021 10:38:43 -0700 Subject: [PATCH 62/83] Next greater element --- .../arrays/next_greater_element.rb | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 data_structures/arrays/next_greater_element.rb diff --git a/data_structures/arrays/next_greater_element.rb b/data_structures/arrays/next_greater_element.rb new file mode 100644 index 0000000..fc71766 --- /dev/null +++ b/data_structures/arrays/next_greater_element.rb @@ -0,0 +1,55 @@ +# You are given two integer arrays nums1 and nums2 both of unique elements, where nums1 is a subset of nums2. +# +# Find all the next greater numbers for nums1's elements in the corresponding places of nums2. +# +# The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, return -1 for this number. + +# Example 1: +# +# Input: nums1 = [4,1,2], nums2 = [1,3,4,2] +# Output: [-1,3,-1] +# +# Explanation: +# For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1. +# For number 1 in the first array, the next greater number for it in the second array is 3. +# For number 2 in the first array, there is no next greater number for it in the second array, so output -1. +# +# Example 2: +# +# Input: nums1 = [2,4], nums2 = [1,2,3,4] +# Output: [3,-1] +# +# Explanation: +# For number 2 in the first array, the next greater number for it in the second array is 3. +# For number 4 in the first array, there is no next greater number for it in the second array, so output -1. + +# @param {Integer[]} nums1 +# @param {Integer[]} nums2 +# @return {Integer[]} +def next_greater_element(nums1, nums2) + nums1.each_with_index do |value, pointer1| + max = 0 + pos_nums2 = nums2.find_index(value) + + nums2[pos_nums2..nums2.count].each do |value| + if value > nums1[pointer1] + max = value + break + end + end + + nums1[pointer1] = (nums1[pointer1] < max ? max : -1) + end + + nums1 +end + +nums1 = [4, 1, 2] +nums2 = [1, 3, 4, 2] +print next_greater_element(nums1, nums2) +# Output: [-1,3,-1] + +nums1 = [2, 4] +nums2 = [1, 2, 3, 4] +print next_greater_element(nums1, nums2) +# Output: [3,-1] From 1c9a26f389a5d62b60a10a7879c0eed848ec8c0f Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 10 Apr 2021 10:41:01 -0700 Subject: [PATCH 63/83] Minor changes --- data_structures/arrays/next_greater_element.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/data_structures/arrays/next_greater_element.rb b/data_structures/arrays/next_greater_element.rb index fc71766..0790ab1 100644 --- a/data_structures/arrays/next_greater_element.rb +++ b/data_structures/arrays/next_greater_element.rb @@ -18,7 +18,7 @@ # # Input: nums1 = [2,4], nums2 = [1,2,3,4] # Output: [3,-1] -# +# # Explanation: # For number 2 in the first array, the next greater number for it in the second array is 3. # For number 4 in the first array, there is no next greater number for it in the second array, so output -1. @@ -27,18 +27,18 @@ # @param {Integer[]} nums2 # @return {Integer[]} def next_greater_element(nums1, nums2) - nums1.each_with_index do |value, pointer1| + nums1.each_with_index do |nums1_value, pointer1| max = 0 - pos_nums2 = nums2.find_index(value) + pos_nums2 = nums2.find_index(nums1_value) - nums2[pos_nums2..nums2.count].each do |value| - if value > nums1[pointer1] - max = value + nums2[pos_nums2..nums2.count].each do |nums2_value| + if nums2_value > nums1_value + max = nums2_value break end end - nums1[pointer1] = (nums1[pointer1] < max ? max : -1) + nums1[pointer1] = (nums1_value < max ? max : -1) end nums1 From 4e0d914d63c1eb4c4ea2eb436325b3e2992623cf Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 10 Apr 2021 17:41:17 +0000 Subject: [PATCH 64/83] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index c94db91..763e907 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -18,6 +18,7 @@ * [Find The Highest Altitude](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/find_the_highest_altitude.rb) * [Fizz Buzz](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/fizz_buzz.rb) * [Get Products Of All Other Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/get_products_of_all_other_elements.rb) + * [Next Greater Element](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/next_greater_element.rb) * [Remove Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/remove_elements.rb) * [Richest Customer Wealth](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/richest_customer_wealth.rb) * [Shuffle Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/shuffle_array.rb) From 4d9d7ca1cdad47591163d384c8de5f891f23c2a8 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 10 Apr 2021 10:46:55 -0700 Subject: [PATCH 65/83] add complexity analysis --- data_structures/arrays/next_greater_element.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/data_structures/arrays/next_greater_element.rb b/data_structures/arrays/next_greater_element.rb index 0790ab1..ffe957e 100644 --- a/data_structures/arrays/next_greater_element.rb +++ b/data_structures/arrays/next_greater_element.rb @@ -23,6 +23,15 @@ # For number 2 in the first array, the next greater number for it in the second array is 3. # For number 4 in the first array, there is no next greater number for it in the second array, so output -1. +# +# Approach: Brute Force +# + +# Complexity Analysis +# +# Time complexity : O(m*n). The complete nums1 array (of size n) needs to be scanned for all the m elements of nums2 in the worst case. +# Space complexity : O(1). No additional space since we're swapping elements in nums1 and returning the input array. + # @param {Integer[]} nums1 # @param {Integer[]} nums2 # @return {Integer[]} From 20d5ea3b6d419bd3b9fc62f07c3863607ff1c167 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 10 Apr 2021 10:49:41 -0700 Subject: [PATCH 66/83] Update next_greater_element.rb --- data_structures/arrays/next_greater_element.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/arrays/next_greater_element.rb b/data_structures/arrays/next_greater_element.rb index ffe957e..92b3344 100644 --- a/data_structures/arrays/next_greater_element.rb +++ b/data_structures/arrays/next_greater_element.rb @@ -29,8 +29,8 @@ # Complexity Analysis # -# Time complexity : O(m*n). The complete nums1 array (of size n) needs to be scanned for all the m elements of nums2 in the worst case. -# Space complexity : O(1). No additional space since we're swapping elements in nums1 and returning the input array. +# Time complexity: O(m*n). The complete nums1 array (of size n) needs to be scanned for all the m elements of nums2 in the worst case. +# Space complexity: O(1). No additional space since we're swapping elements in nums1 and returning the input array. # @param {Integer[]} nums1 # @param {Integer[]} nums2 From f94eafd15a76817545647c5c408af416fd21ee15 Mon Sep 17 00:00:00 2001 From: maxbarsukov Date: Sun, 11 Apr 2021 14:39:22 +0300 Subject: [PATCH 67/83] The Searches folder is renamed to lowercase just like the other folders --- {Searches => searches}/binary_search.rb | 0 {Searches => searches}/depth_first_search.rb | 0 {Searches => searches}/double_linear_search.rb | 0 {Searches => searches}/jump_search.rb | 0 {Searches => searches}/linear_search.rb | 0 {Searches => searches}/recursive_double_linear_search.rb | 0 {Searches => searches}/recursive_linear_search.rb | 0 {Searches => searches}/ternary_search.rb | 0 8 files changed, 0 insertions(+), 0 deletions(-) rename {Searches => searches}/binary_search.rb (100%) rename {Searches => searches}/depth_first_search.rb (100%) rename {Searches => searches}/double_linear_search.rb (100%) rename {Searches => searches}/jump_search.rb (100%) rename {Searches => searches}/linear_search.rb (100%) rename {Searches => searches}/recursive_double_linear_search.rb (100%) rename {Searches => searches}/recursive_linear_search.rb (100%) rename {Searches => searches}/ternary_search.rb (100%) diff --git a/Searches/binary_search.rb b/searches/binary_search.rb similarity index 100% rename from Searches/binary_search.rb rename to searches/binary_search.rb diff --git a/Searches/depth_first_search.rb b/searches/depth_first_search.rb similarity index 100% rename from Searches/depth_first_search.rb rename to searches/depth_first_search.rb diff --git a/Searches/double_linear_search.rb b/searches/double_linear_search.rb similarity index 100% rename from Searches/double_linear_search.rb rename to searches/double_linear_search.rb diff --git a/Searches/jump_search.rb b/searches/jump_search.rb similarity index 100% rename from Searches/jump_search.rb rename to searches/jump_search.rb diff --git a/Searches/linear_search.rb b/searches/linear_search.rb similarity index 100% rename from Searches/linear_search.rb rename to searches/linear_search.rb diff --git a/Searches/recursive_double_linear_search.rb b/searches/recursive_double_linear_search.rb similarity index 100% rename from Searches/recursive_double_linear_search.rb rename to searches/recursive_double_linear_search.rb diff --git a/Searches/recursive_linear_search.rb b/searches/recursive_linear_search.rb similarity index 100% rename from Searches/recursive_linear_search.rb rename to searches/recursive_linear_search.rb diff --git a/Searches/ternary_search.rb b/searches/ternary_search.rb similarity index 100% rename from Searches/ternary_search.rb rename to searches/ternary_search.rb From b2fc530555f38f2da5f3fe173e5ae6f97b406ee3 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 11 Apr 2021 22:56:29 +0000 Subject: [PATCH 68/83] updating DIRECTORY.md --- DIRECTORY.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index a34b83d..45e03fa 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -104,14 +104,14 @@ * [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_5/sol1.rb) ## Searches - * [Binary Search](https://github.com/TheAlgorithms/Ruby/blob/master/Searches/binary_search.rb) - * [Depth First Search](https://github.com/TheAlgorithms/Ruby/blob/master/Searches/depth_first_search.rb) - * [Double Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/Searches/double_linear_search.rb) - * [Jump Search](https://github.com/TheAlgorithms/Ruby/blob/master/Searches/jump_search.rb) - * [Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/Searches/linear_search.rb) - * [Recursive Double Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/Searches/recursive_double_linear_search.rb) - * [Recursive Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/Searches/recursive_linear_search.rb) - * [Ternary Search](https://github.com/TheAlgorithms/Ruby/blob/master/Searches/ternary_search.rb) + * [Binary Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/binary_search.rb) + * [Depth First Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/depth_first_search.rb) + * [Double Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/double_linear_search.rb) + * [Jump Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/jump_search.rb) + * [Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/linear_search.rb) + * [Recursive Double Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/recursive_double_linear_search.rb) + * [Recursive Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/recursive_linear_search.rb) + * [Ternary Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/ternary_search.rb) ## Sorting * [Bogo Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/bogo_sort.rb) From 0f3441c0882faa759dcfcccb021631e0f5b45dd0 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Wed, 14 Apr 2021 23:41:50 +0000 Subject: [PATCH 69/83] updating DIRECTORY.md --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 4f8a0a7..df4f340 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -65,10 +65,12 @@ ## Maths * [Abs](https://github.com/TheAlgorithms/Ruby/blob/master/maths/abs.rb) * [Abs Test](https://github.com/TheAlgorithms/Ruby/blob/master/maths/abs_test.rb) + * [Add](https://github.com/TheAlgorithms/Ruby/blob/master/maths/add.rb) * [Add Digits](https://github.com/TheAlgorithms/Ruby/blob/master/maths/add_digits.rb) * [Aliquot Sum](https://github.com/TheAlgorithms/Ruby/blob/master/maths/aliquot_sum.rb) * [Aliquot Sum Test](https://github.com/TheAlgorithms/Ruby/blob/master/maths/aliquot_sum_test.rb) * [Average Mean](https://github.com/TheAlgorithms/Ruby/blob/master/maths/average_mean.rb) + * [Average Median](https://github.com/TheAlgorithms/Ruby/blob/master/maths/average_median.rb) * [Binary To Decimal](https://github.com/TheAlgorithms/Ruby/blob/master/maths/binary_to_decimal.rb) * [Ceil](https://github.com/TheAlgorithms/Ruby/blob/master/maths/ceil.rb) * [Ceil Test](https://github.com/TheAlgorithms/Ruby/blob/master/maths/ceil_test.rb) From 50e6dc6f86993d9ad93fdf4760d4204fcc49fd9c Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Thu, 15 Apr 2021 17:25:30 -0700 Subject: [PATCH 70/83] Arrays intersection: two-pointer approach --- data_structures/arrays/arrays_intersection.rb | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 data_structures/arrays/arrays_intersection.rb diff --git a/data_structures/arrays/arrays_intersection.rb b/data_structures/arrays/arrays_intersection.rb new file mode 100644 index 0000000..63db80e --- /dev/null +++ b/data_structures/arrays/arrays_intersection.rb @@ -0,0 +1,67 @@ +# Given three integer arrays arr1, arr2 and arr3 sorted in strictly increasing order, return a sorted array of only the integers that appeared in all three arrays. +# +# Example 1: +# +# Input: arr1 = [1,2,3,4,5], arr2 = [1,2,5,7,9], arr3 = [1,3,4,5,8] +# Output: [1,5] +# Explanation: Only 1 and 5 appeared in the three arrays. +# +# Example 2: +# +# Input: arr1 = [197,418,523,876,1356], arr2 = [501,880,1593,1710,1870], arr3 = [521,682,1337,1395,1764] +# Output: [] +# +# + +# +# Approach: Two-pointer +# + +# Complexity Analysis +# +# Time Complexity: O(n), where n is the total length of all of the +# input arrays. +# Space Complexity: O(1), as we only initiate three integer variables +# using constant space. + +# @param {Integer[]} arr1 +# @param {Integer[]} arr2 +# @param {Integer[]} arr3 +# @return {Integer[]} +def arrays_intersection(arr1, arr2, arr3) + result = [] + + # prepare three pointers to iterate through three arrays + # p1, p2, and p3 point to the beginning of arr1, arr2, and arr3 accordingly + p1 = p2 = p3 = 0 + + while (p1 < arr1.count) && (p2 < arr2.count) && (p3 < arr3.count) + if arr1[p1] == arr2[p2] && arr1[p1] == arr3[p3] + result.push(arr1[p1]) + + p1 += 1 + p2 += 1 + p3 += 1 + elsif arr1[p1] < arr2[p2] + p1 += 1 + elsif arr2[p2] < arr3[p3] + p2 += 1 + else + p3 += 1 + end + end + + result +end + +arr1 = [1, 2, 3, 4, 5] +arr2 = [1, 2, 5, 7, 9] +arr3 = [1, 3, 4, 5, 8] +print(arrays_intersection(arr1, arr2, arr3)) +# Output: [1,5] + +arr1 = [197, 418, 523, 876, 1356] +arr2 = [501, 880, 1593, 1710, 1870] +arr3 = [521, 682, 1337, 1395, 1764] +print(arrays_intersection(arr1, arr2, arr3)) +# Output: [] From 6a076bde0750af89a078e98a49c0e7c0b4735f33 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Fri, 16 Apr 2021 00:25:50 +0000 Subject: [PATCH 71/83] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index df4f340..4c08933 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -14,6 +14,7 @@ ## Data Structures * Arrays * [Add Digits](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/add_digits.rb) + * [Arrays Intersection](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/arrays_intersection.rb) * [Find All Duplicates In An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/find_all_duplicates_in_an_array.rb) * [Find The Highest Altitude](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/find_the_highest_altitude.rb) * [Fizz Buzz](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/fizz_buzz.rb) From da89bae9314f1dc838ec299556324908a479f1f3 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Thu, 15 Apr 2021 17:27:27 -0700 Subject: [PATCH 72/83] Update arrays_intersection.rb --- data_structures/arrays/arrays_intersection.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/arrays/arrays_intersection.rb b/data_structures/arrays/arrays_intersection.rb index 63db80e..c9780e0 100644 --- a/data_structures/arrays/arrays_intersection.rb +++ b/data_structures/arrays/arrays_intersection.rb @@ -14,7 +14,7 @@ # # -# Approach: Two-pointer +# Approach: Two-pointers # # Complexity Analysis From b101ef25d7aec58f631293f0ff91c355c298120a Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Thu, 15 Apr 2021 18:00:29 -0700 Subject: [PATCH 73/83] Update and rename arrays_intersection.rb to sorted_arrays_intersection.rb --- ...arrays_intersection.rb => sorted_arrays_intersection.rb} | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) rename data_structures/arrays/{arrays_intersection.rb => sorted_arrays_intersection.rb} (90%) diff --git a/data_structures/arrays/arrays_intersection.rb b/data_structures/arrays/sorted_arrays_intersection.rb similarity index 90% rename from data_structures/arrays/arrays_intersection.rb rename to data_structures/arrays/sorted_arrays_intersection.rb index c9780e0..a48a7c2 100644 --- a/data_structures/arrays/arrays_intersection.rb +++ b/data_structures/arrays/sorted_arrays_intersection.rb @@ -28,7 +28,7 @@ # @param {Integer[]} arr2 # @param {Integer[]} arr3 # @return {Integer[]} -def arrays_intersection(arr1, arr2, arr3) +def sorted_arrays_intersection(arr1, arr2, arr3) result = [] # prepare three pointers to iterate through three arrays @@ -57,11 +57,11 @@ end arr1 = [1, 2, 3, 4, 5] arr2 = [1, 2, 5, 7, 9] arr3 = [1, 3, 4, 5, 8] -print(arrays_intersection(arr1, arr2, arr3)) +print(sorted_arrays_intersection(arr1, arr2, arr3)) # Output: [1,5] arr1 = [197, 418, 523, 876, 1356] arr2 = [501, 880, 1593, 1710, 1870] arr3 = [521, 682, 1337, 1395, 1764] -print(arrays_intersection(arr1, arr2, arr3)) +print(sorted_arrays_intersection(arr1, arr2, arr3)) # Output: [] From b1131f996a577f874dd3470d373daed7cf302c83 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Fri, 16 Apr 2021 01:00:45 +0000 Subject: [PATCH 74/83] updating DIRECTORY.md --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 4c08933..1028fcd 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -14,7 +14,6 @@ ## Data Structures * Arrays * [Add Digits](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/add_digits.rb) - * [Arrays Intersection](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/arrays_intersection.rb) * [Find All Duplicates In An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/find_all_duplicates_in_an_array.rb) * [Find The Highest Altitude](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/find_the_highest_altitude.rb) * [Fizz Buzz](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/fizz_buzz.rb) @@ -25,6 +24,7 @@ * [Shuffle Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/shuffle_array.rb) * [Single Number](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/single_number.rb) * [Sort Squares Of An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/sort_squares_of_an_array.rb) + * [Sorted Arrays Intersection](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/sorted_arrays_intersection.rb) * Strings * [Anagram Checker](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/anagram_checker.rb) * [Jewels And Stones](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/jewels_and_stones.rb) From 2c11993806af4b8d91228274faf2576c325dc283 Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Fri, 16 Apr 2021 14:12:30 -0700 Subject: [PATCH 75/83] Add brute force solution --- data_structures/arrays/intersection.rb | 29 ++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/data_structures/arrays/intersection.rb b/data_structures/arrays/intersection.rb index bd826a0..9633d7b 100644 --- a/data_structures/arrays/intersection.rb +++ b/data_structures/arrays/intersection.rb @@ -7,17 +7,38 @@ # @return {Integer[]} # -# Approach 1: +# Approach 1: Brute Force # -# Time Complexity: +# Time Complexity: O(n^2) # def intersect(arr1, arr2) + result = [] + + if arr1.length < arr2.length + shorter = arr1 + longer = arr2 + else + shorter = arr2 + longer = arr1 + end + + shorter.each do |matcher| + longer.each do |number| + next if number != matcher + result.push(number) + break + end + end + + result end + nums1 = [1, 2, 2, 1] nums2 = [2, 2] -intersect(nums1, nums2) +puts intersect(nums1, nums2) # => [2,2] + nums1 = [4, 9, 5] nums2 = [9, 4, 9, 8, 4] -intersect(nums1, nums2) +puts intersect(nums1, nums2) # => [4,9] \ No newline at end of file From 7b20bca2f82d02d9bc7046bc7d386a11f7389c84 Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Fri, 16 Apr 2021 16:21:49 -0700 Subject: [PATCH 76/83] Add hash solution --- data_structures/arrays/intersection.rb | 32 ++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/data_structures/arrays/intersection.rb b/data_structures/arrays/intersection.rb index 9633d7b..a59f58c 100644 --- a/data_structures/arrays/intersection.rb +++ b/data_structures/arrays/intersection.rb @@ -38,6 +38,38 @@ nums2 = [2, 2] puts intersect(nums1, nums2) # => [2,2] +nums1 = [4, 9, 5] +nums2 = [9, 4, 9, 8, 4] +puts intersect(nums1, nums2) +# => [4,9] + +# +# Approach 2: Hash +# +# Time Complexity: O(n) +# +def intersect(arr1, arr2) + result = [] + + hash = Hash.new(0) + + arr2.each {|num| hash[num] += 1 } + + arr1.each do |num| + if hash.has_key?(num) + result << num if hash[num] >= 1 + hash[num] -= 1 + end + end + + result +end + +nums1 = [1, 2, 2, 1] +nums2 = [2, 2] +puts intersect(nums1, nums2) +# => [2,2] + nums1 = [4, 9, 5] nums2 = [9, 4, 9, 8, 4] puts intersect(nums1, nums2) From b0287375a44d9c55bf992fae05028f0ccf51efcb Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Fri, 16 Apr 2021 16:33:19 -0700 Subject: [PATCH 77/83] Add two pointers solution --- data_structures/arrays/intersection.rb | 41 +++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/data_structures/arrays/intersection.rb b/data_structures/arrays/intersection.rb index a59f58c..b0091bc 100644 --- a/data_structures/arrays/intersection.rb +++ b/data_structures/arrays/intersection.rb @@ -61,7 +61,7 @@ def intersect(arr1, arr2) hash[num] -= 1 end end - + result end @@ -70,6 +70,45 @@ nums2 = [2, 2] puts intersect(nums1, nums2) # => [2,2] +nums1 = [4, 9, 5] +nums2 = [9, 4, 9, 8, 4] +puts intersect(nums1, nums2) +# => [4,9] + +# +# Approach 3: Two Pointers +# +# Time Complexity: O(n log n) +# +def intersect(nums1, nums2) + result = [] + p1 = 0 + p2 = 0 + nums1 = nums1.sort + nums2 = nums2.sort + while p1 < nums1.length && p2 < nums2.length + if nums1[p1] < nums2[p2] + p1 += 1 + elsif nums1[p1] > nums2[p2] + p2 += 1 + elsif nums1[p1] == nums2[p2] + result << nums1[p1] + p1 += 1 + p2 += 1 + end + end + + result +end +nums1 = [1, 2, 2, 1] +nums2 = [2, 2] +intersect(nums1, nums2) + +nums1 = [1, 2, 2, 1] +nums2 = [2, 2] +puts intersect(nums1, nums2) +# => [2,2] + nums1 = [4, 9, 5] nums2 = [9, 4, 9, 8, 4] puts intersect(nums1, nums2) From db2d1b74a299e15038b71b8d77a76d5791a88e59 Mon Sep 17 00:00:00 2001 From: Jessica Kwok <60627484+jsca-kwok@users.noreply.github.com> Date: Mon, 19 Apr 2021 15:54:48 -0700 Subject: [PATCH 78/83] Update data_structures/arrays/intersection.rb Co-authored-by: Vitor Oliveira --- data_structures/arrays/intersection.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/data_structures/arrays/intersection.rb b/data_structures/arrays/intersection.rb index b0091bc..a3cb29c 100644 --- a/data_structures/arrays/intersection.rb +++ b/data_structures/arrays/intersection.rb @@ -78,7 +78,10 @@ puts intersect(nums1, nums2) # # Approach 3: Two Pointers # -# Time Complexity: O(n log n) +# Complexity analysis: + +# Time Complexity: O(nlogn + mlogm), where n and m are the lengths of the arrays. We sort two arrays independently and then do a linear scan. +# Space Complexity: from O(logn+logm) to O(n+m), depending on the implementation of the sorting algorithm. # def intersect(nums1, nums2) result = [] @@ -112,4 +115,4 @@ puts intersect(nums1, nums2) nums1 = [4, 9, 5] nums2 = [9, 4, 9, 8, 4] puts intersect(nums1, nums2) -# => [4,9] \ No newline at end of file +# => [4,9] From 5c1580fc10038963e124a6b7fc3878aeacbf188d Mon Sep 17 00:00:00 2001 From: Jessica Kwok <60627484+jsca-kwok@users.noreply.github.com> Date: Mon, 19 Apr 2021 15:55:22 -0700 Subject: [PATCH 79/83] Update data_structures/arrays/intersection.rb Co-authored-by: Vitor Oliveira --- data_structures/arrays/intersection.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/data_structures/arrays/intersection.rb b/data_structures/arrays/intersection.rb index a3cb29c..dfcd49b 100644 --- a/data_structures/arrays/intersection.rb +++ b/data_structures/arrays/intersection.rb @@ -46,7 +46,14 @@ puts intersect(nums1, nums2) # # Approach 2: Hash # -# Time Complexity: O(n) +# Complexity Analysis +# +# Time Complexity: O(n+m), where n and m are the lengths of the arrays. +# We iterate through the first, and then through the second array; insert +# and lookup operations in the hash map take a constant time. +# +# Space Complexity: O(min(n,m)). We use hash map to store numbers (and their +# counts) from the smaller array. # def intersect(arr1, arr2) result = [] From 9f7e58c574648fce4bc4e5b9a4af1717df8f4a79 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 24 Apr 2021 23:19:41 -0700 Subject: [PATCH 80/83] Sort color algorhtm --- sorting/sort_color.rb | 63 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 sorting/sort_color.rb diff --git a/sorting/sort_color.rb b/sorting/sort_color.rb new file mode 100644 index 0000000..e1cb3a1 --- /dev/null +++ b/sorting/sort_color.rb @@ -0,0 +1,63 @@ +# Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue. +# +# We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively. +# +# Example 1: +# +# Input: nums = [2,0,2,1,1,0] +# Output: [0,0,1,1,2,2] +# +# Example 2: +# +# Input: nums = [2,0,1] +# Output: [0,1,2] +# +# Example 3: +# +# Input: nums = [0] +# Output: [0] +# +# Example 4: +# +# Input: nums = [1] +# Output: [1] + +# @param {Integer[]} nums +# @return {Void} Do not return anything, modify nums in-place instead. +def sort_colors(nums) + bubble_sort(nums) +end + +def bubble_sort(array) + array_length = array.size + return array if array_length <= 1 + + loop do + swapped = false + (array_length - 1).times do |i| + if array[i] > array[i + 1] + array[i], array[i + 1] = array[i + 1], array[i] + swapped = true + end + end + break unless swapped + end + + array +end + +nums = [2,0,2,1,1,0] +puts sort_colors(nums) +# Output: [0,0,1,1,2,2] + +nums = [2,0,1] +puts sort_colors(nums) +# Output: [0,1,2] + +nums = [0] +puts sort_colors(nums) +# Output: [0] + +nums = [1] +puts sort_colors(nums) +# Output: [1] From 812a8e42afa4c3c01e5f387b496b4cc7cfc69f35 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 25 Apr 2021 06:19:56 +0000 Subject: [PATCH 81/83] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index df4f340..59eb222 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -138,4 +138,5 @@ * [Selection Sort Test](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/selection_sort_test.rb) * [Shell Sort](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/shell_sort.rb) * [Shell Sort Test](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/shell_sort_test.rb) + * [Sort Color](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/sort_color.rb) * [Sort Tests](https://github.com/TheAlgorithms/Ruby/blob/master/sorting/sort_tests.rb) From 42dafdfe7aec3ada31cb7b2d1aeee562b04c2cd1 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 24 Apr 2021 23:21:08 -0700 Subject: [PATCH 82/83] enter --- sorting/sort_color.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sorting/sort_color.rb b/sorting/sort_color.rb index e1cb3a1..fd428ee 100644 --- a/sorting/sort_color.rb +++ b/sorting/sort_color.rb @@ -34,12 +34,14 @@ def bubble_sort(array) loop do swapped = false + (array_length - 1).times do |i| if array[i] > array[i + 1] array[i], array[i + 1] = array[i + 1], array[i] swapped = true end end + break unless swapped end From fe6d8489c354b2f030b74630b84159f80a50fbc9 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 24 Apr 2021 23:26:02 -0700 Subject: [PATCH 83/83] Fix lint --- sorting/sort_color.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sorting/sort_color.rb b/sorting/sort_color.rb index fd428ee..441679e 100644 --- a/sorting/sort_color.rb +++ b/sorting/sort_color.rb @@ -48,11 +48,11 @@ def bubble_sort(array) array end -nums = [2,0,2,1,1,0] +nums = [2, 0, 2, 1, 1, 0] puts sort_colors(nums) # Output: [0,0,1,1,2,2] -nums = [2,0,1] +nums = [2, 0, 1] puts sort_colors(nums) # Output: [0,1,2]