mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-15 03:43:22 +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
|
# 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)
|
def two_sum(nums, target)
|
||||||
hash = {}
|
hash = {}
|
||||||
|
|
Loading…
Reference in a new issue