Update data_structures/arrays/two_sum.rb

This commit is contained in:
Vitor Oliveira 2021-03-11 21:22:34 -08:00 committed by GitHub
parent a6ae1c25b5
commit bdee1f9277
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -76,7 +76,11 @@ print two_sum([3, 3], 6)
#
# Approach 3: Using a Hash
#
# Time Complexity: O(N), where N is the length of the array
# 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 = {}
@ -96,4 +100,4 @@ print two_sum([2, 7, 11, 15], 9)
print two_sum([3, 2, 4], 6)
# => [1,2]
print two_sum([3, 3], 6)
# => [0,1]
# => [0,1]