mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-27 19:58:06 +01:00
Update data_structures/arrays/intersection.rb
Co-authored-by: Vitor Oliveira <vbrazo@gmail.com>
This commit is contained in:
parent
25d76f8944
commit
db2d1b74a2
1 changed files with 5 additions and 2 deletions
|
@ -78,7 +78,10 @@ puts intersect(nums1, nums2)
|
||||||
#
|
#
|
||||||
# Approach 3: Two Pointers
|
# Approach 3: Two Pointers
|
||||||
#
|
#
|
||||||
# Time Complexity: O(n log n)
|
# Complexity analysis:
|
||||||
|
|
||||||
|
# Time Complexity: O(nlogn + mlogm), where n and m are the lengths of the arrays. We sort two arrays independently and then do a linear scan.
|
||||||
|
# Space Complexity: from O(logn+logm) to O(n+m), depending on the implementation of the sorting algorithm.
|
||||||
#
|
#
|
||||||
def intersect(nums1, nums2)
|
def intersect(nums1, nums2)
|
||||||
result = []
|
result = []
|
||||||
|
@ -112,4 +115,4 @@ puts intersect(nums1, nums2)
|
||||||
nums1 = [4, 9, 5]
|
nums1 = [4, 9, 5]
|
||||||
nums2 = [9, 4, 9, 8, 4]
|
nums2 = [9, 4, 9, 8, 4]
|
||||||
puts intersect(nums1, nums2)
|
puts intersect(nums1, nums2)
|
||||||
# => [4,9]
|
# => [4,9]
|
||||||
|
|
Loading…
Add table
Reference in a new issue