Intersection of Two Arrays
Question
Given two arrays, write a function to compute their intersection.
Example 1:
Input: nums1 = [1,2,2,1], nums2 = [2,2] Output: [2]
Example 2:
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4] Output: [9,4]
Note:
- Each element in the result must be unique.
- The result can be in any order.
Approach 1: Array
Intuition and Algorithm
1 | class Solution(object): |
Complexity Analysis
- Time Complexity: O(N), where N is the
length of
nums1
. - Space Complexity: O(1).
- Runtime: 52 ms, faster than 23.52% of Python online submissions for Intersection of Two Arrays.
- Memory Usage: 11.8 MB, less than 86.37% of Python online submissions for Intersection of Two Arrays.
Approach 2: Binary Search
Intuition and Algorithm
The comparison A[i] < A[i+1]
in a mountain array
looks like
[True, True, True, ..., True, False, False, ..., False]
: 1
or more boolean True
s, followed by 1 or more boolean
False
. For example, in the mountain array
[1, 2, 3, 4, 1]
, the comparisons
A[i] < A[i+1]
would be
True, True, True, False
.
We can binary search over this array of comparisons, to find the
largest index i
such that A[i] < A[i+1]
.
For more on binary search, see the LeetCode
explore topic here.
1 | class Solution(object): |
Complexity Analysis
- Time complexity :
in the average case and in the worst case when load factor is high enough. - Space complexity :
in the worst case when all elements in the arrays are different. - Runtime: 28 ms, faster than 93.60% of Python online submissions for Intersection of Two Arrays.
- Memory Usage: 11.9 MB, less than 50.78% of Python online submissions for Intersection of Two Arrays.