From 57a46a8271c8be49b199142fc0efa589a9bf6702 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Mon, 29 Mar 2021 15:35:18 -0700 Subject: [PATCH] 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]