From a6ae1c25b5053fd69135f56dca7dc005f812bbec Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Thu, 11 Mar 2021 15:53:01 -0800 Subject: [PATCH] Add solution using a hash --- data_structures/arrays/two_sum.rb | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/data_structures/arrays/two_sum.rb b/data_structures/arrays/two_sum.rb index 6fec6fc..adc5eb4 100644 --- a/data_structures/arrays/two_sum.rb +++ b/data_structures/arrays/two_sum.rb @@ -24,7 +24,7 @@ # @return {Integer[]} # -# Approach 1: Brute Force +# Approach 1: Brute Force with Addition # # Time Complexity: O(N^2), where N is the length of the array # @@ -51,7 +51,7 @@ print two_sum([3, 3], 6) # => [0,1] # -# Approach 2: Define and seek +# Approach 2: Brute Force with Difference # # Time Complexity: O(N^2), where N is the length of the array # @@ -66,6 +66,31 @@ def two_sum(nums, target) end end +print two_sum([2, 7, 11, 15], 9) +# => [0,1] +print two_sum([3, 2, 4], 6) +# => [1,2] +print two_sum([3, 3], 6) +# => [0,1] + +# +# Approach 3: Using a Hash +# +# Time Complexity: O(N), where N is the length of the array +# +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 + return [i, hash[difference_target]] if hash[difference_target] && hash[difference_target] != i + end +end + print two_sum([2, 7, 11, 15], 9) # => [0,1] print two_sum([3, 2, 4], 6)