From bdee1f9277dcf62853d525383c11f4dbed75b40a Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Thu, 11 Mar 2021 21:22:34 -0800 Subject: [PATCH] Update data_structures/arrays/two_sum.rb --- data_structures/arrays/two_sum.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/data_structures/arrays/two_sum.rb b/data_structures/arrays/two_sum.rb index adc5eb4..efc8fd6 100644 --- a/data_structures/arrays/two_sum.rb +++ b/data_structures/arrays/two_sum.rb @@ -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] \ No newline at end of file +# => [0,1]