mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-26 21:58:56 +01:00
Update data_structures/arrays/two_sum.rb
This commit is contained in:
parent
a6ae1c25b5
commit
bdee1f9277
1 changed files with 6 additions and 2 deletions
|
@ -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]
|
||||
|
|
Loading…
Reference in a new issue